Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore class names of default-exported modules #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cincodenada
Copy link

@cincodenada cincodenada commented Dec 9, 2021

This is an attempt to resolve the downstream issue I just filed, jsdoc2md/jsdoc-to-markdown#270 - see that issue for more details, but the short is, if we're parsing the following file:

/** module ExampleClass */

export default class ExampleClass {
  constructor() {}
}

Then we'll currently generate entries for the class and the constructor under module.exports because of how ES6 rewrites things internally. This uses the module name for the exported class and constructor instead, which is more in line with what end-users expect. There's an existing workaround with @alias for jsdoc-to-markdown, but it seems to me like just that - a suboptimal workaround.

That said, I'm not sure that this is the correct level to implement this remapping at - I don't know if this library is used widely outside of jsdoc-to-markdown, and if this would qualify as a breaking change. I looked into fixing it up in jsdoc-to-markdown, but since the results of jsdoc-parse are basically piped directly into dmd, it looked like it would require either elaborate template conditions, or a remapping similar to this, added to a pipeline that currently doesn't do any transformation of the result from jsdoc-parse.

But if this is a reasonable approach that's just not in the right place, it's a pretty simple transform, so it wouldn't be difficult to add into the pipeline in jsdoc-to-markdown.

Also the conditions I used for rewriting seem pretty conservative to me, but I'm not super familiar with the problem space here, so I don't know if there's a possibility of affecting other use cases.

If we're parsing the following file:
```
/** module ExampleClass */

export default class ExampleClass {
  constructor() {}
}
```

Then we'll currently generate entries for the class and the constructor
under `module.exports` because of how ES6 rewrites things internally.

This uses the module name for the exported class and constructor
instead, which is friendlier behavior.
@75lb
Copy link
Member

75lb commented Dec 13, 2021

Hi, thanks for looking into this.. I'll have a detailed look when I get time - the first thing I'll check is whether we're working around a jsdoc issue or not.. If i remember right, functions assigned directly to module.exports are not documented in the usual consistent manner by jsdoc, unless that changed..

@75lb
Copy link
Member

75lb commented Sep 1, 2024

Hi, thanks for the PR - I gave it a try in the testbed with dozens of example test cases..

The output looks ok (as you intended I assume) except in cases where the @module tag has no value. E.g.

/**
* This is the module description
* @module
*/

/**
* the main function description
* @returns {object | string} this return has several types
*/
module.exports = function (one, two) {}

In this case you get output something like, where the filename (0-src.js) is used as the function name:

## 0-src
This is the module description

### 0-src(one, two) ⇒ <code>object</code> \| <code>string</code> ⏏
the main function description

**Kind**: Exported function  
**Returns**: <code>object</code> \| <code>string</code> - this return has several types  

What should we do in cases like this? Keep module.exports as it is currently?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants