UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

80 lines (78 loc) 3.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ts = require("typescript"); const errors = require("./../../errors"); const utils_1 = require("./../../utils"); const callBaseFill_1 = require("./../callBaseFill"); function ExportableNode(Base) { return class extends Base { hasExportKeyword() { return this.getExportKeyword() != null; } getExportKeyword() { return this.getFirstModifierByKind(ts.SyntaxKind.ExportKeyword); } getExportKeywordOrThrow() { return errors.throwIfNullOrUndefined(this.getExportKeyword(), "Expected to find an export keyword."); } hasDefaultKeyword() { return this.getDefaultKeyword() != null; } getDefaultKeyword() { return this.getFirstModifierByKind(ts.SyntaxKind.DefaultKeyword); } getDefaultKeywordOrThrow() { return errors.throwIfNullOrUndefined(this.getDefaultKeyword(), "Expected to find a default keyword."); } isDefaultExport() { if (this.hasDefaultKeyword()) return true; const thisSymbol = this.getSymbol(); const defaultExportSymbol = this.getSourceFile().getDefaultExportSymbol(); if (defaultExportSymbol == null || thisSymbol == null) return false; if (thisSymbol.equals(defaultExportSymbol)) return true; const aliasedSymbol = defaultExportSymbol.getAliasedSymbol(); return thisSymbol.equals(aliasedSymbol); } isNamedExport() { const parentNode = this.getParentOrThrow(); return utils_1.TypeGuards.isSourceFile(parentNode) && this.hasExportKeyword() && !this.hasDefaultKeyword(); } setIsDefaultExport(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 const sourceFile = this.getSourceFile(); const fileDefaultExportSymbol = sourceFile.getDefaultExportSymbol(); if (fileDefaultExportSymbol != null) sourceFile.removeDefaultExport(fileDefaultExportSymbol); // set this node as the one to default export if (value) { this.addModifier("export"); this.addModifier("default"); } return this; } setIsExported(value) { // remove the default export if it is one no matter what if (utils_1.TypeGuards.isSourceFile(this.getParentOrThrow())) this.setIsDefaultExport(false); this.toggleModifier("export", value); return this; } fill(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; } }; } exports.ExportableNode = ExportableNode; //# sourceMappingURL=ExportableNode.js.map