UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

84 lines (83 loc) 3.54 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 callBaseFill_1 = require("./../callBaseFill"); var manipulation_1 = require("./../../manipulation"); function GeneratorableNode(Base) { return /** @class */ (function (_super) { __extends(class_1, _super); function class_1() { return _super !== null && _super.apply(this, arguments) || this; } class_1.prototype.isGenerator = function () { return this.compilerNode.asteriskToken != null; }; class_1.prototype.getAsteriskToken = function () { var asteriskToken = this.compilerNode.asteriskToken; return asteriskToken == null ? undefined : this.global.compilerFactory.getNodeFromCompilerNode(asteriskToken, this.sourceFile); }; class_1.prototype.getAsteriskTokenOrThrow = function () { return errors.throwIfNullOrUndefined(this.getAsteriskToken(), "Expected to find an asterisk token."); }; class_1.prototype.setIsGenerator = function (value) { var asteriskToken = this.getAsteriskToken(); var isSet = asteriskToken != null; if (isSet === value) return this; if (asteriskToken == null) { var info = getAsteriskInsertInfo(this); manipulation_1.insertIntoParent({ insertPos: info.pos, childIndex: info.childIndex, insertItemsCount: 1, parent: this, newText: "*" }); } else { manipulation_1.removeChildrenWithFormatting({ children: [asteriskToken], getSiblingFormatting: function () { return manipulation_1.FormattingKind.Space; } }); } return this; }; class_1.prototype.fill = function (structure) { callBaseFill_1.callBaseFill(Base.prototype, this, structure); if (structure.isGenerator != null) this.setIsGenerator(structure.isGenerator); return this; }; return class_1; }(Base)); } exports.GeneratorableNode = GeneratorableNode; function getAsteriskInsertInfo(node) { if (node.getKind() === ts.SyntaxKind.FunctionDeclaration) { var functionKeyword = node.getFirstChildByKindOrThrow(ts.SyntaxKind.FunctionKeyword); return { pos: functionKeyword.getEnd(), childIndex: functionKeyword.getChildIndex() + 1 }; } var namedNode = node; /* istanbul ignore if */ if (namedNode.getName == null) throw new errors.NotImplementedError("Expected a name node for a non-function declaration."); var identifier = namedNode.getNameNode(); return { pos: identifier.getStart(), childIndex: identifier.getChildIndex() }; }