ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
44 lines (42 loc) • 1.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
const errors = require("./errors");
const compiler_1 = require("./compiler");
const GlobalContainer_1 = require("./GlobalContainer");
const DefaultFileSystemHost_1 = require("./DefaultFileSystemHost");
/**
* Creates a wrapped node from a compiler node.
* @param node - Node to create a wrapped node from.
* @param sourceFile - Optional source file of the node to help improve performance.
*/
function createWrappedNode(node, sourceFile) {
let wrappedSourceFile;
if (sourceFile == null)
wrappedSourceFile = getSourceFileFromNode(node);
else if (sourceFile instanceof compiler_1.SourceFile)
wrappedSourceFile = sourceFile;
else
wrappedSourceFile = getWrappedSourceFile(sourceFile);
return wrappedSourceFile.global.compilerFactory.getNodeFromCompilerNode(node, wrappedSourceFile);
}
exports.createWrappedNode = createWrappedNode;
function getSourceFileFromNode(node) {
if (node.kind === ts.SyntaxKind.SourceFile)
return getWrappedSourceFile(node);
if (node.parent == null)
throw new errors.InvalidOperationError("Please ensure the node was created from a source file with 'setParentNodes' set to 'true'.");
let parent = node;
while (parent.parent != null)
parent = parent.parent;
if (parent.kind !== ts.SyntaxKind.SourceFile)
throw new errors.NotImplementedError("For some reason the top parent was not a source file.");
return getWrappedSourceFile(parent);
}
function getWrappedSourceFile(sourceFile) {
return getGlobalContainer().compilerFactory.getSourceFile(sourceFile);
}
function getGlobalContainer() {
return new GlobalContainer_1.GlobalContainer(new DefaultFileSystemHost_1.DefaultFileSystemHost(), {}, false);
}
//# sourceMappingURL=createWrappedNode.js.map