UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

71 lines (69 loc) 2.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ts = require("typescript"); const errors = require("./../../errors"); const callBaseFill_1 = require("./../callBaseFill"); const manipulation_1 = require("./../../manipulation"); function GeneratorableNode(Base) { return class extends Base { isGenerator() { return this.compilerNode.asteriskToken != null; } getAsteriskToken() { const asteriskToken = this.compilerNode.asteriskToken; return asteriskToken == null ? undefined : this.global.compilerFactory.getNodeFromCompilerNode(asteriskToken, this.sourceFile); } getAsteriskTokenOrThrow() { return errors.throwIfNullOrUndefined(this.getAsteriskToken(), "Expected to find an asterisk token."); } setIsGenerator(value) { const asteriskToken = this.getAsteriskToken(); const isSet = asteriskToken != null; if (isSet === value) return this; if (asteriskToken == null) { const info = getAsteriskInsertInfo(this); manipulation_1.insertIntoParent({ insertPos: info.pos, childIndex: info.childIndex, insertItemsCount: 1, parent: this, newText: "*" }); } else { manipulation_1.removeChildrenWithFormatting({ children: [asteriskToken], getSiblingFormatting: () => manipulation_1.FormattingKind.Space }); } return this; } fill(structure) { callBaseFill_1.callBaseFill(Base.prototype, this, structure); if (structure.isGenerator != null) this.setIsGenerator(structure.isGenerator); return this; } }; } exports.GeneratorableNode = GeneratorableNode; function getAsteriskInsertInfo(node) { if (node.getKind() === ts.SyntaxKind.FunctionDeclaration) { const functionKeyword = node.getFirstChildByKindOrThrow(ts.SyntaxKind.FunctionKeyword); return { pos: functionKeyword.getEnd(), childIndex: functionKeyword.getChildIndex() + 1 }; } const namedNode = node; /* istanbul ignore if */ if (namedNode.getName == null) throw new errors.NotImplementedError("Expected a name node for a non-function declaration."); const identifier = namedNode.getNameIdentifier(); return { pos: identifier.getStart(), childIndex: identifier.getChildIndex() }; } //# sourceMappingURL=GeneratorableNode.js.map