-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy patherrors.test.ts
More file actions
377 lines (346 loc) · 12.6 KB
/
Copy patherrors.test.ts
File metadata and controls
377 lines (346 loc) · 12.6 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import { it, mock } from "node:test";
import assert from "node:assert";
import tsBlankSpace from "../src/index.ts";
import ts from "typescript";
it("errors on enums", () => {
const onError = mock.fn();
const out = tsBlankSpace(
`
enum E1 {}
export enum E2 {}
`,
onError,
);
assert.equal(onError.mock.callCount(), 2);
assert.equal(
out,
`
enum E1 {}
export enum E2 {}
`,
);
});
it("errors on parameter properties", () => {
const onError = mock.fn();
const out = tsBlankSpace(
`
class C {
constructor(public a, private b, protected c, readonly d) {}
}
`,
onError,
);
assert.equal(onError.mock.callCount(), 4);
assert.equal(
out,
`
class C {
constructor(public a, private b, protected c, readonly d) {}
}
`,
);
});
function errorCallbackToModuleDeclarationNames(onError: import("node:test").Mock<(...args: any[]) => void>): string[] {
return onError.mock.calls.map(({ arguments: [node] }) => {
assert(ts.isModuleDeclaration(node));
assert(ts.isIdentifier(node.name));
return node.name.escapedText.toString();
});
}
it("errors on TypeScript `module` declarations due to overlap with github.com/tc39/proposal-module-declarations", () => {
const onError = mock.fn();
const out = tsBlankSpace(
`
module A {}
module B { export type T = string; }
module C { export const V = ""; }
module D.E {}
`,
onError,
);
assert.equal(onError.mock.callCount(), 4);
const errorNodeNames = errorCallbackToModuleDeclarationNames(onError);
assert.deepEqual(errorNodeNames, ["A", "B", "C", "D"]);
assert.equal(
out,
`
module A {}
module B { export type T = string; }
module C { export const V = ""; }
module D.E {}
`,
);
});
it("errors on instantiated namespaces due to having runtime emit", () => {
const onError = mock.fn();
const out = tsBlankSpace(
`
namespace A { 1; }
namespace B { globalThis; }
namespace C { export let x; }
namespace D { declare let x; }
namespace E { export type T = any; 2; }
namespace F { export namespace Inner { 3; } }
namespace G.H { 4; }
namespace I { export import X = E.T }
namespace J { {} }
`,
onError,
);
assert.equal(onError.mock.callCount(), 9);
const errorNodeNames = errorCallbackToModuleDeclarationNames(onError);
assert.deepEqual(errorNodeNames, ["A", "B", "C", "D", "E", "F", "G", /* H (nested)*/ "I", "J"]);
assert.equal(
out,
`
namespace A { 1; }
namespace B { globalThis; }
namespace C { export let x; }
namespace D { declare let x; }
namespace E { export type T = any; 2; }
namespace F { export namespace Inner { 3; } }
namespace G.H { 4; }
namespace I { export import X = E.T }
namespace J { {} }
`,
);
});
it("importing instantiated namespace", () => {
const onError = mock.fn();
const out = tsBlankSpace(
`
namespace A { export let x = 1; }
namespace B { import x = A.x; }
namespace C { export import x = A.x; }
`,
onError,
);
assert.equal(onError.mock.callCount(), 2);
const errorNodeNames = errorCallbackToModuleDeclarationNames(onError);
assert.deepEqual(errorNodeNames, ["A", "C"]);
// Only 'B' is erased:
assert.equal(
out,
[
``,
` namespace A { export let x = 1; }`,
` ; `,
` namespace C { export import x = A.x; }`,
` `,
].join("\n"),
);
});
it("errors on declared legacy modules", () => {
const onError = mock.fn();
const out = tsBlankSpace(`declare module M {}\n`, onError);
assert.equal(onError.mock.callCount(), 1);
assert.equal(out, "declare module M {}\n");
});
it("errors on non-instantiated legacy modules", () => {
const onError = mock.fn();
const out = tsBlankSpace(`module M {}\n`, onError);
assert.equal(onError.mock.callCount(), 1);
assert.equal(out, "module M {}\n");
});
it("errors on CJS export assignment syntax", () => {
const onError = mock.fn();
const out = tsBlankSpace(
`
export = 1;
`,
onError,
);
assert.equal(onError.mock.callCount(), 1);
assert.equal(
out,
`
export = 1;
`,
);
});
it("errors on CJS import syntax", () => {
const onError = mock.fn();
const out = tsBlankSpace(
`
import lib = require("");
`,
onError,
);
assert.equal(onError.mock.callCount(), 1);
assert.equal(
out,
`
import lib = require("");
`,
);
});
it("errors on `as` expression that would change operator precedence if erased", () => {
const onError = mock.fn();
const tsInput = `1+1 as T / 2`;
let jsOutput = tsBlankSpace(tsInput, onError);
assert.equal(onError.mock.callCount(), 1);
// TypeScript would emit `(1+1) / 2`, but ts-blank-space can't insert
// the leading `(` without shifting offsets, so it must error.
assert.equal(jsOutput, `1+1 as T / 2`);
});
it("errors on (invalid) `as const` expression that would change operator precedence if evaluated", () => {
const onError = mock.fn();
const tsInput = `1+1 as const / 2`;
let jsOutput = tsBlankSpace(tsInput, onError);
assert.equal(onError.mock.callCount(), 1);
// TypeScript would emit `(1+1) / 2`, but ts-blank-space can't insert
// the leading `(` without shifting offsets, so it must error.
assert.equal(jsOutput, `1+1 as const / 2`);
});
it("errors on nested `as` expressions that would change operator precedence if erased", () => {
const onError = mock.fn();
const tsInput = `1 + 1 as unknown as number / 2`;
const jsOutput = tsBlankSpace(tsInput, onError);
assert.equal(onError.mock.callCount(), 1);
assert.equal(jsOutput, `1 + 1 as unknown as number / 2`);
});
it("errors on (invalid) ambiguous `??` precedence that would error if erased", () => {
logicalOr: {
const onError = mock.fn();
// TypeScript produces an error for this input:
const tsInput = `a ?? b as any || 2`; // "ts(5076): '??' and '||' operations cannot be mixed without parentheses"
// If that TS error was ignored the TS emitted code would actually be valid
// because TS adds parenthesis: `(a ?? b) || 2`.
// `ts-blank-space` would only erase, meaning there would be a runtime syntax error.
// To catch this early we should emit an error.
const jsOutput = tsBlankSpace(tsInput, onError);
assert.equal(onError.mock.callCount(), 1);
assert.equal(jsOutput, `a ?? b as any || 2`);
}
logicalAnd: {
const onError = mock.fn();
const tsInput = `a ?? b as any && 2`; // "ts(5076): '??' and '||' operations cannot be mixed without parentheses"
const jsOutput = tsBlankSpace(tsInput, onError);
assert.equal(onError.mock.callCount(), 1);
assert.equal(jsOutput, `a ?? b as any && 2`);
}
logicalOrReversed: {
const onError = mock.fn();
const tsInput = `a || b as any ?? 2`;
const jsOutput = tsBlankSpace(tsInput, onError);
assert.equal(onError.mock.callCount(), 1);
assert.equal(jsOutput, `a || b as any ?? 2`);
}
logicalAndReversed: {
const onError = mock.fn();
const tsInput = `a && b as any ?? 2`;
const jsOutput = tsBlankSpace(tsInput, onError);
assert.equal(onError.mock.callCount(), 1);
assert.equal(jsOutput, `a && b as any ?? 2`);
}
satisfies: {
const onError = mock.fn();
const tsInput = `a ?? b satisfies any || 2`;
const jsOutput = tsBlankSpace(tsInput, onError);
assert.equal(onError.mock.callCount(), 1);
assert.equal(jsOutput, `a ?? b satisfies any || 2`);
}
assertionChain: {
const onError = mock.fn();
const tsInput = `a ?? b as any as unknown && 2`;
const jsOutput = tsBlankSpace(tsInput, onError);
assert.equal(onError.mock.callCount(), 1);
assert.equal(jsOutput, `a ?? b as any as unknown && 2`);
}
});
it("errors on `??` mix nested inside a larger expression", () => {
threeOperatorChain: {
// `a ?? b as T && c || d` parses as `||[??[a, &&[as[b, T], c]], d]`
// The ?? mix error is caught at the inner `??` node
const onError = mock.fn();
const tsInput = `a ?? b as T && c || d`;
const jsOutput = tsBlankSpace(tsInput, onError);
assert.equal(onError.mock.callCount(), 1);
assert.equal(jsOutput, `a ?? b as T && c || d`);
}
precedenceErrorDeeperInTree: {
// `1 + 2 as T * 3 + 4` parses as `+[*[as[+[1, 2], T], 3], 4]`
// The precedence error (+ vs *) is caught on the nested `as` node
const onError = mock.fn();
const tsInput = `1 + 2 as T * 3 + 4`;
const jsOutput = tsBlankSpace(tsInput, onError);
assert.equal(onError.mock.callCount(), 1);
assert.equal(jsOutput, `1 + 2 as T * 3 + 4`);
}
});
it("errors even with comments between assertion chain and next operator", () => {
const onError = mock.fn();
const tsInput = `1 + 1 as unknown /* comment */ / 2`;
const jsOutput = tsBlankSpace(tsInput, onError);
assert.equal(onError.mock.callCount(), 1);
assert.equal(jsOutput, `1 + 1 as unknown /* comment */ / 2`);
});
it("covers assertion-chain binary precedence error cases across operators", () => {
const runErrorCase = (input: string, context: string) => {
const onError = mock.fn();
const jsOutput = tsBlankSpace(input, onError);
assert.equal(onError.mock.callCount(), 1, `unexpected onError count: ${context}`);
assert.equal(jsOutput, input, `should preserve source when it errors: ${context}`);
};
runErrorCase(`1 ** 1 as unknown ** 2`, "same operator **");
const higherPrecedenceCases = [
{ base: "<", next: "<<" },
{ base: "<=", next: "<<" },
{ base: ">", next: "<<" },
{ base: ">=", next: "<<" },
{ base: "instanceof", next: "<<" },
{ base: "in", next: "<<" },
{ base: "<<", next: "+" },
{ base: ">>", next: "+" },
{ base: ">>>", next: "+" },
{ base: "+", next: "*" },
{ base: "-", next: "*" },
{ base: "*", next: "**" },
{ base: "/", next: "**" },
{ base: "%", next: "**" },
];
for (const { base, next } of higherPrecedenceCases) {
runErrorCase(`a ${base} a as unknown ${next} a`, `${next} should outrank ${base}`);
}
runErrorCase(`1 + 1 as unknown * 2`, "targeted higher-precedence operator");
});
it("errors on `satisfies` expression that would change operator precedence", () => {
const onError = mock.fn();
const tsInput = `1+1 satisfies T / 2`;
let jsOutput = tsBlankSpace(tsInput, onError);
assert.equal(onError.mock.callCount(), 1);
assert.equal(jsOutput, `1+1 satisfies T / 2`);
});
it("errors on prefix type assertion", () => {
const onError = mock.fn();
const tsInput = `let x = <string>"test";`;
let jsOutput = tsBlankSpace(tsInput, onError);
assert.equal(onError.mock.callCount(), 1);
assert.equal(jsOutput, `let x = <string>"test";`);
});
it("errors on prefix type assertion as arrow body within binary expressions", () => {
const onError = mock.fn();
const tsInput = `(()=><any>{p:null}.p ?? 1);`;
let jsOutput = tsBlankSpace(tsInput, onError);
assert.equal(onError.mock.callCount(), 1);
assert.equal(jsOutput, `(()=><any>{p:null}.p ?? 1);`);
});
// (negative) Test cases from https://github.com/microsoft/TypeScript/issues/63533
it("error with the test cases from TypeScript issue 63533", () => {
const cases = [
`export const x03 = 1 + 1 as number * 2;`,
`export const x04 = 1 + 1 as any as number * 2;`,
`export const x23 = 1 >> 1 as number + 2;`,
`export const x24 = 1 >> 1 as any as number + 2;`,
`export const y03 = 1 + 1 satisfies number * 2;`,
`export const y04 = 1 + 1 satisfies any satisfies number * 2;`,
`export const y23 = 1 >> 1 satisfies number + 2;`,
`export const y24 = 1 >> 1 satisfies any satisfies number + 2;`,
];
for (const tsInput of cases) {
const onError = mock.fn();
const jsOutput = tsBlankSpace(tsInput, onError);
assert.equal(onError.mock.callCount(), 1);
assert.equal(jsOutput, tsInput);
}
});