Skip to content

Commit

Permalink
fix: use a special longname for a non-default export in an ES2015 module
Browse files Browse the repository at this point in the history
Previously, we used `exports.NAME`, which was both incorrect and confusing.
  • Loading branch information
hegemonic committed May 12, 2024
1 parent 575f0dc commit f7d5fa7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
11 changes: 5 additions & 6 deletions packages/jsdoc-ast/lib/ast-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions packages/jsdoc-core/lib/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions packages/jsdoc-core/test/specs/lib/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions packages/jsdoc-parse/lib/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down

0 comments on commit f7d5fa7

Please sign in to comment.