UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

61 lines (59 loc) 2.59 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 InitializerExpressionableNode(Base) { return class extends Base { isInitializerExpressionableNode() { return true; } hasInitializer() { return this.compilerNode.initializer != null; } getInitializer() { return this.compilerNode.initializer == null ? undefined : this.global.compilerFactory.getNodeFromCompilerNode(this.compilerNode.initializer, this.sourceFile); } getInitializerOrThrow() { return errors.throwIfNullOrUndefined(this.getInitializer(), "Expected to find an initializer."); } removeInitializer() { const initializer = this.getInitializer(); if (initializer == null) return this; const previousSibling = initializer.getPreviousSibling(); /* istanbul ignore if */ if (previousSibling == null || previousSibling.getKind() !== ts.SyntaxKind.FirstAssignment) throw errors.getNotImplementedForSyntaxKindError(this.getKind()); manipulation_1.removeChildren({ children: [previousSibling, initializer], removePrecedingSpaces: true }); return this; } setInitializer(text) { errors.throwIfNotStringOrWhitespace(text, "text"); if (this.hasInitializer()) this.removeInitializer(); const semiColonToken = this.getLastChildIfKind(ts.SyntaxKind.SemicolonToken); manipulation_1.insertIntoParent({ insertPos: semiColonToken != null ? semiColonToken.getPos() : this.getEnd(), childIndex: semiColonToken != null ? semiColonToken.getChildIndex() : this.getChildCount(), insertItemsCount: 2, parent: this, newText: ` = ${text}` }); return this; } fill(structure) { callBaseFill_1.callBaseFill(Base.prototype, this, structure); if (structure.initializer != null) this.setInitializer(structure.initializer); return this; } }; } exports.InitializerExpressionableNode = InitializerExpressionableNode; //# sourceMappingURL=InitializerExpressionableNode.js.map