UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for static analysis and code manipulation.

159 lines (158 loc) 6.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var manipulation_1 = require("../../../manipulation"); var errors = require("../../../errors"); var typescript_1 = require("../../../typescript"); var utils_1 = require("../../../utils"); var base_1 = require("../base"); var common_1 = require("../common"); var callBaseGetStructure_1 = require("../callBaseGetStructure"); var callBaseSet_1 = require("../callBaseSet"); exports.TypeParameterDeclarationBase = base_1.NamedNode(common_1.Node); var TypeParameterDeclaration = /** @class */ (function (_super) { tslib_1.__extends(TypeParameterDeclaration, _super); function TypeParameterDeclaration() { return _super !== null && _super.apply(this, arguments) || this; } /** * Gets the constraint of the type parameter. */ TypeParameterDeclaration.prototype.getConstraint = function () { return this._getNodeFromCompilerNodeIfExists(this.compilerNode.constraint); }; /** * Gets the constraint of the type parameter or throws if it doesn't exist. */ TypeParameterDeclaration.prototype.getConstraintOrThrow = function () { return errors.throwIfNullOrUndefined(this.getConstraint(), "Expected to find the type parameter's constraint."); }; /** * Sets the type parameter constraint. * @param text - Text to set as the constraint. */ TypeParameterDeclaration.prototype.setConstraint = function (text) { text = this.getParent()._getTextWithQueuedChildIndentation(text); if (utils_1.StringUtils.isNullOrWhitespace(text)) { this.removeConstraint(); return this; } var constraint = this.getConstraint(); if (constraint != null) { constraint.replaceWithText(text); return this; } var nameNode = this.getNameNode(); manipulation_1.insertIntoParentTextRange({ parent: this, insertPos: nameNode.getEnd(), newText: " extends " + text }); return this; }; /** * Removes the constraint type node. */ TypeParameterDeclaration.prototype.removeConstraint = function () { removeConstraintOrDefault(this.getConstraint(), typescript_1.SyntaxKind.ExtendsKeyword); return this; }; /** * Gets the default node of the type parameter. */ TypeParameterDeclaration.prototype.getDefault = function () { return this._getNodeFromCompilerNodeIfExists(this.compilerNode.default); }; /** * Gets the default node of the type parameter or throws if it doesn't exist. */ TypeParameterDeclaration.prototype.getDefaultOrThrow = function () { return errors.throwIfNullOrUndefined(this.getDefault(), "Expected to find the type parameter's default."); }; /** * Sets the type parameter default type node. * @param text - Text to set as the default type node. */ TypeParameterDeclaration.prototype.setDefault = function (text) { text = this.getParent()._getTextWithQueuedChildIndentation(text); if (utils_1.StringUtils.isNullOrWhitespace(text)) { this.removeDefault(); return this; } var defaultNode = this.getDefault(); if (defaultNode != null) { defaultNode.replaceWithText(text); return this; } var insertAfterNode = this.getConstraint() || this.getNameNode(); manipulation_1.insertIntoParentTextRange({ parent: this, insertPos: insertAfterNode.getEnd(), newText: " = " + text }); return this; }; /** * Removes the default type node. */ TypeParameterDeclaration.prototype.removeDefault = function () { removeConstraintOrDefault(this.getDefault(), typescript_1.SyntaxKind.EqualsToken); return this; }; /** * Removes this type parameter. */ TypeParameterDeclaration.prototype.remove = function () { var parentSyntaxList = this.getParentSyntaxListOrThrow(); var typeParameters = parentSyntaxList.getChildrenOfKind(typescript_1.SyntaxKind.TypeParameter); if (typeParameters.length === 1) removeAllTypeParameters(); else manipulation_1.removeCommaSeparatedChild(this); function removeAllTypeParameters() { var children = [ parentSyntaxList.getPreviousSiblingIfKindOrThrow(typescript_1.SyntaxKind.LessThanToken), parentSyntaxList, parentSyntaxList.getNextSiblingIfKindOrThrow(typescript_1.SyntaxKind.GreaterThanToken) ]; manipulation_1.removeChildren({ children: children }); } }; /** * Sets the node from a structure. * @param structure - Structure to set the node with. */ TypeParameterDeclaration.prototype.set = function (structure) { callBaseSet_1.callBaseSet(exports.TypeParameterDeclarationBase.prototype, this, structure); if (structure.constraint != null) this.setConstraint(structure.constraint); else if (structure.hasOwnProperty("constraint")) this.removeConstraint(); if (structure.default != null) this.setDefault(structure.default); else if (structure.hasOwnProperty("default")) this.removeDefault(); return this; }; /** * Gets the structure equivalent to this node. */ TypeParameterDeclaration.prototype.getStructure = function () { var constraintNode = this.getConstraint(); var defaultNode = this.getDefault(); return callBaseGetStructure_1.callBaseGetStructure(exports.TypeParameterDeclarationBase.prototype, this, { constraint: constraintNode != null ? constraintNode.getText() : undefined, default: defaultNode ? defaultNode.getText() : undefined }); }; return TypeParameterDeclaration; }(exports.TypeParameterDeclarationBase)); exports.TypeParameterDeclaration = TypeParameterDeclaration; function removeConstraintOrDefault(nodeToRemove, siblingKind) { if (nodeToRemove == null) return; manipulation_1.removeChildren({ children: [nodeToRemove.getPreviousSiblingIfKindOrThrow(siblingKind), nodeToRemove], removePrecedingSpaces: true }); }