]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/shared/traverser.js
first commit
[pve-eslint.git] / eslint / tests / lib / shared / traverser.js
1 "use strict";
2
3 const assert = require("chai").assert;
4 const Traverser = require("../../../lib/shared/traverser");
5
6 describe("Traverser", () => {
7 it("traverses all keys except 'parent', 'leadingComments', and 'trailingComments'", () => {
8 const traverser = new Traverser();
9 const fakeAst = {
10 type: "Program",
11 body: [
12 {
13 type: "ExpressionStatement",
14 leadingComments: {
15 type: "Line"
16 },
17 trailingComments: {
18 type: "Block"
19 }
20 },
21 {
22 type: "FooStatement",
23 foo: {
24 type: "BarStatement"
25 }
26 }
27 ]
28 };
29
30 fakeAst.body[0].parent = fakeAst;
31
32 const enteredNodes = [];
33 const exitedNodes = [];
34
35 traverser.traverse(fakeAst, {
36 enter: node => enteredNodes.push(node),
37 leave: node => exitedNodes.push(node)
38 });
39
40 assert.deepStrictEqual(enteredNodes, [fakeAst, fakeAst.body[0], fakeAst.body[1], fakeAst.body[1].foo]);
41 assert.deepStrictEqual(exitedNodes, [fakeAst.body[0], fakeAst.body[1].foo, fakeAst.body[1], fakeAst]);
42 });
43
44 it("traverses AST as using 'visitorKeys' option if given", () => {
45 const traverser = new Traverser();
46 const fakeAst = {
47 type: "Program",
48 body: [
49 {
50 type: "ClassDeclaration",
51 id: {
52 type: "Identifier"
53 },
54 superClass: null,
55 body: {
56 type: "ClassBody",
57 body: []
58 },
59 experimentalDecorators: [
60 {
61 type: "Decorator",
62 expression: {}
63 }
64 ]
65 }
66 ]
67 };
68
69 fakeAst.body[0].parent = fakeAst;
70
71 const visited = [];
72
73 // with 'visitorKeys' option to traverse decorators.
74 traverser.traverse(fakeAst, {
75 enter: node => visited.push(node.type),
76 visitorKeys: Object.assign({}, Traverser.DEFAULT_VISITOR_KEYS, {
77 ClassDeclaration: Traverser.DEFAULT_VISITOR_KEYS.ClassDeclaration.concat(["experimentalDecorators"])
78 })
79 });
80 assert.deepStrictEqual(visited, ["Program", "ClassDeclaration", "Identifier", "ClassBody", "Decorator"]);
81 });
82 });