Debian Codegraph @

packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/test/util.skip-bundled.js

raw symbols SCIP
1 import { shouldTransform } from "../lib/util.js";
2 import { parseSync, traverse } from "@babel/core";
3
4 function getPath(input, parserOpts = {}) {
5 let targetPath;
6 traverse(
7 parseSync(input, {
8 parserOpts,
9 filename: "example.js",
10 configFile: false,
11 }),
12 {
13 "OptionalMemberExpression|OptionalCallExpression"(path) {
14 targetPath = path;
15 path.stop();
16 },
17 noScope: true,
18 },
19 );
20 return targetPath;
21 }
22
23 describe("shouldTransform", () => {
24 const positiveCases = [
25 "fn?.(...[], 0)",
26 "fn?.(...[], ...[])",
27 "fn?.(0, ...[], ...[])",
28 "a?.b(...[], 0)",
29 "a?.[b](...[], 0)",
30 "a?.b?.(...[], 0)",
31 "fn?.(0, ...[], 0)",
32 "a?.b?.(0, ...[], 0)",
33 "(a?.b)?.(...[], 0)",
34 "a?.b.c?.(...[], 0)",
35 "class C { #c; p = obj?.#c(...[], 0) }",
36 "class C { #c; p = obj.#c?.(...[], 0) }",
37 ];
38
39 const negativeCases = [
40 "a?.b",
41 "fn?.(1)",
42 "fn?.(...[])",
43 "fn?.(1, ...[])",
44 "a?.b(...[])",
45 "a?.()(...[], 1)", // optional call under optional call is not affected
46 "(a?.b)(...[], 1)", // not an optional call
47 "a?.b.c(...[], 1)",
48 "a?.[fn?.(...[], 0)]", // optional chain in property will be handled when traversed
49 "a?.(fn?.(...[], 0))", // optional chain in arguments will be handled when traversed
50 "class C { #c; p = obj?.#c(...[]) }",
51 ];
52
53 const typescriptPositiveCases = [
54 "(a?.(...[], 0) as any)?.b",
55 "(a?.(...[], 0) as any)?.()",
56 ];
57
58 const typescriptNegativeCases = ["(a?.b as any)(...[], 0)"];
59
60 describe("default parser options", () => {
61 test.each(positiveCases)(
62 "shouldTransform(a?.b in %p) should return true",
63 input => {
64 expect(shouldTransform(getPath(input))).toBe(true);
65 },
66 );
67 test.each(negativeCases)(
68 "shouldTransform(a?.b in %p) should return false",
69 input => {
70 expect(shouldTransform(getPath(input))).toBe(false);
71 },
72 );
73 });
74
75 describe("createParenthesizedExpressions", () => {
76 test.each(positiveCases)(
77 "shouldTransform(a?.b in %p with { createParenthesizedExpressions: true }) should return true",
78 input => {
79 const parserOpts = { createParenthesizedExpressions: true };
80 expect(shouldTransform(getPath(input, parserOpts))).toBe(true);
81 },
82 );
83 test.each(negativeCases)(
84 "shouldTransform(a?.b in %p with { createParenthesizedExpressions: true }) should return false",
85 input => {
86 const parserOpts = { createParenthesizedExpressions: true };
87 expect(shouldTransform(getPath(input, parserOpts))).toBe(false);
88 },
89 );
90 });
91
92 describe("plugins: [typescript]", () => {
93 test.each(positiveCases.concat(typescriptPositiveCases))(
94 "shouldTransform(a?.b in %p with { plugins: [typescript] }) should return true",
95 input => {
96 const parserOpts = { plugins: ["typescript"] };
97 expect(shouldTransform(getPath(input, parserOpts))).toBe(true);
98 },
99 );
100 test.each(negativeCases.concat(typescriptNegativeCases))(
101 "shouldTransform(a?.b in %p with { plugins: [typescript] }) should return false",
102 input => {
103 const parserOpts = { plugins: ["typescript"] };
104 expect(shouldTransform(getPath(input, parserOpts))).toBe(false);
105 },
106 );
107 });
108 });