ts-simple-ast
Version:
TypeScript compiler wrapper for static analysis and code manipulation.
88 lines (87 loc) • 4.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var errors = require("../../../errors");
var manipulation_1 = require("../../../manipulation");
var structurePrinters_1 = require("../../../structurePrinters");
var typescript_1 = require("../../../typescript");
var callBaseSet_1 = require("../callBaseSet");
var callBaseGetStructure_1 = require("../callBaseGetStructure");
function ImplementsClauseableNode(Base) {
return /** @class */ (function (_super) {
tslib_1.__extends(class_1, _super);
function class_1() {
return _super !== null && _super.apply(this, arguments) || this;
}
class_1.prototype.getImplements = function () {
var implementsClause = this.getHeritageClauseByKind(typescript_1.SyntaxKind.ImplementsKeyword);
return implementsClause == null ? [] : implementsClause.getTypeNodes();
};
class_1.prototype.addImplements = function (text) {
return this.insertImplements(this.getImplements().length, text);
};
class_1.prototype.insertImplements = function (index, texts) {
var originalImplements = this.getImplements();
var wasStringInput = typeof texts === "string";
if (typeof texts === "string") {
errors.throwIfWhitespaceOrNotString(texts, "texts");
texts = [texts];
}
else if (texts.length === 0)
return [];
var writer = this._getWriterWithQueuedChildIndentation();
var structurePrinter = new structurePrinters_1.CommaSeparatedStructuresPrinter(new structurePrinters_1.StringStructurePrinter());
structurePrinter.printText(writer, texts);
var heritageClauses = this.getHeritageClauses();
index = manipulation_1.verifyAndGetIndex(index, originalImplements.length);
if (originalImplements.length > 0) {
var implementsClause = this.getHeritageClauseByKindOrThrow(typescript_1.SyntaxKind.ImplementsKeyword);
manipulation_1.insertIntoCommaSeparatedNodes({
parent: implementsClause.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.SyntaxList),
currentNodes: originalImplements,
insertIndex: index,
newText: writer.toString()
});
}
else {
var openBraceToken = this.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.OpenBraceToken);
var openBraceStart = openBraceToken.getStart();
var isLastSpace = /\s/.test(this.getSourceFile().getFullText()[openBraceStart - 1]);
var insertText = "implements " + writer.toString() + " ";
if (!isLastSpace)
insertText = " " + insertText;
// assumes there can only be another extends heritage clause
manipulation_1.insertIntoParentTextRange({
parent: heritageClauses.length === 0 ? this : heritageClauses[0].getParentSyntaxListOrThrow(),
insertPos: openBraceStart,
newText: insertText
});
}
var newImplements = this.getImplements();
return wasStringInput ? newImplements[0] : manipulation_1.getNodesToReturn(newImplements, index, newImplements.length - originalImplements.length);
};
class_1.prototype.removeImplements = function (implementsNodeOrIndex) {
var implementsClause = this.getHeritageClauseByKind(typescript_1.SyntaxKind.ImplementsKeyword);
if (implementsClause == null)
throw new errors.InvalidOperationError("Cannot remove an implements when none exist.");
implementsClause.removeExpression(implementsNodeOrIndex);
return this;
};
class_1.prototype.set = function (structure) {
var _this = this;
callBaseSet_1.callBaseSet(Base.prototype, this, structure);
if (structure.implements != null) {
this.getImplements().forEach(function (expr) { return _this.removeImplements(expr); });
this.addImplements(structure.implements);
}
return this;
};
class_1.prototype.getStructure = function () {
return callBaseGetStructure_1.callBaseGetStructure(Base.prototype, this, {
implements: this.getImplements().map(function (node) { return node.getText(); })
});
};
return class_1;
}(Base));
}
exports.ImplementsClauseableNode = ImplementsClauseableNode;