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
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
10 function convertAst(sourceFile) {
\r
14 kind: ts.SyntaxKind.SourceFile,
\r
20 let current = wrapped;
\r
21 function collectChildren(node) {
\r
22 current.children.push({
\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
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
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
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
64 // done on this node
\r
65 if (stack.length === 1)
\r
67 // remove index from stack and go back to parent
\r
69 current = current.parent;
\r
78 exports.convertAst = convertAst;
\r
79 //# sourceMappingURL=convert-ast.js.map