UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

83 lines (82 loc) 3.8 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 __()); }; })(); var __values = (this && this.__values)/* istanbul ignore next */ || function (o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; }; Object.defineProperty(exports, "__esModule", { value: true }); var typescript_1 = require("./../../typescript"); var errors = require("./../../errors"); var utils_1 = require("./../../utils"); var callBaseFill_1 = require("./../callBaseFill"); function AmbientableNode(Base) { return /** @class */ (function (_super) { __extends(class_1, _super); function class_1() { return _super !== null && _super.apply(this, arguments) || this; } class_1.prototype.hasDeclareKeyword = function () { return this.getDeclareKeyword() != null; }; class_1.prototype.getDeclareKeywordOrThrow = function () { return errors.throwIfNullOrUndefined(this.getDeclareKeyword(), "Expected to find a declare keyword."); }; class_1.prototype.getDeclareKeyword = function () { return this.getFirstModifierByKind(typescript_1.SyntaxKind.DeclareKeyword); }; class_1.prototype.isAmbient = function () { var isThisAmbient = (this.getCombinedModifierFlags() & typescript_1.ts.ModifierFlags.Ambient) === typescript_1.ts.ModifierFlags.Ambient; if (isThisAmbient || utils_1.TypeGuards.isInterfaceDeclaration(this) || utils_1.TypeGuards.isTypeAliasDeclaration(this)) return true; var topParent = this; try { for (var _a = __values(this.getAncestors()), _b = _a.next(); !_b.done; _b = _a.next()) { var parent = _b.value; topParent = parent; // store the top parent for later var modifierFlags = parent.getCombinedModifierFlags(); if (modifierFlags & typescript_1.ts.ModifierFlags.Ambient) return true; } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_b && !_b.done && (_c = _a.return)) _c.call(_a); } finally { if (e_1) throw e_1.error; } } return utils_1.TypeGuards.isSourceFile(topParent) && topParent.isDeclarationFile(); var e_1, _c; }; class_1.prototype.setHasDeclareKeyword = function (value) { // do nothing for these kind of nodes if (utils_1.TypeGuards.isInterfaceDeclaration(this) || utils_1.TypeGuards.isTypeAliasDeclaration(this)) return this; this.toggleModifier("declare", value); return this; }; class_1.prototype.fill = function (structure) { callBaseFill_1.callBaseFill(Base.prototype, this, structure); if (structure.hasDeclareKeyword != null) this.setHasDeclareKeyword(structure.hasDeclareKeyword); return this; }; return class_1; }(Base)); } exports.AmbientableNode = AmbientableNode;