@ts-ast-parser/core
Version:
Reflects a simplified version of the TypeScript AST for generating documentation
79 lines • 2.55 kB
JavaScript
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 namespace import declaration.
* For example: `import * as foo from './foo.js'`
*/
export class NamespaceImportNode {
constructor(node, context) {
Object.defineProperty(this, "_node", {
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._context = context;
}
getTSNode() {
return this._node;
}
getContext() {
return this._context;
}
getName() {
const identifier = (this._node.importClause?.namedBindings).name;
return identifier.escapedText ?? '';
}
getNodeType() {
return RootNodeType.Import;
}
getKind() {
return ImportKind.Namespace;
}
getImportPath() {
return this._node.moduleSpecifier.text ?? '';
}
getOriginalPath() {
const identifier = (this._node.importClause?.namedBindings).name;
const importPath = this.getImportPath();
const compilerOptions = this._context.getCommandLine().options;
return matchesTsConfigPath(importPath, compilerOptions)
? getOriginalImportPath(identifier, this._context)
: importPath;
}
isTypeOnly() {
return !!this._node.importClause?.isTypeOnly;
}
isBareModuleSpecifier() {
return isBareModuleSpecifier(this.getImportPath());
}
/**
* Serializes the reflected node
*
* @returns The reflected node as a serializable object
*/
serialize() {
const originalPath = this.getOriginalPath();
const tmpl = {
name: this.getName(),
kind: this.getKind(),
importPath: this.getImportPath(),
};
if (originalPath !== tmpl.importPath) {
tmpl.originalPath = originalPath;
}
tryAddProperty(tmpl, 'typeOnly', this.isTypeOnly());
tryAddProperty(tmpl, 'bareModuleSpecifier', this.isBareModuleSpecifier());
return tmpl;
}
}
//# sourceMappingURL=namespace-import-node.js.map