UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for static analysis and code manipulation.

87 lines (86 loc) 4.47 kB
"use strict"; 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 ExtendsClauseableNode(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.getExtends = function () { var extendsClause = this.getHeritageClauseByKind(typescript_1.SyntaxKind.ExtendsKeyword); return extendsClause == null ? [] : extendsClause.getTypeNodes(); }; class_1.prototype.addExtends = function (text) { return this.insertExtends(this.getExtends().length, text); }; class_1.prototype.insertExtends = function (index, texts) { var originalExtends = this.getExtends(); 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); index = manipulation_1.verifyAndGetIndex(index, originalExtends.length); if (originalExtends.length > 0) { var extendsClause = this.getHeritageClauseByKindOrThrow(typescript_1.SyntaxKind.ExtendsKeyword); manipulation_1.insertIntoCommaSeparatedNodes({ parent: extendsClause.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.SyntaxList), currentNodes: originalExtends, 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 = "extends " + writer.toString() + " "; if (!isLastSpace) insertText = " " + insertText; manipulation_1.insertIntoParentTextRange({ parent: this, insertPos: openBraceStart, newText: insertText }); } var newExtends = this.getExtends(); return wasStringInput ? newExtends[index] : manipulation_1.getNodesToReturn(newExtends, index, newExtends.length - originalExtends.length); }; class_1.prototype.removeExtends = function (implementsNodeOrIndex) { var extendsClause = this.getHeritageClauseByKind(typescript_1.SyntaxKind.ExtendsKeyword); if (extendsClause == null) throw new errors.InvalidOperationError("Cannot remove an extends when none exist."); extendsClause.removeExpression(implementsNodeOrIndex); return this; }; class_1.prototype.set = function (structure) { var _this = this; callBaseSet_1.callBaseSet(Base.prototype, this, structure); if (structure.extends != null) { this.getExtends().forEach(function (e) { return _this.removeExtends(e); }); this.addExtends(structure.extends); } return this; }; class_1.prototype.getStructure = function () { return callBaseGetStructure_1.callBaseGetStructure(Base.prototype, this, { extends: this.getExtends().map(function (e) { return e.getText(); }) }); }; return class_1; }(Base)); } exports.ExtendsClauseableNode = ExtendsClauseableNode;