UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

84 lines (83 loc) 4.05 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 errors = require("./../../errors"); var callBaseFill_1 = require("./../callBaseFill"); function ExtendsClauseableNode(Base) { return /** @class */ (function (_super) { __extends(class_1, _super); function class_1() { return _super !== null && _super.apply(this, arguments) || this; } class_1.prototype.getExtends = function () { var extendsClause = this.getHeritageClauseByKind(ts.SyntaxKind.ExtendsKeyword); return extendsClause == null ? [] : extendsClause.getTypes(); }; class_1.prototype.addExtends = function (text) { return this.insertExtends(this.getExtends().length, text); }; class_1.prototype.insertExtends = 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 extendsTypes = this.getExtends(); index = manipulation_1.verifyAndGetIndex(index, extendsTypes.length); if (extendsTypes.length > 0) { var extendsClause = this.getHeritageClauseByKindOrThrow(ts.SyntaxKind.ExtendsKeyword); manipulation_1.insertIntoCommaSeparatedNodes({ parent: extendsClause.getFirstChildByKindOrThrow(ts.SyntaxKind.SyntaxList), currentNodes: extendsTypes, insertIndex: index, newTexts: texts }); return manipulation_1.getNodeOrNodesToReturn(this.getExtends(), index, length); } var openBraceToken = this.getFirstChildByKindOrThrow(ts.SyntaxKind.OpenBraceToken); var openBraceStart = openBraceToken.getStart(); var isLastSpace = /\s/.test(this.getSourceFile().getFullText()[openBraceStart - 1]); var 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); }; class_1.prototype.removeExtends = function (implementsNodeOrIndex) { var extendsClause = this.getHeritageClauseByKind(ts.SyntaxKind.ExtendsKeyword); if (extendsClause == null) throw new errors.InvalidOperationError("Cannot remove an extends when none exist."); extendsClause.removeExpression(implementsNodeOrIndex); return this; }; class_1.prototype.fill = function (structure) { callBaseFill_1.callBaseFill(Base.prototype, this, structure); if (structure.extends != null && structure.extends.length > 0) this.addExtends(structure.extends); return this; }; return class_1; }(Base)); } exports.ExtendsClauseableNode = ExtendsClauseableNode;