UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

104 lines (103 loc) 5.11 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 typescript_1 = require("./../../typescript"); var errors = require("./../../errors"); var utils_1 = require("./../../utils"); var callBaseFill_1 = require("./../callBaseFill"); function ExportableNode(Base) { return /** @class */ (function (_super) { __extends(class_1, _super); function class_1() { return _super !== null && _super.apply(this, arguments) || this; } class_1.prototype.hasExportKeyword = function () { return this.getExportKeyword() != null; }; class_1.prototype.getExportKeyword = function () { return this.getFirstModifierByKind(typescript_1.SyntaxKind.ExportKeyword); }; class_1.prototype.getExportKeywordOrThrow = function () { return errors.throwIfNullOrUndefined(this.getExportKeyword(), "Expected to find an export keyword."); }; class_1.prototype.hasDefaultKeyword = function () { return this.getDefaultKeyword() != null; }; class_1.prototype.getDefaultKeyword = function () { return this.getFirstModifierByKind(typescript_1.SyntaxKind.DefaultKeyword); }; class_1.prototype.getDefaultKeywordOrThrow = function () { return errors.throwIfNullOrUndefined(this.getDefaultKeyword(), "Expected to find a default keyword."); }; class_1.prototype.isExported = function () { return this.hasExportKeyword() || this.isDefaultExport(); }; class_1.prototype.isDefaultExport = function () { if (this.hasDefaultKeyword()) return true; if (!utils_1.TypeGuards.isSourceFile(this.getParentOrThrow())) return false; var thisSymbol = this.getSymbol(); var defaultExportSymbol = this.getSourceFile().getDefaultExportSymbol(); if (defaultExportSymbol == null || thisSymbol == null) return false; if (thisSymbol.equals(defaultExportSymbol)) return true; var aliasedSymbol = defaultExportSymbol.getAliasedSymbol(); return thisSymbol.equals(aliasedSymbol); }; class_1.prototype.isNamedExport = function () { var parentNode = this.getParentOrThrow(); return utils_1.TypeGuards.isSourceFile(parentNode) && this.hasExportKeyword() && !this.hasDefaultKeyword(); }; class_1.prototype.setIsDefaultExport = function (value) { if (value === this.isDefaultExport()) return this; if (value && !utils_1.TypeGuards.isSourceFile(this.getParentOrThrow())) throw new errors.InvalidOperationError("The parent must be a source file in order to set this node as a default export."); // remove any existing default export var sourceFile = this.getSourceFile(); var fileDefaultExportSymbol = sourceFile.getDefaultExportSymbol(); if (fileDefaultExportSymbol != null) sourceFile.removeDefaultExport(fileDefaultExportSymbol); if (!value) return this; // set this node as the one to default export if (utils_1.TypeGuards.isAmbientableNode(this) && utils_1.TypeGuards.hasName(this) && this.isAmbient()) { var parentSyntaxList = this.getFirstAncestorByKindOrThrow(typescript_1.SyntaxKind.SyntaxList); parentSyntaxList.insertChildText(this.getChildIndex() + 1, "export default " + this.getName() + ";"); } else { this.addModifier("export"); this.addModifier("default"); } return this; }; class_1.prototype.setIsExported = function (value) { // remove the default keyword if it exists if (utils_1.TypeGuards.isSourceFile(this.getParentOrThrow())) this.toggleModifier("default", false); this.toggleModifier("export", value); return this; }; class_1.prototype.fill = function (structure) { callBaseFill_1.callBaseFill(Base.prototype, this, structure); if (structure.isExported != null) this.setIsExported(structure.isExported); if (structure.isDefaultExport != null) this.setIsDefaultExport(structure.isDefaultExport); return this; }; return class_1; }(Base)); } exports.ExportableNode = ExportableNode;