.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tsutils / util / convert-ast.js
1 "use strict";\r
2 Object.defineProperty(exports, "__esModule", { value: true });\r
3 exports.convertAst = void 0;\r
4 const ts = require("typescript");\r
5 const util_1 = require("./util");\r
6 /**\r
7  * Takes a `ts.SourceFile` and creates data structures that are easier (or more performant) to traverse.\r
8  * Note that there is only a performance gain if you can reuse these structures. It's not recommended for one-time AST walks.\r
9  */\r
10 function convertAst(sourceFile) {\r
11     const wrapped = {\r
12         node: sourceFile,\r
13         parent: undefined,\r
14         kind: ts.SyntaxKind.SourceFile,\r
15         children: [],\r
16         next: undefined,\r
17         skip: undefined,\r
18     };\r
19     const flat = [];\r
20     let current = wrapped;\r
21     function collectChildren(node) {\r
22         current.children.push({\r
23             node,\r
24             parent: current,\r
25             kind: node.kind,\r
26             children: [],\r
27             next: undefined,\r
28             skip: undefined,\r
29         });\r
30     }\r
31     const stack = [];\r
32     while (true) {\r
33         if (current.children.length === 0) {\r
34             ts.forEachChild(current.node, collectChildren);\r
35             if (current.children.length === 0) {\r
36                 current = current.parent; // nothing to do here, go back to parent\r
37             }\r
38             else {\r
39                 // recurse into first child\r
40                 const firstChild = current.children[0];\r
41                 current.next = firstChild;\r
42                 flat.push(firstChild.node);\r
43                 if (util_1.isNodeKind(firstChild.kind))\r
44                     current = firstChild;\r
45                 stack.push(1); // set index in stack so we know where to continue processing children\r
46             }\r
47         }\r
48         else {\r
49             const index = stack[stack.length - 1];\r
50             if (index < current.children.length) { // handles 2nd child to the last\r
51                 const currentChild = current.children[index];\r
52                 flat.push(currentChild.node);\r
53                 let previous = current.children[index - 1];\r
54                 while (previous.children.length !== 0) {\r
55                     previous.skip = currentChild;\r
56                     previous = previous.children[previous.children.length - 1];\r
57                 }\r
58                 previous.skip = previous.next = currentChild;\r
59                 ++stack[stack.length - 1];\r
60                 if (util_1.isNodeKind(currentChild.kind))\r
61                     current = currentChild; // recurse into child\r
62             }\r
63             else {\r
64                 // done on this node\r
65                 if (stack.length === 1)\r
66                     break;\r
67                 // remove index from stack and go back to parent\r
68                 stack.pop();\r
69                 current = current.parent;\r
70             }\r
71         }\r
72     }\r
73     return {\r
74         wrapped,\r
75         flat,\r
76     };\r
77 }\r
78 exports.convertAst = convertAst;\r
79 //# sourceMappingURL=convert-ast.js.map