UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

226 lines (225 loc) 9.83 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 __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; }; 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 }; } }; }; Object.defineProperty(exports, "__esModule", { value: true }); var utils_1 = require("./../utils"); var Directory_1 = require("./../fileSystem/Directory"); /** * Cache for the directories. * @internal */ var DirectoryCache = /** @class */ (function () { function DirectoryCache(global) { this.global = global; this.directoriesByPath = new utils_1.KeyValueCache(); this.orphanDirs = utils_1.createHashSet(); } DirectoryCache.prototype.has = function (dirPath) { return this.directoriesByPath.has(dirPath); }; DirectoryCache.prototype.get = function (dirPath) { return this.directoriesByPath.get(dirPath); }; DirectoryCache.prototype.getOrphans = function () { return utils_1.ArrayUtils.from(this.orphanDirs.values()); }; DirectoryCache.prototype.getAll = function () { return utils_1.ArrayUtils.from(this.directoriesByPath.getValues()); }; DirectoryCache.prototype.getAllByDepth = function () { function addToDirLevels(dir) { var dirDepth = dir.getDepth(); /* istanbul ignore if */ if (depth > dirDepth) throw new Error("For some reason a subdirectory had a lower depth than the parent directory: " + dir.getPath()); var dirs = dirLevels.getOrCreate(dirDepth, function () { return []; }); dirs.push(dir); } var dirLevels, depth, _a, _b, dir, e_1_1, e_1, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: dirLevels = new utils_1.KeyValueCache(); depth = 0; this.getOrphans().forEach(addToDirLevels); depth = Math.min.apply(Math, __spread(utils_1.ArrayUtils.from(dirLevels.getKeys()))); _d.label = 1; case 1: if (!(dirLevels.getSize() > 0)) return [3 /*break*/, 10]; _d.label = 2; case 2: _d.trys.push([2, 7, 8, 9]); _a = __values(dirLevels.get(depth) || []), _b = _a.next(); _d.label = 3; case 3: if (!!_b.done) return [3 /*break*/, 6]; dir = _b.value; return [4 /*yield*/, dir]; case 4: _d.sent(); dir.getDirectories().forEach(addToDirLevels); _d.label = 5; case 5: _b = _a.next(); return [3 /*break*/, 3]; case 6: return [3 /*break*/, 9]; case 7: e_1_1 = _d.sent(); e_1 = { error: e_1_1 }; return [3 /*break*/, 9]; case 8: try { if (_b && !_b.done && (_c = _a.return)) _c.call(_a); } finally { if (e_1) throw e_1.error; } return [7 /*endfinally*/]; case 9: dirLevels.removeByKey(depth); depth++; return [3 /*break*/, 1]; case 10: return [2 /*return*/]; } }); }; DirectoryCache.prototype.remove = function (dirPath) { return this.directoriesByPath.removeByKey(dirPath); }; DirectoryCache.prototype.createOrAddIfNotExists = function (dirPath) { if (this.has(dirPath)) return this.get(dirPath); this.fillParentsOfDirPath(dirPath); return this.createDirectory(dirPath); }; DirectoryCache.prototype.createDirectory = function (path) { var newDirectory = new Directory_1.Directory(this.global, path); var parentDirPath = utils_1.FileUtils.getDirPath(path); var isRootDir = parentDirPath === path; try { for (var _a = __values(this.directoriesByPath.getValues()), _b = _a.next(); !_b.done; _b = _a.next()) { var childDir = _b.value; var childDirPath = childDir.getPath(); var childDirParentPath = utils_1.FileUtils.getDirPath(childDirPath); var isChildRootDir = childDirParentPath === childDirPath; if (!isChildRootDir && childDirParentPath === path) { newDirectory._addDirectory(childDir); this.orphanDirs.delete(childDir); } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_b && !_b.done && (_c = _a.return)) _c.call(_a); } finally { if (e_2) throw e_2.error; } } if (!isRootDir) { var parentDir = this.directoriesByPath.get(parentDirPath); if (parentDir != null) parentDir._addDirectory(newDirectory); } if (newDirectory.getParent() == null) this.orphanDirs.add(newDirectory); this.directoriesByPath.set(path, newDirectory); try { for (var _d = __values(this.orphanDirs.values()), _e = _d.next(); !_e.done; _e = _d.next()) { var orphanDir = _e.value; if (newDirectory.isAncestorOf(orphanDir)) this.fillParentsOfDirPath(orphanDir.getPath()); } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (_e && !_e.done && (_f = _d.return)) _f.call(_d); } finally { if (e_3) throw e_3.error; } } return newDirectory; var e_2, _c, e_3, _f; }; DirectoryCache.prototype.fillParentsOfDirPath = function (dirPath) { var passedDirPaths = []; var dir = dirPath; var parentDir = utils_1.FileUtils.getDirPath(dir); while (dir !== parentDir) { dir = parentDir; parentDir = utils_1.FileUtils.getDirPath(dir); if (this.directoriesByPath.has(dir)) { try { for (var passedDirPaths_1 = __values(passedDirPaths), passedDirPaths_1_1 = passedDirPaths_1.next(); !passedDirPaths_1_1.done; passedDirPaths_1_1 = passedDirPaths_1.next()) { var currentDirPath = passedDirPaths_1_1.value; this.createDirectory(currentDirPath); } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (passedDirPaths_1_1 && !passedDirPaths_1_1.done && (_a = passedDirPaths_1.return)) _a.call(passedDirPaths_1); } finally { if (e_4) throw e_4.error; } } break; } passedDirPaths.unshift(dir); } var e_4, _a; }; return DirectoryCache; }()); exports.DirectoryCache = DirectoryCache;