-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
util.skip-bundled.js
109 lines (101 loc) 路 3.24 KB
/
util.skip-bundled.js
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
import { shouldTransform } from "../lib/util.js";
import babel from "@babel/core";
const { parseSync, traverse } = babel;
function getPath(input, parserOpts = {}) {
let targetPath;
traverse(
parseSync(input, {
parserOpts,
filename: "example.js",
configFile: false,
}),
{
"OptionalMemberExpression|OptionalCallExpression"(path) {
targetPath = path;
path.stop();
},
noScope: true,
},
);
return targetPath;
}
describe("shouldTransform", () => {
const positiveCases = [
"fn?.(...[], 0)",
"fn?.(...[], ...[])",
"fn?.(0, ...[], ...[])",
"a?.b(...[], 0)",
"a?.[b](...[], 0)",
"a?.b?.(...[], 0)",
"fn?.(0, ...[], 0)",
"a?.b?.(0, ...[], 0)",
"(a?.b)?.(...[], 0)",
"a?.b.c?.(...[], 0)",
"class C { #c; p = obj?.#c(...[], 0) }",
"class C { #c; p = obj.#c?.(...[], 0) }",
];
const negativeCases = [
"a?.b",
"fn?.(1)",
"fn?.(...[])",
"fn?.(1, ...[])",
"a?.b(...[])",
"a?.()(...[], 1)", // optional call under optional call is not affected
"(a?.b)(...[], 1)", // not an optional call
"a?.b.c(...[], 1)",
"a?.[fn?.(...[], 0)]", // optional chain in property will be handled when traversed
"a?.(fn?.(...[], 0))", // optional chain in arguments will be handled when traversed
"class C { #c; p = obj?.#c(...[]) }",
];
const typescriptPositiveCases = [
"(a?.(...[], 0) as any)?.b",
"(a?.(...[], 0) as any)?.()",
];
const typescriptNegativeCases = ["(a?.b as any)(...[], 0)"];
describe("default parser options", () => {
test.each(positiveCases)(
"shouldTransform(a?.b in %p) should return true",
input => {
expect(shouldTransform(getPath(input))).toBe(true);
},
);
test.each(negativeCases)(
"shouldTransform(a?.b in %p) should return false",
input => {
expect(shouldTransform(getPath(input))).toBe(false);
},
);
});
describe("createParenthesizedExpressions", () => {
test.each(positiveCases)(
"shouldTransform(a?.b in %p with { createParenthesizedExpressions: true }) should return true",
input => {
const parserOpts = { createParenthesizedExpressions: true };
expect(shouldTransform(getPath(input, parserOpts))).toBe(true);
},
);
test.each(negativeCases)(
"shouldTransform(a?.b in %p with { createParenthesizedExpressions: true }) should return false",
input => {
const parserOpts = { createParenthesizedExpressions: true };
expect(shouldTransform(getPath(input, parserOpts))).toBe(false);
},
);
});
describe("plugins: [typescript]", () => {
test.each(positiveCases.concat(typescriptPositiveCases))(
"shouldTransform(a?.b in %p with { plugins: [typescript] }) should return true",
input => {
const parserOpts = { plugins: ["typescript"] };
expect(shouldTransform(getPath(input, parserOpts))).toBe(true);
},
);
test.each(negativeCases.concat(typescriptNegativeCases))(
"shouldTransform(a?.b in %p with { plugins: [typescript] }) should return false",
input => {
const parserOpts = { plugins: ["typescript"] };
expect(shouldTransform(getPath(input, parserOpts))).toBe(false);
},
);
});
});