ts-simple-ast
Version:
TypeScript compiler wrapper for static analysis and code manipulation.
100 lines (99 loc) • 3.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var utils_1 = require("../../../utils");
var TextSpan_1 = require("./TextSpan");
/**
* Document span.
*/
var DocumentSpan = /** @class */ (function () {
/**
* @private
*/
function DocumentSpan(context, compilerObject) {
var _this = this;
this._context = context;
this._compilerObject = compilerObject;
// store this node so that it's start doesn't go out of date because of manipulation (though the text span may)
// Note: this will cause the source file to transitively hold a reference to this node and so it won't be released
// from the WeakMap until a manipulation happens on the source file.
this._sourceFile = this._context.compilerFactory.getSourceFileFromCacheFromFilePath(this.compilerObject.fileName);
this._sourceFile._doActionPreNextModification(function () { return _this.getNode(); });
}
Object.defineProperty(DocumentSpan.prototype, "compilerObject", {
/**
* Gets the compiler object.
*/
get: function () {
return this._compilerObject;
},
enumerable: true,
configurable: true
});
/**
* Gets the source file this reference is in.
*/
DocumentSpan.prototype.getSourceFile = function () {
return this._sourceFile;
};
/**
* Gets the text span.
*/
DocumentSpan.prototype.getTextSpan = function () {
return new TextSpan_1.TextSpan(this.compilerObject.textSpan);
};
/**
* Gets the node at the start of the text span.
*/
DocumentSpan.prototype.getNode = function () {
var textSpan = this.getTextSpan();
var sourceFile = this.getSourceFile();
var start = textSpan.getStart();
var width = textSpan.getEnd();
return findBestMatchingNode();
function findBestMatchingNode() {
// more relaxed getDescendantAtStartWithWidth because the position may be within a string literal
var bestNode;
sourceFile._context.compilerFactory.forgetNodesCreatedInBlock(function (remember) {
var foundNode;
var nextNode = sourceFile;
while (nextNode != null) {
if (foundNode == null)
bestNode = nextNode;
if (nextNode.getStart() === start && nextNode.getWidth() === width)
bestNode = foundNode = nextNode;
else if (foundNode != null)
break; // no need to keep looking
nextNode = nextNode.getChildAtPos(start);
}
if (bestNode != null)
remember(bestNode);
});
return bestNode;
}
};
/**
* Gets the original text span if the span represents a location that was remapped.
*/
DocumentSpan.prototype.getOriginalTextSpan = function () {
var originalTextSpan = this.compilerObject.originalTextSpan;
return originalTextSpan == null ? undefined : new TextSpan_1.TextSpan(originalTextSpan);
};
/**
* Gets the original file name if the span represents a location that was remapped.
*/
DocumentSpan.prototype.getOriginalFileName = function () {
return this.compilerObject.originalFileName;
};
tslib_1.__decorate([
utils_1.Memoize
], DocumentSpan.prototype, "getTextSpan", null);
tslib_1.__decorate([
utils_1.Memoize
], DocumentSpan.prototype, "getNode", null);
tslib_1.__decorate([
utils_1.Memoize
], DocumentSpan.prototype, "getOriginalTextSpan", null);
return DocumentSpan;
}());
exports.DocumentSpan = DocumentSpan;