UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

76 lines (75 loc) 3.13 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 errors = require("./../../errors"); var callBaseFill_1 = require("./../callBaseFill"); var manipulation_1 = require("./../../manipulation"); var typescript_1 = require("./../../typescript"); function AwaitableNode(Base) { return /** @class */ (function (_super) { __extends(class_1, _super); function class_1() { return _super !== null && _super.apply(this, arguments) || this; } class_1.prototype.isAwaited = function () { return this.compilerNode.awaitModifier != null; }; class_1.prototype.getAwaitKeyword = function () { var awaitModifier = this.compilerNode.awaitModifier; return this.getNodeFromCompilerNodeIfExists(awaitModifier); }; class_1.prototype.getAwaitKeywordOrThrow = function () { return errors.throwIfNullOrUndefined(this.getAwaitKeyword(), "Expected to find an await token."); }; class_1.prototype.setIsAwaited = function (value) { var awaitModifier = this.getAwaitKeyword(); var isSet = awaitModifier != null; if (isSet === value) return this; if (awaitModifier == null) { var info = getAwaitInsertInfo(this); manipulation_1.insertIntoParent({ insertPos: info.pos, childIndex: info.childIndex, insertItemsCount: 1, parent: this, newText: " await" }); } else { manipulation_1.removeChildren({ children: [awaitModifier], removePrecedingSpaces: true }); } return this; }; class_1.prototype.fill = function (structure) { callBaseFill_1.callBaseFill(Base.prototype, this, structure); if (structure.isAwaited != null) this.setIsAwaited(structure.isAwaited); return this; }; return class_1; }(Base)); } exports.AwaitableNode = AwaitableNode; function getAwaitInsertInfo(node) { if (node.getKind() === typescript_1.SyntaxKind.ForOfStatement) { var forKeyword = node.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.ForKeyword); return { pos: forKeyword.getEnd(), childIndex: forKeyword.getChildIndex() + 1 }; } throw new errors.NotImplementedError("Expected a for of statement node."); }