--- /dev/null
+"use strict";\r
+Object.defineProperty(exports, "__esModule", { value: true });\r
+exports.convertAst = void 0;\r
+const ts = require("typescript");\r
+const util_1 = require("./util");\r
+/**\r
+ * Takes a `ts.SourceFile` and creates data structures that are easier (or more performant) to traverse.\r
+ * Note that there is only a performance gain if you can reuse these structures. It's not recommended for one-time AST walks.\r
+ */\r
+function convertAst(sourceFile) {\r
+ const wrapped = {\r
+ node: sourceFile,\r
+ parent: undefined,\r
+ kind: ts.SyntaxKind.SourceFile,\r
+ children: [],\r
+ next: undefined,\r
+ skip: undefined,\r
+ };\r
+ const flat = [];\r
+ let current = wrapped;\r
+ function collectChildren(node) {\r
+ current.children.push({\r
+ node,\r
+ parent: current,\r
+ kind: node.kind,\r
+ children: [],\r
+ next: undefined,\r
+ skip: undefined,\r
+ });\r
+ }\r
+ const stack = [];\r
+ while (true) {\r
+ if (current.children.length === 0) {\r
+ ts.forEachChild(current.node, collectChildren);\r
+ if (current.children.length === 0) {\r
+ current = current.parent; // nothing to do here, go back to parent\r
+ }\r
+ else {\r
+ // recurse into first child\r
+ const firstChild = current.children[0];\r
+ current.next = firstChild;\r
+ flat.push(firstChild.node);\r
+ if (util_1.isNodeKind(firstChild.kind))\r
+ current = firstChild;\r
+ stack.push(1); // set index in stack so we know where to continue processing children\r
+ }\r
+ }\r
+ else {\r
+ const index = stack[stack.length - 1];\r
+ if (index < current.children.length) { // handles 2nd child to the last\r
+ const currentChild = current.children[index];\r
+ flat.push(currentChild.node);\r
+ let previous = current.children[index - 1];\r
+ while (previous.children.length !== 0) {\r
+ previous.skip = currentChild;\r
+ previous = previous.children[previous.children.length - 1];\r
+ }\r
+ previous.skip = previous.next = currentChild;\r
+ ++stack[stack.length - 1];\r
+ if (util_1.isNodeKind(currentChild.kind))\r
+ current = currentChild; // recurse into child\r
+ }\r
+ else {\r
+ // done on this node\r
+ if (stack.length === 1)\r
+ break;\r
+ // remove index from stack and go back to parent\r
+ stack.pop();\r
+ current = current.parent;\r
+ }\r
+ }\r
+ }\r
+ return {\r
+ wrapped,\r
+ flat,\r
+ };\r
+}\r
+exports.convertAst = convertAst;\r
+//# sourceMappingURL=convert-ast.js.map
\ No newline at end of file