github-pages-deploy-action/node_modules/tsutils/util/convert-ast.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-01-28 13:08:03 +08:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2020-03-07 11:45:40 +08:00
const ts = require("typescript");
const util_1 = require("./util");
2020-01-28 13:08:03 +08:00
function convertAst(sourceFile) {
2020-03-07 11:45:40 +08:00
const wrapped = {
2020-01-28 13:08:03 +08:00
node: sourceFile,
parent: undefined,
kind: ts.SyntaxKind.SourceFile,
children: [],
next: undefined,
skip: undefined,
};
2020-03-07 11:45:40 +08:00
const flat = [];
let current = wrapped;
let previous = current;
2020-01-28 13:08:03 +08:00
ts.forEachChild(sourceFile, function wrap(node) {
flat.push(node);
2020-03-07 11:45:40 +08:00
const parent = current;
2020-01-28 13:08:03 +08:00
previous.next = current = {
2020-03-07 11:45:40 +08:00
node,
parent,
2020-01-28 13:08:03 +08:00
kind: node.kind,
children: [],
next: undefined,
skip: undefined,
};
if (previous !== parent)
setSkip(previous, current);
previous = current;
parent.children.push(current);
if (util_1.isNodeKind(node.kind))
ts.forEachChild(node, wrap);
current = parent;
});
return {
2020-03-07 11:45:40 +08:00
wrapped,
flat,
2020-01-28 13:08:03 +08:00
};
}
exports.convertAst = convertAst;
function setSkip(node, skip) {
do {
node.skip = skip;
node = node.parent;
} while (node !== skip.parent);
}