-
-
Notifications
You must be signed in to change notification settings - Fork 161
/
fixtures.ts
250 lines (196 loc) · 5.44 KB
/
fixtures.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import type { PackageJson, TsConfigJson } from 'type-fest';
export const createPackageJson = (packageJson: PackageJson) => JSON.stringify(packageJson);
export const createTsconfig = (tsconfig: TsConfigJson) => JSON.stringify(tsconfig);
const cjsContextCheck = 'typeof module !== \'undefined\'';
const tsCheck = '1 as number';
const declareReact = `
const React = {
createElement: (...args) => Array.from(args),
};
`;
export const jsxCheck = '<><div>JSX</div></>';
const preserveName = `
assert(
(function functionName() {}).name === 'functionName',
'Name should be preserved'
);
`;
const syntaxLowering = `
// es2016 - Exponentiation operator
10 ** 4;
// es2017 - Async functions
(async () => {});
// es2018 - Spread properties
({...Object});
// es2018 - Rest properties
const {...x} = Object;
// es2019 - Optional catch binding
try {} catch {}
// es2020 - Optional chaining
Object?.keys;
// es2020 - Nullish coalescing
Object ?? true
// es2020 - import.meta
// import.meta
// es2021 - Logical assignment operators
// let a = false; a ??= true; a ||= true; a &&= true;
// es2022 - Class instance fields
(class { x });
// es2022 - Static class fields
(class { static x });
// es2022 - Private instance methods
(class { #x() {} });
// es2022 - Private instance fields
(class { #x });
// es2022 - Private static methods
(class { static #x() {} });
// es2022 - Private static fields
(class { static #x });
// es2022 - Class static blocks
(class { static {} });
export const named = 2;
export default 1;
`;
const sourcemap = {
// Adding the dynamic import helps test the import transformation's source map
test: (
extension: string,
) => `import ('node:fs');\nconst { stack } = new Error(); const searchString = 'index.${extension}:SOURCEMAP_LINE'; assert(stack.includes(searchString), \`Expected \${searchString} in stack: \${stack}\`)`,
tag: (
strings: TemplateStringsArray,
...values: string[]
) => {
const finalString = String.raw({ raw: strings }, ...values);
const lineNumber = finalString.split('\n').findIndex(line => line.includes('SOURCEMAP_LINE')) + 1;
return finalString.replaceAll('SOURCEMAP_LINE', lineNumber.toString());
},
};
export const expectErrors = {
'node_modules/expect-errors/index.js': `
exports.expectErrors = async (...assertions) => {
let errors = await Promise.all(
assertions.map(async ([fn, expectedError]) => {
let thrown;
try {
await fn();
} catch (error) {
thrown = error;
}
if (!thrown) {
return new Error('No error thrown');
} else if (!thrown.message.includes(expectedError)) {
return new Error(\`Message \${JSON.stringify(expectedError)} not found in \${JSON.stringify(thrown.message)}\n\${thrown.stack}\`);
}
}),
);
errors = errors.filter(Boolean);
if (errors.length > 0) {
console.error(errors);
process.exitCode = 1;
}
};
`,
};
export const files = {
...expectErrors,
'js/index.js': `
import assert from 'assert';
${syntaxLowering}
${preserveName}
export const cjsContext = ${cjsContextCheck};
`,
'json/index.json': JSON.stringify({ loaded: 'json' }),
'cjs/index.cjs': sourcemap.tag`
const assert = require('node:assert');
assert(${cjsContextCheck}, 'Should have CJS context');
${preserveName}
${sourcemap.test('cjs')}
// Assert __esModule is unwrapped
import ('../ts/index.ts').then((m) => assert(
!(typeof m.default === 'object' && ('default' in m.default)),
));
exports.named = 'named';
`,
'mjs/index.mjs': `
import assert from 'assert';
export const mjsHasCjsContext = ${cjsContextCheck};
import ('pkg-commonjs').then((m) => assert(
!(typeof m.default === 'object' && ('default' in m.default)),
));
`,
'ts/index.ts': sourcemap.tag`
import assert from 'assert';
import type {Type} from 'resolved-by-tsc'
interface Foo {}
type Foo = number
declare module 'foo' {}
enum BasicEnum {
Left,
Right,
}
enum NamedEnum {
SomeEnum = 'some-value',
}
export const a = BasicEnum.Left;
export const b = NamedEnum.SomeEnum;
export default function foo(): string {
return 'foo'
}
// For "ts as tsx" test
const bar = <T>(value: T) => fn<T>();
${preserveName}
${sourcemap.test('ts')}
export const cjsContext = ${cjsContextCheck};
${tsCheck};
`,
// TODO: test resolution priority for files 'index.tsx` & 'index.tsx.ts` via 'index.tsx'
'jsx/index.jsx': sourcemap.tag`
import assert from 'assert';
export const cjsContext = ${cjsContextCheck};
${declareReact}
export const jsx = ${jsxCheck};
${preserveName}
${sourcemap.test('jsx')}
`,
'tsx/index.tsx': sourcemap.tag`
import assert from 'assert';
export const cjsContext = ${cjsContextCheck};
${tsCheck};
${declareReact}
export const jsx = ${jsxCheck};
${preserveName}
${sourcemap.test('tsx')}
`,
'mts/index.mts': sourcemap.tag`
import assert from 'assert';
export const mjsHasCjsContext = ${cjsContextCheck};
${tsCheck};
${preserveName}
${sourcemap.test('mts')}
`,
'cts/index.cts': sourcemap.tag`
const assert = require('assert');
assert(${cjsContextCheck}, 'Should have CJS context');
${tsCheck};
${preserveName}
${sourcemap.test('cts')}
`,
'file.txt': 'hello',
'broken-syntax.ts': 'if',
node_modules: {
'pkg-commonjs': {
'package.json': createPackageJson({
type: 'commonjs',
}),
'index.js': syntaxLowering,
},
'pkg-module': {
'package.json': createPackageJson({
type: 'module',
main: './index.js',
}),
'index.js': `${syntaxLowering}\nexport * from "./empty-export"`,
'empty-export/index.js': 'export {}',
},
},
};