Skip to content

Commit c8ed09f

Browse files
committed
add polyfill originals to test against
1 parent df5baab commit c8ed09f

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// NOTE: Unlike other types files, this is NOT auto-generated by our build scripts. Since these functions are the
2+
// originals of functions we adapted from Rollup and Sucrase, there's no reason they should ever change, but if they do,
3+
// this file needs to be regenerated, by running
4+
// `yarn tsc --allowJs --skipLibCheck --declaration --emitDeclarationOnly test/buildPolyfills/originals.js`
5+
// from within the `utils` package. Keep in mind that running that command will clobber this note, so make sure to copy
6+
// it before you regenerate the types, so you can add it back in.)
7+
8+
export function _asyncNullishCoalesce(lhs: any, rhsFn: any): Promise<any>;
9+
export function _asyncOptionalChain(ops: any): Promise<any>;
10+
export function _asyncOptionalChainDelete(ops: any): Promise<any>;
11+
export function _createNamedExportFrom(obj: any, localName: any, importedName: any): void;
12+
export function _createStarExport(obj: any): void;
13+
export function _interopDefault(e: any): any;
14+
export function _interopNamespace(e: any): any;
15+
export function _interopNamespaceDefaultOnly(e: any): {
16+
__proto__: any;
17+
default: any;
18+
};
19+
export function _interopRequireDefault(obj: any): any;
20+
export function _interopRequireWildcard(obj: any): any;
21+
export function _nullishCoalesce(lhs: any, rhsFn: any): any;
22+
export function _optionalChain(ops: any): any;
23+
export function _optionalChainDelete(ops: any): any;
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// Originals of the buildPolyfills from Sucrase and Rollup we use (which we have adapted in various ways), preserved here for testing, to prove that
2+
// the modified versions do the same thing the originals do.
3+
4+
// From Sucrase
5+
export async function _asyncNullishCoalesce(lhs, rhsFn) {
6+
if (lhs != null) {
7+
return lhs;
8+
} else {
9+
return await rhsFn();
10+
}
11+
}
12+
13+
// From Sucrase
14+
export async function _asyncOptionalChain(ops) {
15+
let lastAccessLHS = undefined;
16+
let value = ops[0];
17+
let i = 1;
18+
while (i < ops.length) {
19+
const op = ops[i];
20+
const fn = ops[i + 1];
21+
i += 2;
22+
if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) {
23+
return undefined;
24+
}
25+
if (op === 'access' || op === 'optionalAccess') {
26+
lastAccessLHS = value;
27+
value = await fn(value);
28+
} else if (op === 'call' || op === 'optionalCall') {
29+
value = await fn((...args) => value.call(lastAccessLHS, ...args));
30+
lastAccessLHS = undefined;
31+
}
32+
}
33+
return value;
34+
}
35+
36+
// From Sucrase
37+
export async function _asyncOptionalChainDelete(ops) {
38+
const result = await _asyncOptionalChain(ops);
39+
// by checking for loose equality to `null`, we catch both `null` and `undefined`
40+
return result == null ? true : result;
41+
}
42+
43+
// From Sucrase
44+
export function _createNamedExportFrom(obj, localName, importedName) {
45+
Object.defineProperty(exports, localName, { enumerable: true, get: () => obj[importedName] });
46+
}
47+
48+
// From Sucrase
49+
export function _createStarExport(obj) {
50+
Object.keys(obj)
51+
.filter(key => key !== 'default' && key !== '__esModule')
52+
.forEach(key => {
53+
// eslint-disable-next-line no-prototype-builtins
54+
if (exports.hasOwnProperty(key)) {
55+
return;
56+
}
57+
Object.defineProperty(exports, key, { enumerable: true, get: () => obj[key] });
58+
});
59+
}
60+
61+
// From Rollup
62+
export function _interopDefault(e) {
63+
return e && e.__esModule ? e['default'] : e;
64+
}
65+
66+
// From Rollup
67+
export function _interopNamespace(e) {
68+
if (e && e.__esModule) return e;
69+
var n = Object.create(null);
70+
if (e) {
71+
// eslint-disable-next-line guard-for-in
72+
for (var k in e) {
73+
n[k] = e[k];
74+
}
75+
}
76+
n['default'] = e;
77+
return n;
78+
}
79+
80+
export function _interopNamespaceDefaultOnly(e) {
81+
return {
82+
__proto__: null,
83+
default: e,
84+
};
85+
}
86+
87+
// From Sucrase
88+
export function _interopRequireDefault(obj) {
89+
return obj && obj.__esModule ? obj : { default: obj };
90+
}
91+
92+
// From Sucrase
93+
export function _interopRequireWildcard(obj) {
94+
if (obj && obj.__esModule) {
95+
return obj;
96+
} else {
97+
var newObj = {};
98+
if (obj != null) {
99+
for (var key in obj) {
100+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
101+
newObj[key] = obj[key];
102+
}
103+
}
104+
}
105+
newObj.default = obj;
106+
return newObj;
107+
}
108+
}
109+
110+
// From Sucrase
111+
export function _nullishCoalesce(lhs, rhsFn) {
112+
if (lhs != null) {
113+
return lhs;
114+
} else {
115+
return rhsFn();
116+
}
117+
}
118+
119+
// From Sucrase
120+
export function _optionalChain(ops) {
121+
let lastAccessLHS = undefined;
122+
let value = ops[0];
123+
let i = 1;
124+
while (i < ops.length) {
125+
const op = ops[i];
126+
const fn = ops[i + 1];
127+
i += 2;
128+
if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) {
129+
return undefined;
130+
}
131+
if (op === 'access' || op === 'optionalAccess') {
132+
lastAccessLHS = value;
133+
value = fn(value);
134+
} else if (op === 'call' || op === 'optionalCall') {
135+
value = fn((...args) => value.call(lastAccessLHS, ...args));
136+
lastAccessLHS = undefined;
137+
}
138+
}
139+
return value;
140+
}
141+
142+
// From Sucrase
143+
export function _optionalChainDelete(ops) {
144+
const result = _optionalChain(ops);
145+
// by checking for loose equality to `null`, we catch both `null` and `undefined`
146+
return result == null ? true : result;
147+
}

0 commit comments

Comments
 (0)