UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

58 lines (56 loc) 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ts = require("typescript"); const TsSimpleAst_1 = require("./../../../TsSimpleAst"); const DefaultFileSystemHost_1 = require("./../../../DefaultFileSystemHost"); const defaultHost = new DefaultFileSystemHost_1.DefaultFileSystemHost(); const pastReadFile = defaultHost.readFile; const fileMap = new Map(); defaultHost.readFile = filePath => { // cache any file reads if (!fileMap.has(filePath)) fileMap.set(filePath, pastReadFile.call(defaultHost, filePath)); return fileMap.get(filePath); }; /** @internal */ function getInfoFromText(text, opts) { // tslint:disable-next-line:no-unnecessary-initializer -- tslint not realizing undefined is required const { isDefinitionFile = false, filePath = undefined, host = defaultHost, disableErrorCheck = false } = opts || {}; const tsSimpleAst = new TsSimpleAst_1.TsSimpleAst({ compilerOptions: { target: ts.ScriptTarget.ES2017 } }, host); const sourceFile = tsSimpleAst.addSourceFileFromText(filePath || (isDefinitionFile ? "testFile.d.ts" : "testFile.ts"), text); const firstChild = sourceFile.getChildSyntaxListOrThrow().getChildren()[0]; // disabled because the tests will run out of memory (I believe this is a ts compiler issue) /* if (!disableErrorCheck) ensureNoCompileErrorsInSourceFile(sourceFile); */ return { tsSimpleAst, sourceFile, firstChild }; } exports.getInfoFromText = getInfoFromText; function ensureNoCompileErrorsInSourceFile(sourceFile) { const diagnostics = sourceFile.getDiagnostics().filter(checkAllowDiagnostic); if (diagnostics.length === 0) return; console.log("SOURCE FILE"); console.log("==========="); console.log(sourceFile.getText()); console.log("==========="); for (const diagnostic of diagnostics) { console.log(diagnostic.getCode()); console.log(diagnostic.getMessageText()); } throw new Error("Compile error!"); } function checkAllowDiagnostic(diagnostic) { switch (diagnostic.getCode()) { case 7005: // no implicit any case 7025: // no implicit any case 2304: // cannot find name case 4020: // extends clause is using private name case 4019:// implements clause is using private name return false; default: return true; } } //# sourceMappingURL=getInfoFromText.js.map