UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for static analysis and code manipulation.

101 lines (100 loc) 4.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var errors = require("../../../errors"); var manipulation_1 = require("../../../manipulation"); var typescript_1 = require("../../../typescript"); var utils_1 = require("../../../utils"); var callBaseSet_1 = require("../callBaseSet"); var callBaseGetStructure_1 = require("../callBaseGetStructure"); function TypedNode(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.getTypeNode = function () { return this._getNodeFromCompilerNodeIfExists(this.compilerNode.type); }; class_1.prototype.getTypeNodeOrThrow = function () { return errors.throwIfNullOrUndefined(this.getTypeNode(), "Expected to find a type node."); }; class_1.prototype.setType = function (textOrWriterFunction) { var text = utils_1.getTextFromStringOrWriter(this._getWriterWithQueuedChildIndentation(), textOrWriterFunction); if (utils_1.StringUtils.isNullOrWhitespace(text)) return this.removeType(); var typeNode = this.getTypeNode(); if (typeNode != null && typeNode.getText() === text) return this; // remove previous type var separatorSyntaxKind = getSeparatorSyntaxKindForNode(this); var separatorNode = this.getFirstChildByKind(separatorSyntaxKind); var insertPos; var newText; if (separatorNode == null) { insertPos = getInsertPosWhenNoType(this); newText = (separatorSyntaxKind === typescript_1.SyntaxKind.EqualsToken ? " = " : ": ") + text; } else { insertPos = typeNode.getStart(); newText = text; } // insert new type manipulation_1.insertIntoParentTextRange({ parent: this, insertPos: insertPos, newText: newText, replacing: { textLength: typeNode == null ? 0 : typeNode.getWidth() } }); return this; function getInsertPosWhenNoType(node) { var identifier = node.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.Identifier); var nextSibling = identifier.getNextSibling(); var insertAfterNode = isQuestionOrExclamation(nextSibling) ? nextSibling : identifier; return insertAfterNode.getEnd(); } function isQuestionOrExclamation(node) { if (node == null) return false; var kind = node.getKind(); return kind === typescript_1.SyntaxKind.QuestionToken || kind === typescript_1.SyntaxKind.ExclamationToken; } }; class_1.prototype.set = function (structure) { callBaseSet_1.callBaseSet(Base.prototype, this, structure); if (structure.type != null) this.setType(structure.type); else if (structure.hasOwnProperty("type")) this.removeType(); return this; }; class_1.prototype.removeType = function () { if (this.getKind() === typescript_1.SyntaxKind.TypeAliasDeclaration) throw new errors.NotSupportedError("Cannot remove the type of a type alias. Use " + "setType" + " instead."); var typeNode = this.getTypeNode(); if (typeNode == null) return this; var separatorToken = typeNode.getPreviousSiblingIfKindOrThrow(getSeparatorSyntaxKindForNode(this)); manipulation_1.removeChildren({ children: [separatorToken, typeNode], removePrecedingSpaces: true }); return this; }; class_1.prototype.getStructure = function () { var typeNode = this.getTypeNode(); return callBaseGetStructure_1.callBaseGetStructure(Base.prototype, this, { type: typeNode ? typeNode.getText() : undefined }); }; return class_1; }(Base)); } exports.TypedNode = TypedNode; function getSeparatorSyntaxKindForNode(node) { switch (node.getKind()) { case typescript_1.SyntaxKind.TypeAliasDeclaration: return typescript_1.SyntaxKind.EqualsToken; default: return typescript_1.SyntaxKind.ColonToken; } }