UNPKG

@ts-ast-parser/core

Version:

Reflects a simplified version of the TypeScript AST for generating documentation

91 lines 2.9 kB
import { getOriginalImportPath, isBareModuleSpecifier, matchesTsConfigPath } from '../utils/import.js'; import { tryAddProperty } from '../utils/try-add-property.js'; import { ImportKind } from '../models/import.js'; import { RootNodeType } from '../models/node.js'; /** * Represents the reflected node of a named import declaration. * For example: `import { x } from 'y'` */ export class NamedImportNode { constructor(node, element, context) { Object.defineProperty(this, "_node", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "_element", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "_context", { enumerable: true, configurable: true, writable: true, value: void 0 }); this._node = node; this._element = element; this._context = context; } getTSNode() { return this._node; } getContext() { return this._context; } getName() { return this._element.name.escapedText ?? ''; } getReferenceName() { return this._element.propertyName?.escapedText ?? this.getName(); } getNodeType() { return RootNodeType.Import; } getKind() { return ImportKind.Named; } getImportPath() { return this._node.moduleSpecifier.text ?? ''; } getOriginalPath() { const importPath = this.getImportPath(); const compilerOptions = this._context.getCommandLine().options; return matchesTsConfigPath(importPath, compilerOptions) ? getOriginalImportPath(this._element.name, this._context) : importPath; } isTypeOnly() { return !!this._node.importClause?.isTypeOnly || this._element.isTypeOnly; } isBareModuleSpecifier() { return isBareModuleSpecifier(this.getImportPath()); } /** * Serializes the reflected node * * @returns The reflected node as a serializable object */ serialize() { const originalPath = this.getOriginalPath(); const referenceName = this.getReferenceName(); const tmpl = { name: this.getName(), kind: this.getKind(), importPath: this.getImportPath(), }; if (referenceName !== tmpl.name) { tmpl.referenceName = referenceName; } if (originalPath !== tmpl.importPath) { tmpl.originalPath = originalPath; } tryAddProperty(tmpl, 'typeOnly', this.isTypeOnly()); tryAddProperty(tmpl, 'bareModuleSpecifier', this.isBareModuleSpecifier()); return tmpl; } } //# sourceMappingURL=named-import-node.js.map