UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

60 lines (58 loc) 2.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ts = require("typescript"); const manipulation_1 = require("./../../manipulation"); const callBaseFill_1 = require("./../callBaseFill"); const errors = require("./../../errors"); function ImplementsClauseableNode(Base) { return class extends Base { getImplements(heritageClauses = this.getHeritageClauses()) { const implementsClause = heritageClauses.find(c => c.compilerNode.token === ts.SyntaxKind.ImplementsKeyword); return implementsClause == null ? [] : implementsClause.getTypes(); } addImplements(text) { return this.insertImplements(this.getImplements().length, text); } insertImplements(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 heritageClauses = this.getHeritageClauses(); const implementsTypes = this.getImplements(heritageClauses); index = manipulation_1.verifyAndGetIndex(index, implementsTypes.length); if (implementsTypes.length > 0) { manipulation_1.insertIntoCommaSeparatedNodes({ parent: this, currentNodes: implementsTypes, insertIndex: index, newTexts: texts }); return manipulation_1.getNodeOrNodesToReturn(this.getImplements(), index, length); } const openBraceToken = this.getFirstChildByKindOrThrow(ts.SyntaxKind.OpenBraceToken); const openBraceStart = openBraceToken.getStart(); const isLastSpace = /\s/.test(this.getSourceFile().getFullText()[openBraceStart - 1]); let insertText = `implements ${texts.join(", ")} `; if (!isLastSpace) insertText = " " + insertText; // assumes there can only be another extends heritage clause manipulation_1.insertIntoCreatableSyntaxList({ parent: this, insertPos: openBraceStart, newText: insertText, syntaxList: heritageClauses.length === 0 ? undefined : heritageClauses[0].getParentSyntaxListOrThrow(), childIndex: 1, insertItemsCount: 1 }); return manipulation_1.getNodeOrNodesToReturn(this.getImplements(), index, length); } fill(structure) { callBaseFill_1.callBaseFill(Base.prototype, this, structure); if (structure.implements != null && structure.implements.length > 0) this.addImplements(structure.implements); return this; } }; } exports.ImplementsClauseableNode = ImplementsClauseableNode; //# sourceMappingURL=ImplementsClauseableNode.js.map