UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for static analysis and code manipulation.

140 lines (139 loc) 5.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var manipulation_1 = require("../../../manipulation"); var typescript_1 = require("../../../typescript"); var utils_1 = require("../../../utils"); var base_1 = require("../base"); var callBaseSet_1 = require("../callBaseSet"); var Node_1 = require("../common/Node"); var callBaseGetStructure_1 = require("../callBaseGetStructure"); exports.ParameterDeclarationBase = base_1.QuestionTokenableNode(base_1.DecoratableNode(base_1.ScopeableNode(base_1.ReadonlyableNode(base_1.ModifierableNode(base_1.TypedNode(base_1.InitializerExpressionableNode(base_1.DeclarationNamedNode(Node_1.Node)))))))); var ParameterDeclaration = /** @class */ (function (_super) { tslib_1.__extends(ParameterDeclaration, _super); function ParameterDeclaration() { return _super !== null && _super.apply(this, arguments) || this; } /** * Gets the dot dot dot token (...) if it exists, for a rest parameter. */ ParameterDeclaration.prototype.getDotDotDotToken = function () { return this._getNodeFromCompilerNodeIfExists(this.compilerNode.dotDotDotToken); }; /** * Gets if it's a rest parameter. */ ParameterDeclaration.prototype.isRestParameter = function () { return this.compilerNode.dotDotDotToken != null; }; /** * Gets if this is a property with a scope or readonly keyword (found in class constructors). */ ParameterDeclaration.prototype.isParameterProperty = function () { return this.getScope() != null || this.isReadonly(); }; /** * Sets if it's a rest parameter. * @param value - Sets if it's a rest parameter or not. */ ParameterDeclaration.prototype.setIsRestParameter = function (value) { if (this.isRestParameter() === value) return this; if (value) { addParensIfNecessary(this); manipulation_1.insertIntoParentTextRange({ insertPos: this.getNameNodeOrThrow().getStart(), parent: this, newText: "..." }); } else manipulation_1.removeChildren({ children: [this.getDotDotDotToken()] }); return this; }; /** * Gets if it's optional. */ ParameterDeclaration.prototype.isOptional = function () { return this.compilerNode.questionToken != null || this.isRestParameter() || this.hasInitializer(); }; /** * Remove this parameter. */ ParameterDeclaration.prototype.remove = function () { manipulation_1.removeCommaSeparatedChild(this); }; /** * Sets the node from a structure. * @param structure - Structure to set the node with. */ ParameterDeclaration.prototype.set = function (structure) { callBaseSet_1.callBaseSet(exports.ParameterDeclarationBase.prototype, this, structure); if (structure.isRestParameter != null) this.setIsRestParameter(structure.isRestParameter); return this; }; /** * Gets the structure equivalent to this node. */ ParameterDeclaration.prototype.getStructure = function () { return callBaseGetStructure_1.callBaseGetStructure(exports.ParameterDeclarationBase.prototype, this, { isRestParameter: this.isRestParameter() }); }; // ------ Methods to override to add parens ------ /** * Sets if this node has a question token. * @param value - If it should have a question token or not. */ ParameterDeclaration.prototype.setHasQuestionToken = function (value) { if (value) addParensIfNecessary(this); _super.prototype.setHasQuestionToken.call(this, value); return this; }; /** * Sets the initializer. * @param text - Text or writer function to set for the initializer. */ ParameterDeclaration.prototype.setInitializer = function (textOrWriterFunction) { addParensIfNecessary(this); _super.prototype.setInitializer.call(this, textOrWriterFunction); return this; }; /** * Sets the type. * @param textOrWriterFunction - Text or writer function to set the type with. */ ParameterDeclaration.prototype.setType = function (textOrWriterFunction) { addParensIfNecessary(this); _super.prototype.setType.call(this, textOrWriterFunction); return this; }; return ParameterDeclaration; }(exports.ParameterDeclarationBase)); exports.ParameterDeclaration = ParameterDeclaration; function addParensIfNecessary(parameter) { var parent = parameter.getParentOrThrow(); if (isParameterWithoutParens()) addParens(); function isParameterWithoutParens() { return utils_1.TypeGuards.isArrowFunction(parent) && parent.compilerNode.parameters.length === 1 && parameter.getParentSyntaxListOrThrow().getPreviousSiblingIfKind(typescript_1.SyntaxKind.OpenParenToken) == null; } function addParens() { var paramText = parameter.getText(); manipulation_1.insertIntoParentTextRange({ parent: parent, insertPos: parameter.getStart(), newText: "(" + paramText + ")", replacing: { textLength: paramText.length }, customMappings: function (newParent) { return [{ currentNode: parameter, newNode: newParent.parameters[0] }]; } }); } }