ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
58 lines (56 loc) • 2.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
const manipulation_1 = require("./../../manipulation");
const errors = require("./../../errors");
const callBaseFill_1 = require("./../callBaseFill");
function ExtendsClauseableNode(Base) {
return class extends Base {
getExtends() {
const extendsClause = this.getHeritageClauses().find(c => c.compilerNode.token === ts.SyntaxKind.ExtendsKeyword);
return extendsClause == null ? [] : extendsClause.getTypes();
}
addExtends(text) {
return this.insertExtends(this.getExtends().length, text);
}
insertExtends(index, texts) {
const length = texts instanceof Array ? texts.length : 0;
if (typeof texts === "string") {
errors.throwIfNotStringOrWhitespace(texts, "texts");
texts = [texts];
}
else if (texts.length === 0) {
return [];
}
const extendsTypes = this.getExtends();
index = manipulation_1.verifyAndGetIndex(index, extendsTypes.length);
if (extendsTypes.length > 0) {
manipulation_1.insertIntoCommaSeparatedNodes({ parent: this, currentNodes: extendsTypes, insertIndex: index, newTexts: texts });
return manipulation_1.getNodeOrNodesToReturn(this.getExtends(), index, length);
}
const openBraceToken = this.getFirstChildByKindOrThrow(ts.SyntaxKind.OpenBraceToken);
const openBraceStart = openBraceToken.getStart();
const isLastSpace = /\s/.test(this.getSourceFile().getFullText()[openBraceStart - 1]);
let insertText = `extends ${texts.join(", ")} `;
if (!isLastSpace)
insertText = " " + insertText;
manipulation_1.insertIntoCreatableSyntaxList({
parent: this,
insertPos: openBraceStart,
newText: insertText,
syntaxList: undefined,
childIndex: index,
insertItemsCount: length
});
return manipulation_1.getNodeOrNodesToReturn(this.getExtends(), index, length);
}
fill(structure) {
callBaseFill_1.callBaseFill(Base.prototype, this, structure);
if (structure.extends != null && structure.extends.length > 0)
this.addExtends(structure.extends);
return this;
}
};
}
exports.ExtendsClauseableNode = ExtendsClauseableNode;
//# sourceMappingURL=ExtendsClauseableNode.js.map