12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.convertAst = void 0;
- const ts = require("typescript");
- const util_1 = require("./util");
- function convertAst(sourceFile) {
- const wrapped = {
- node: sourceFile,
- parent: undefined,
- kind: ts.SyntaxKind.SourceFile,
- children: [],
- next: undefined,
- skip: undefined,
- };
- const flat = [];
- let current = wrapped;
- function collectChildren(node) {
- current.children.push({
- node,
- parent: current,
- kind: node.kind,
- children: [],
- next: undefined,
- skip: undefined,
- });
- }
- const stack = [];
- while (true) {
- if (current.children.length === 0) {
- ts.forEachChild(current.node, collectChildren);
- if (current.children.length === 0) {
- current = current.parent;
- }
- else {
-
- const firstChild = current.children[0];
- current.next = firstChild;
- flat.push(firstChild.node);
- if (util_1.isNodeKind(firstChild.kind))
- current = firstChild;
- stack.push(1);
- }
- }
- else {
- const index = stack[stack.length - 1];
- if (index < current.children.length) {
- const currentChild = current.children[index];
- flat.push(currentChild.node);
- let previous = current.children[index - 1];
- while (previous.children.length !== 0) {
- previous.skip = currentChild;
- previous = previous.children[previous.children.length - 1];
- }
- previous.skip = previous.next = currentChild;
- ++stack[stack.length - 1];
- if (util_1.isNodeKind(currentChild.kind))
- current = currentChild;
- }
- else {
-
- if (stack.length === 1)
- break;
-
- stack.pop();
- current = current.parent;
- }
- }
- }
- return {
- wrapped,
- flat,
- };
- }
- exports.convertAst = convertAst;
|