UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

67 lines (66 loc) 2.89 kB
"use strict"; var __extends = (this && this.__extends)/* istanbul ignore next */ || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var ts = require("typescript"); var errors = require("./../../../errors"); var manipulation_1 = require("./../../../manipulation"); var utils_1 = require("./../../../utils"); function NameableNode(Base) { return /** @class */ (function (_super) { __extends(class_1, _super); function class_1() { return _super !== null && _super.apply(this, arguments) || this; } class_1.prototype.getNameNode = function () { var nameNode = this.compilerNode.name; if (nameNode == null) return undefined; return this.global.compilerFactory.getNodeFromCompilerNode(nameNode, this.sourceFile); }; class_1.prototype.getNameNodeOrThrow = function () { return errors.throwIfNullOrUndefined(this.getNameNode(), "Expected to have a name node."); }; class_1.prototype.getName = function () { var identifier = this.getNameNode(); return identifier == null ? undefined : identifier.getText(); }; class_1.prototype.getNameOrThrow = function () { return errors.throwIfNullOrUndefined(this.getName(), "Expected to have a name."); }; class_1.prototype.rename = function (newName) { if (newName === this.getName()) return this; var nameNode = this.getNameNode(); if (utils_1.StringUtils.isNullOrWhitespace(newName)) { if (nameNode == null) return this; manipulation_1.removeChildren({ children: [nameNode], removePrecedingSpaces: true }); return this; } if (nameNode == null) { var openParenToken = this.getFirstChildByKindOrThrow(ts.SyntaxKind.OpenParenToken); manipulation_1.insertIntoParent({ childIndex: openParenToken.getChildIndex(), insertItemsCount: 1, insertPos: openParenToken.getStart(), newText: " " + newName, parent: this }); } else nameNode.rename(newName); return this; }; return class_1; }(Base)); } exports.NameableNode = NameableNode;