Debian Codegraph @

packages/babel-helper-simple-access/test/index.js

raw symbols SCIP
1 import * as babel from "@babel/core";
2 import simplifyAccess from "../lib/index.js";
3
4 const plugin = (_api, options) => {
5 const { includeUpdateExpression, bindingNames } = options;
6
7 return {
8 visitor: {
9 Program(path) {
10 (simplifyAccess.default || simplifyAccess)(
11 path,
12 new Set(bindingNames),
13 includeUpdateExpression,
14 );
15 },
16 },
17 };
18 };
19
20 it("simplifyAccess with default config", function () {
21 const code = `
22 let a = foo++;
23 a = ++foo;
24 foo++;
25 ++foo;
26 ++a;
27 a++;
28 foo = a++;
29 foo = ++a;
30
31 let b = bar--;
32 b = --bar;
33 bar--;
34 --bar;
35 --b;
36 b--;
37 bar = b--;
38 bar = --b;
39
40 let c = baz += 1;
41 baz += 1;
42 c += 1;
43
44 function f() {
45 let foo = 1;
46 let a = foo++;
47 a = ++foo;
48 foo++;
49 ++foo;
50 ++a;
51 a++;
52 foo = a++;
53 foo = ++a;
54 }
55 `;
56
57 const output = babel.transformSync(code, {
58 configFile: false,
59 ast: false,
60 plugins: [[plugin, { bindingNames: ["foo", "bar", "baz"] }]],
61 }).code;
62
63 expect(output).toMatchInlineSnapshot(`
64 "var _foo, _bar;
65 let a = (_foo = +foo, foo = _foo + 1, _foo);
66 a = foo = +foo + 1;
67 foo = foo + 1;
68 foo = foo + 1;
69 ++a;
70 a++;
71 foo = a++;
72 foo = ++a;
73 let b = (_bar = +bar, bar = _bar - 1, _bar);
74 b = bar = +bar - 1;
75 bar = bar - 1;
76 bar = bar - 1;
77 --b;
78 b--;
79 bar = b--;
80 bar = --b;
81 let c = baz = baz + 1;
82 baz = baz + 1;
83 c += 1;
84 function f() {
85 let foo = 1;
86 let a = foo++;
87 a = ++foo;
88 foo++;
89 ++foo;
90 ++a;
91 a++;
92 foo = a++;
93 foo = ++a;
94 }"
95 `);
96 });
97
98 it("simplifyAccess with includeUpdateExpression=false", function () {
99 const code = `
100 let a = foo++;
101 a = ++foo;
102 foo++;
103 ++foo;
104 ++a;
105 a++;
106 foo = a++;
107 foo = ++a;
108
109 let b = bar--;
110 b = --bar;
111 bar--;
112 --bar;
113 --b;
114 b--;
115 bar = b--;
116 bar = --b;
117
118 let c = baz += 1;
119 baz += 1;
120 c += 1;
121
122 function f() {
123 let foo = 1;
124 let a = foo++;
125 a = ++foo;
126 foo++;
127 ++foo;
128 ++a;
129 a++;
130 foo = a++;
131 foo = ++a;
132 }
133 `;
134
135 const output = babel.transformSync(code, {
136 configFile: false,
137 ast: false,
138 plugins: [
139 [
140 plugin,
141 { includeUpdateExpression: false, bindingNames: ["foo", "bar", "baz"] },
142 ],
143 ],
144 }).code;
145
146 expect(output).toMatchInlineSnapshot(`
147 "let a = foo++;
148 a = ++foo;
149 foo++;
150 ++foo;
151 ++a;
152 a++;
153 foo = a++;
154 foo = ++a;
155 let b = bar--;
156 b = --bar;
157 bar--;
158 --bar;
159 --b;
160 b--;
161 bar = b--;
162 bar = --b;
163 let c = baz = baz + 1;
164 baz = baz + 1;
165 c += 1;
166 function f() {
167 let foo = 1;
168 let a = foo++;
169 a = ++foo;
170 foo++;
171 ++foo;
172 ++a;
173 a++;
174 foo = a++;
175 foo = ++a;
176 }"
177 `);
178 });