fix: use a special longname for a non-default export in an ES2015 module

Previously, we used `exports.NAME`, which was both incorrect and confusing.
This commit is contained in:
Jeff Williams 2024-05-11 17:42:04 -07:00
parent 575f0dccc8
commit f7d5fa77b0
No known key found for this signature in database
4 changed files with 16 additions and 8 deletions

View File

@ -171,20 +171,19 @@ export function nodeToValue(node) {
case Syntax.ExportNamedDeclaration:
if (node.declaration) {
// like `var` in: export var foo = 'bar';
// we need a single value, so we use the first variable name
// Like the declaration in: `export const foo = 'bar';`
// We need a single value, so we use the first variable name.
if (node.declaration.declarations) {
str = `exports.${nodeToValue(node.declaration.declarations[0])}`;
str = `${LONGNAMES.MODULE_EXPORT}.${nodeToValue(node.declaration.declarations[0])}`;
} else {
str = `exports.${nodeToValue(node.declaration)}`;
str = `${LONGNAMES.MODULE_EXPORT}.${nodeToValue(node.declaration)}`;
}
}
// otherwise we'll use the ExportSpecifier nodes
break;
case Syntax.ExportSpecifier:
str = `exports.${nodeToValue(node.exported)}`;
str = `${LONGNAMES.MODULE_EXPORT}.${nodeToValue(node.exported)}`;
break;
case Syntax.ArrowFunctionExpression:

View File

@ -36,6 +36,8 @@ export const LONGNAMES = {
GLOBAL: '<global>',
/** Longname for the default export in an ES2015 module. */
MODULE_DEFAULT_EXPORT: '<moduleDefaultExport>',
/** Longname prefix for an export in an ES2015 module. */
MODULE_EXPORT: '<moduleExport>',
};
// Module namespace prefix.

View File

@ -196,6 +196,10 @@ describe('@jsdoc/core.name', () => {
it('has a MODULE_DEFAULT_EXPORT property', () => {
expect(name.LONGNAMES.MODULE_DEFAULT_EXPORT).toBeString();
});
it('has a MODULE_EXPORT property', () => {
expect(name.LONGNAMES.MODULE_EXPORT).toBeString();
});
});
// TODO: longnamesToTree tests

View File

@ -20,8 +20,11 @@ import escape from 'escape-string-regexp';
const PROTOTYPE_OWNER_REGEXP = /^(.+?)(\.prototype|#)$/;
const { LONGNAMES, SCOPE } = name;
const ESCAPED_MODULE_LONGNAMES =
escape(LONGNAMES.MODULE_DEFAULT_EXPORT) + '|' + escape('module.exports');
const ESCAPED_MODULE_LONGNAMES = [
escape(LONGNAMES.MODULE_DEFAULT_EXPORT),
escape(LONGNAMES.MODULE_EXPORT),
escape('module.exports'),
].join('|');
let currentModule = null;
// Modules inferred from the value of an `@alias` tag, like `@alias module:foo.bar`.