UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for static analysis and code manipulation.

69 lines (68 loc) 3.18 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 QuestionTokenableNode(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.hasQuestionToken = function () { return this.compilerNode.questionToken != null; }; class_1.prototype.getQuestionTokenNode = function () { return this._getNodeFromCompilerNodeIfExists(this.compilerNode.questionToken); }; class_1.prototype.getQuestionTokenNodeOrThrow = function () { return errors.throwIfNullOrUndefined(this.getQuestionTokenNode(), "Expected to find a question token."); }; class_1.prototype.setHasQuestionToken = function (value) { var questionTokenNode = this.getQuestionTokenNode(); var hasQuestionToken = questionTokenNode != null; if (value === hasQuestionToken) return this; if (value) { if (utils_1.TypeGuards.isExclamationTokenableNode(this)) this.setHasExclamationToken(false); manipulation_1.insertIntoParentTextRange({ insertPos: getInsertPos.call(this), parent: this, newText: "?" }); } else manipulation_1.removeChildren({ children: [questionTokenNode] }); return this; function getInsertPos() { if (utils_1.TypeGuards.hasName(this)) return this.getNameNode().getEnd(); var colonNode = this.getFirstChildByKind(typescript_1.SyntaxKind.ColonToken); if (colonNode != null) return colonNode.getStart(); var semicolonToken = this.getLastChildByKind(typescript_1.SyntaxKind.SemicolonToken); if (semicolonToken != null) return semicolonToken.getStart(); return this.getEnd(); } }; class_1.prototype.set = function (structure) { callBaseSet_1.callBaseSet(Base.prototype, this, structure); if (structure.hasQuestionToken != null) this.setHasQuestionToken(structure.hasQuestionToken); return this; }; class_1.prototype.getStructure = function () { return callBaseGetStructure_1.callBaseGetStructure(Base.prototype, this, { hasQuestionToken: this.hasQuestionToken() }); }; return class_1; }(Base)); } exports.QuestionTokenableNode = QuestionTokenableNode;