ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
105 lines (104 loc) • 4.07 kB
JavaScript
;
var __extends = (this && this.__extends)/* istanbul ignore next */ || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
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 });
/* barrel:ignore */
var ts = require("typescript");
var errors = require("./../errors");
var utils_1 = require("./../utils");
/**
* Extension of KeyValueCache that allows for "forget points."
*/
var ForgetfulNodeCache = /** @class */ (function (_super) {
__extends(ForgetfulNodeCache, _super);
function ForgetfulNodeCache() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.forgetStack = [];
return _this;
}
ForgetfulNodeCache.prototype.getOrCreate = function (key, createFunc) {
var _this = this;
return _super.prototype.getOrCreate.call(this, key, function () {
var node = createFunc();
if (_this.forgetStack.length > 0)
_this.forgetStack[_this.forgetStack.length - 1].add(node);
return node;
});
};
ForgetfulNodeCache.prototype.setForgetPoint = function () {
this.forgetStack.push(utils_1.createHashSet());
};
ForgetfulNodeCache.prototype.forgetLastPoint = function () {
var nodes = this.forgetStack.pop();
if (nodes != null)
this.forgetNodes(nodes.values());
};
ForgetfulNodeCache.prototype.rememberNode = function (node) {
if (node.wasForgotten())
throw new errors.InvalidOperationError("Cannot remember a node that was removed or forgotten.");
var wasInForgetStack = false;
try {
for (var _a = __values(this.forgetStack), _b = _a.next(); !_b.done; _b = _a.next()) {
var stackItem = _b.value;
if (stackItem.delete(node)) {
wasInForgetStack = true;
break;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_1) throw e_1.error; }
}
if (wasInForgetStack)
this.rememberParentOfNode(node);
return wasInForgetStack;
var e_1, _c;
};
ForgetfulNodeCache.prototype.rememberParentOfNode = function (node) {
var parent = node.getParentSyntaxList() || node.getParent();
if (parent != null)
this.rememberNode(parent);
};
ForgetfulNodeCache.prototype.forgetNodes = function (nodes) {
try {
for (var nodes_1 = __values(nodes), nodes_1_1 = nodes_1.next(); !nodes_1_1.done; nodes_1_1 = nodes_1.next()) {
var node = nodes_1_1.value;
if (node.wasForgotten() || node.getKind() === ts.SyntaxKind.SourceFile)
continue;
node.forgetOnlyThis();
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (nodes_1_1 && !nodes_1_1.done && (_a = nodes_1.return)) _a.call(nodes_1);
}
finally { if (e_2) throw e_2.error; }
}
var e_2, _a;
};
return ForgetfulNodeCache;
}(utils_1.KeyValueCache));
exports.ForgetfulNodeCache = ForgetfulNodeCache;