UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

73 lines (71 loc) 3.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ts = require("typescript"); const errors = require("./../../errors"); const callBaseFill_1 = require("./../callBaseFill"); function ExportableNode(Base) { return class extends Base { hasExportKeyword() { return this.getExportKeyword() != null; } getExportKeyword() { return this.getFirstModifierByKind(ts.SyntaxKind.ExportKeyword); } hasDefaultKeyword() { return this.getDefaultKeyword() != null; } getDefaultKeyword() { return this.getFirstModifierByKind(ts.SyntaxKind.DefaultKeyword); } 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 parentNode.isSourceFile() && this.hasExportKeyword() && !this.hasDefaultKeyword(); } setIsDefaultExport(value) { if (value === this.isDefaultExport()) return this; if (value && !this.getParentOrThrow().isSourceFile()) 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 (this.getParentOrThrow().isSourceFile()) 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