@jsdocs-io/extractor
Version:
The API extractor for npm packages powering jsdocs.io
33 lines (32 loc) • 1.63 kB
JavaScript
import { SyntaxKind } from "ts-morph";
import { isNamespace } from "./declaration-type-guards.js";
import { isExportedDeclarations } from "./is-exported-declarations.js";
import { isHidden } from "./is-hidden.js";
import { isShorthandAmbientModule } from "./is-shorthand-ambient-module.js";
export function exportEqualsDeclarations(containerName, container) {
// Shorthand ambient modules have no body and thus no declarations.
if (isShorthandAmbientModule(container))
return [];
// Get the identifier from the export equals assignment (e.g., `export = foo`).
const exportIdentifier = container
.getExportAssignment((assignment) => assignment.isExportEquals())
?.getLastChildByKind(SyntaxKind.Identifier);
if (!exportIdentifier)
return [];
// Get the declarations linked to the exported identifier.
const exportName = exportIdentifier.getText();
const exportEqualsDeclarations = [];
for (const declaration of exportIdentifier.getDefinitionNodes()) {
if (!isExportedDeclarations(declaration) || isHidden(declaration))
continue;
if (isNamespace(declaration)) {
// Ignore namespaces since `exportedDeclarations()` already extracts
// the inner declarations of an export equals namespace as
// non-namespaced declarations belonging to the parent container.
// See snapshot for `export-equals-function-and-namespace.test.ts`.
continue;
}
exportEqualsDeclarations.push({ containerName, exportName, declaration });
}
return exportEqualsDeclarations;
}