UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

194 lines (193 loc) 9.06 kB
"use strict"; var __generator = (this && this.__generator)/* istanbul ignore next */ || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [0, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __values = (this && this.__values)/* istanbul ignore next */ || function (o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; }; var __read = (this && this.__read)/* istanbul ignore next */ || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spread = (this && this.__spread)/* istanbul ignore next */ || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); var compilerApi = require("typescript"); var errors = require("./../../errors"); var compiler_1 = require("./../../compiler"); var utils_1 = require("./../../utils"); function getTsConfigParseResult(tsConfigFilePath, fileSystem) { tsConfigFilePath = utils_1.FileUtils.getStandardizedAbsolutePath(fileSystem, tsConfigFilePath); errors.throwIfFileNotExists(fileSystem, tsConfigFilePath); var parseResult = compilerApi.parseConfigFileTextToJson(tsConfigFilePath, fileSystem.readFileSync(tsConfigFilePath)); if (parseResult.error != null) throw new Error(parseResult.error.messageText.toString()); return parseResult; } exports.getTsConfigParseResult = getTsConfigParseResult; function getCompilerOptionsFromTsConfigParseResult(tsConfigFilePath, fileSystem, parseResult) { tsConfigFilePath = utils_1.FileUtils.getStandardizedAbsolutePath(fileSystem, tsConfigFilePath); var settings = compilerApi.convertCompilerOptionsFromJson(parseResult.config.compilerOptions, utils_1.FileUtils.getDirPath(tsConfigFilePath), tsConfigFilePath); return { options: settings.options, errors: (settings.errors || []).map(function (e) { return new compiler_1.Diagnostic(undefined, e); }) }; } exports.getCompilerOptionsFromTsConfigParseResult = getCompilerOptionsFromTsConfigParseResult; function getFilePathsFromTsConfigParseResult(tsConfigFilePath, fileSystem, parseResult, compilerOptions) { tsConfigFilePath = utils_1.FileUtils.getStandardizedAbsolutePath(fileSystem, tsConfigFilePath); var currentDir = fileSystem.getCurrentDirectory(); var host = { useCaseSensitiveFileNames: true, readDirectory: function (rootDir, extensions, excludes, includes) { return tsMatchFiles(rootDir, extensions, excludes || [], includes, false, currentDir, undefined, function (path) { return getFileSystemEntries(path, fileSystem); }); }, fileExists: function (path) { return fileSystem.fileExistsSync(path); }, readFile: function (path) { return fileSystem.readFileSync(path); } }; var files = utils_1.createHashSet(); var tsConfigDir = utils_1.FileUtils.getDirPath(tsConfigFilePath); try { for (var _a = __values(getRootDirs()), _b = _a.next(); !_b.done; _b = _a.next()) { var rootDir = _b.value; try { for (var _c = __values(getFilesFromDir(utils_1.FileUtils.getStandardizedAbsolutePath(fileSystem, rootDir, tsConfigDir))), _d = _c.next(); !_d.done; _d = _c.next()) { var filePath = _d.value; files.add(filePath); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_d && !_d.done && (_e = _c.return)) _e.call(_c); } finally { if (e_1) throw e_1.error; } } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_b && !_b.done && (_f = _a.return)) _f.call(_a); } finally { if (e_2) throw e_2.error; } } return utils_1.ArrayUtils.from(files.values()); function getRootDirs() { var result = []; if (typeof compilerOptions.rootDir === "string") result.push(compilerOptions.rootDir); if (compilerOptions.rootDirs != null) result.push.apply(result, __spread(compilerOptions.rootDirs)); // use the tsconfig directory if no rootDir or rootDirs is specified if (result.length === 0) result.push(tsConfigDir); return result; } function getFilesFromDir(dirPath) { var _a, _b, filePath, e_3_1, e_3, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: _d.trys.push([0, 5, 6, 7]); _a = __values(compilerApi.parseJsonConfigFileContent(parseResult.config, host, dirPath, compilerOptions, undefined).fileNames), _b = _a.next(); _d.label = 1; case 1: if (!!_b.done) return [3 /*break*/, 4]; filePath = _b.value; return [4 /*yield*/, utils_1.FileUtils.getStandardizedAbsolutePath(fileSystem, filePath)]; case 2: _d.sent(); _d.label = 3; case 3: _b = _a.next(); return [3 /*break*/, 1]; case 4: return [3 /*break*/, 7]; case 5: e_3_1 = _d.sent(); e_3 = { error: e_3_1 }; return [3 /*break*/, 7]; case 6: try { if (_b && !_b.done && (_c = _a.return)) _c.call(_a); } finally { if (e_3) throw e_3.error; } return [7 /*endfinally*/]; case 7: return [2 /*return*/]; } }); } var e_2, _f, e_1, _e; } exports.getFilePathsFromTsConfigParseResult = getFilePathsFromTsConfigParseResult; // todo: move this somewhere common /* tslint:disable:align */ function tsMatchFiles(path, extensions, excludes, includes, useCaseSensitiveFileNames, currentDirectory, depth, getEntries) { return compilerApi.matchFiles.apply(this, arguments); } function getFileSystemEntries(path, fileSystem) { var entries = fileSystem.readDirSync(path); var files = []; var directories = []; try { for (var entries_1 = __values(entries), entries_1_1 = entries_1.next(); !entries_1_1.done; entries_1_1 = entries_1.next()) { var entry = entries_1_1.value; if (fileSystem.fileExistsSync(entry)) files.push(entry); else directories.push(entry); } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (entries_1_1 && !entries_1_1.done && (_a = entries_1.return)) _a.call(entries_1); } finally { if (e_4) throw e_4.error; } } return { files: files, directories: directories }; var e_4, _a; }