UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

86 lines (85 loc) 4.36 kB
"use strict"; 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 __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var ts = require("typescript"); var manipulation_1 = require("./../../manipulation"); var callBaseFill_1 = require("./../callBaseFill"); var errors = require("./../../errors"); function ImplementsClauseableNode(Base) { return /** @class */ (function (_super) { __extends(class_1, _super); function class_1() { return _super !== null && _super.apply(this, arguments) || this; } class_1.prototype.getImplements = function () { var implementsClause = this.getHeritageClauseByKind(ts.SyntaxKind.ImplementsKeyword); return implementsClause == null ? [] : implementsClause.getTypes(); }; class_1.prototype.addImplements = function (text) { return this.insertImplements(this.getImplements().length, text); }; class_1.prototype.insertImplements = function (index, texts) { var length = texts instanceof Array ? texts.length : 0; if (typeof texts === "string") { errors.throwIfNotStringOrWhitespace(texts, "texts"); texts = [texts]; } else if (texts.length === 0) { return []; } var heritageClauses = this.getHeritageClauses(); var implementsTypes = this.getImplements(); index = manipulation_1.verifyAndGetIndex(index, implementsTypes.length); if (implementsTypes.length > 0) { var implementsClause = this.getHeritageClauseByKindOrThrow(ts.SyntaxKind.ImplementsKeyword); manipulation_1.insertIntoCommaSeparatedNodes({ parent: implementsClause.getFirstChildByKindOrThrow(ts.SyntaxKind.SyntaxList), currentNodes: implementsTypes, insertIndex: index, newTexts: texts }); return manipulation_1.getNodeOrNodesToReturn(this.getImplements(), index, length); } var openBraceToken = this.getFirstChildByKindOrThrow(ts.SyntaxKind.OpenBraceToken); var openBraceStart = openBraceToken.getStart(); var isLastSpace = /\s/.test(this.getSourceFile().getFullText()[openBraceStart - 1]); var 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); }; class_1.prototype.removeImplements = function (implementsNodeOrIndex) { var implementsClause = this.getHeritageClauseByKind(ts.SyntaxKind.ImplementsKeyword); if (implementsClause == null) throw new errors.InvalidOperationError("Cannot remove an implements when none exist."); implementsClause.removeExpression(implementsNodeOrIndex); return this; }; class_1.prototype.fill = function (structure) { callBaseFill_1.callBaseFill(Base.prototype, this, structure); if (structure.implements != null && structure.implements.length > 0) this.addImplements(structure.implements); return this; }; return class_1; }(Base)); } exports.ImplementsClauseableNode = ImplementsClauseableNode;