Debian Codegraph @

packages/babel-traverse/test/traverse.js

raw symbols SCIP
1 import { parse } from "@babel/parser";
2 import * as t from "@babel/types";
3
4 import _traverse from "../lib/index.js";
5 const traverse = _traverse.default || _traverse;
6
7 describe("traverse", function () {
8 const code = `
9 var foo = "bar";
10 this.test = "wow";
11 `;
12 const ast = parse(code);
13 const program = ast.program;
14 const body = program.body;
15
16 it("traverse replace", function () {
17 const replacement = {
18 type: "StringLiteral",
19 value: "foo",
20 };
21 const ast2 = JSON.parse(JSON.stringify(program));
22
23 traverse(ast2, {
24 enter: function (path) {
25 if (path.node.type === "ThisExpression") path.replaceWith(replacement);
26 },
27 });
28
29 expect(ast2.body[1].expression.left.object).toBe(replacement);
30 });
31
32 it("traverse", function () {
33 const expected = [
34 body[0],
35 body[0].declarations[0],
36 body[0].declarations[0].id,
37 body[0].declarations[0].init,
38 body[1],
39 body[1].expression,
40 body[1].expression.left,
41 body[1].expression.left.object,
42 body[1].expression.left.property,
43 body[1].expression.right,
44 ];
45
46 const actual = [];
47
48 traverse(program, {
49 enter: function (path) {
50 actual.push(path.node);
51 },
52 });
53
54 expect(actual).toEqual(expected);
55 });
56
57 it("traverse falsy parent", function () {
58 traverse(null, {
59 enter: function () {
60 throw new Error("should not be ran");
61 },
62 });
63 });
64
65 it("traverse denylistTypes", function () {
66 const expected = [
67 body[0],
68 body[0].declarations[0],
69 body[0].declarations[0].id,
70 body[0].declarations[0].init,
71 body[1],
72 body[1].expression,
73 body[1].expression.right,
74 ];
75
76 const actual = [];
77
78 traverse(program, {
79 denylist: ["MemberExpression"],
80 enter: function (path) {
81 actual.push(path.node);
82 },
83 });
84
85 expect(actual).toEqual(expected);
86 });
87
88 it("hasType", function () {
89 expect(traverse.hasType(ast, "ThisExpression")).toBeTruthy();
90 expect(
91 traverse.hasType(ast, "ThisExpression", ["AssignmentExpression"]),
92 ).toBeFalsy();
93
94 expect(traverse.hasType(ast, "ThisExpression")).toBeTruthy();
95 expect(traverse.hasType(ast, "Program")).toBeTruthy();
96
97 expect(
98 traverse.hasType(ast, "ThisExpression", ["MemberExpression"]),
99 ).toBeFalsy();
100 expect(traverse.hasType(ast, "ThisExpression", ["Program"])).toBeFalsy();
101
102 expect(traverse.hasType(ast, "ArrowFunctionExpression")).toBeFalsy();
103 });
104
105 it("clearCache", function () {
106 const paths = [];
107 const scopes = [];
108 traverse(ast, {
109 enter(path) {
110 scopes.push(path.scope);
111 paths.push(path);
112 path.stop();
113 },
114 });
115
116 traverse.cache.clear();
117
118 const paths2 = [];
119 const scopes2 = [];
120 traverse(ast, {
121 enter(path) {
122 scopes2.push(path.scope);
123 paths2.push(path);
124 path.stop();
125 },
126 });
127
128 scopes2.forEach(function (_, i) {
129 expect(scopes[i]).not.toBe(scopes2[i]);
130 expect(paths[i]).not.toBe(paths2[i]);
131 });
132 });
133
134 it("clearPath", function () {
135 const paths = [];
136 traverse(ast, {
137 enter(path) {
138 paths.push(path);
139 },
140 });
141
142 traverse.cache.clearPath();
143
144 const paths2 = [];
145 traverse(ast, {
146 enter(path) {
147 paths2.push(path);
148 },
149 });
150
151 paths2.forEach(function (p, i) {
152 expect(p).not.toBe(paths[i]);
153 });
154 });
155
156 it("clearScope", function () {
157 const scopes = [];
158 traverse(ast, {
159 enter(path) {
160 scopes.push(path.scope);
161 path.stop();
162 },
163 });
164
165 traverse.cache.clearScope();
166
167 const scopes2 = [];
168 traverse(ast, {
169 enter(path) {
170 scopes2.push(path.scope);
171 path.stop();
172 },
173 });
174
175 scopes2.forEach(function (p, i) {
176 expect(p).not.toBe(scopes[i]);
177 });
178 });
179
180 describe("path.skip()", function () {
181 it("replaced paths can be skipped", function () {
182 const ast = parse("id");
183
184 let skipped;
185 traverse(ast, {
186 noScope: true,
187 Identifier(path) {
188 path.replaceWith(t.numericLiteral(0));
189 path.skip();
190 skipped = true;
191 },
192 NumericLiteral() {
193 skipped = false;
194 },
195 });
196
197 expect(skipped).toBe(true);
198 });
199
200 // Skipped: see the comment in the `NodePath.requeue` method.
201 // eslint-disable-next-line jest/no-disabled-tests
202 it.skip("skipped and requeued paths should be visited", function () {
203 const ast = parse("id");
204
205 let visited = false;
206 traverse(ast, {
207 noScope: true,
208 Identifier(path) {
209 path.replaceWith(t.numericLiteral(0));
210 path.skip();
211 path.requeue();
212 },
213 NumericLiteral() {
214 visited = true;
215 },
216 });
217
218 expect(visited).toBe(true);
219 });
220 });
221 describe("path.visit()", () => {
222 it("should preserve traversal context after enter hook is executed", () => {
223 const ast = parse("{;}");
224 // The test initiates a sub-traverse from program. When the `enter` hook of BlockStatement
225 // is called, the unshiftContainer will change the traversal context of the BlockStatement
226 // to the one of Program which has an EmptyStatement visitor. If the traversal context
227 // is not restored after the `enter` hook is executed, the `EmptyStatement` visitor will
228 // be run twice: one in the sub-traverse and the other in the top level traverse.
229 let emptyStatementVisitedCounter = 0;
230 traverse(ast, {
231 noScope: true,
232 Program(path) {
233 path.traverse({
234 noScope: true,
235 BlockStatement: {
236 enter(path) {
237 path.parentPath.unshiftContainer("body", [t.numericLiteral(0)]);
238 },
239 },
240 });
241 },
242 EmptyStatement() {
243 ++emptyStatementVisitedCounter;
244 },
245 });
246 expect(emptyStatementVisitedCounter).toBe(1);
247 });
248 it("should preserve traversal context after visitor is executed", () => {
249 const ast = parse("{;}");
250 // The test initiates a sub-traverse from program. During the BlockStatement is traversed,
251 // the EmptyStatement visitor will be called and the unshiftContainer will change the
252 // traversal context of the BlockStatement to that of Program which has an EmptyStatement
253 // visitor. If the traversal context is not restored after `enter` hook is executed,
254 // the `BlockStatement:exit` visitor will be run twice: one in the sub-traverse and the other
255 // in the top level traverse.
256 let blockStatementVisitedCounter = 0;
257 traverse(ast, {
258 noScope: true,
259 Program(path) {
260 path.traverse({
261 noScope: true,
262 EmptyStatement: {
263 enter(path) {
264 path.parentPath.parentPath.unshiftContainer("body", [
265 t.numericLiteral(0),
266 ]);
267 },
268 },
269 });
270 },
271 BlockStatement: {
272 exit() {
273 ++blockStatementVisitedCounter;
274 },
275 },
276 });
277 expect(blockStatementVisitedCounter).toBe(1);
278 });
279 });
280 describe("path.stop()", () => {
281 it("should stop the traversal when a grand child is stopped", () => {
282 const ast = parse("f;g;");
283
284 let visitedCounter = 0;
285 traverse(ast, {
286 noScope: true,
287 Identifier(path) {
288 visitedCounter += 1;
289 path.stop();
290 },
291 });
292
293 expect(visitedCounter).toBe(1);
294 });
295
296 it("can be reverted in the exit listener of the parent whose child is stopped", () => {
297 const ast = parse("f;g;");
298
299 let visitedCounter = 0;
300 traverse(ast, {
301 noScope: true,
302 Identifier(path) {
303 visitedCounter += 1;
304 path.stop();
305 },
306 ExpressionStatement: {
307 exit(path) {
308 path.shouldStop = false;
309 path.shouldSkip = false;
310 },
311 },
312 });
313
314 expect(visitedCounter).toBe(2);
315 });
316
317 it("should not affect root traversal", () => {
318 const ast = parse("f;g;");
319
320 let visitedCounter = 0;
321 let programShouldStop;
322 traverse(ast, {
323 noScope: true,
324 Program(path) {
325 path.traverse({
326 noScope: true,
327 Identifier(path) {
328 visitedCounter += 1;
329 path.stop();
330 },
331 });
332 programShouldStop = path.shouldStop;
333 },
334 });
335
336 expect(visitedCounter).toBe(1);
337 expect(programShouldStop).toBe(false);
338 });
339 });
340 });