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

Add exported functions option to require-jsdoc rule #262

Merged
merged 17 commits into from
Jun 19, 2019

Conversation

Extersky
Copy link
Contributor

This PR adds support for configuring require-jsdoc rule to require JSDoc comments for exported function bodies only. There are options for configuring the export checks for CommonJS and ES6 exports.
Fixes #192

@brettz9 brettz9 force-pushed the jsdocexports branch 2 times, most recently from 0b64389 to 322307f Compare May 24, 2019 22:36
@Extersky
Copy link
Contributor Author

There are at least following limitations:

  1. Scopes and branches are not supported. This may not be an issue, but may require significant refactoring if desired to be added later
  2. The performance would likely improve significantly if a code file wasn't parsed again for each processed node

@brettz9
Copy link
Collaborator

brettz9 commented May 25, 2019

I'm not sure what "branches" are in this context (unless it refers to blocks within a scope?), but export isn't allowed anywhere but in the main module scope, so IIUC, I think that is not an issue. (Was forgetting this is for Node modules too, not just ESM.)

From my eyeballing, it looks like this will be quite useful for many projects...

@brettz9
Copy link
Collaborator

brettz9 commented May 25, 2019

I've amended my comment, as I was forgetting this is for Node modules , not ESM. But speaking of which, how about amending to have an optional publicFunctionsOnly check for ESM exports? I see it does...

@Extersky
Copy link
Contributor Author

With branches I meant to refer conditional branching, such as statically analyzable if blocks, which are possible in CommonJS. One such rare case would be a polyfill module where a shim would only be returned if the class/method is not already available. I would consider it ok that for such classes or methods the comments are not enforced, since it can still offer implementation with no JSDoc comment.

@brettz9 brettz9 requested a review from gajus May 25, 2019 14:51
@brettz9 brettz9 force-pushed the jsdocexports branch 2 times, most recently from 51b4d8c to 80c3cb4 Compare May 28, 2019 07:48
@Extersky
Copy link
Contributor Author

Noticed some more cases where referencing unknown variables would cause errors to be thrown in exportParser.js. The "browserEnv" option also modifies the window variable when the option is not enabled. I'll submit fixes for these today.

@brettz9
Copy link
Collaborator

brettz9 commented May 28, 2019

As nice as it is to be able to analyze the whole file, if it wouldn't be too hard to add an option to only check ancestors, I think that would be a nice optimization and possibly the only needed use case for some usage scenarios.

@LukeSkyw
Copy link

Very useful PR.

Sorry, I don't know how eslint and its plugin works internally, but I see you are using a custom parser, will that be a problem for Typescript? Would your rule include dangling functions, interfaces, enums, etc

Besides, would your solution worked has it used to on tslint completed-docs rule ? I always got frustrated with this rule because it does not allow precise control on where you do expect documentation and where you don't. For example, how to require documentation on everything on interfaces but to restrict the check to the top level doc for classes (no doc required on methods).

@Extersky
Copy link
Contributor Author

It is definitely useful to consider how the same rule could be made available for both JavaScript and TypeScript. The typescript-eslint project aims at being able to use many of the same ESLint rules with TypeScript. One obvious option is to implement the same rule in that project separately. Another option is to modify this implementation to make extendable, but I'm not sure if that is reasonable approach.
Does TypeScript have a language service planned or already existing that could provide info about source code exports? In that case the custom parser/resolver implemented here could be replaced.

@brettz9
Copy link
Collaborator

brettz9 commented Jun 10, 2019

Are we ok to merge this as it is now, @Extersky ?

@brettz9 brettz9 force-pushed the jsdocexports branch 2 times, most recently from 78c9301 to 43477e0 Compare June 11, 2019 07:59
@brettz9 brettz9 force-pushed the jsdocexports branch 2 times, most recently from f511818 to af03126 Compare June 14, 2019 10:48
@brettz9
Copy link
Collaborator

brettz9 commented Jun 16, 2019

It'd be nice to get this merged, as I think it should be ready. Anything else @Extersky before sending it on to gajus?

@Extersky
Copy link
Contributor Author

Sorry, I missed this. From my side merging this is ok in case the optimisation option for only checking through ancestors isn't needed yet.

@Extersky
Copy link
Contributor Author

The added documentation mentions browser window global export as an option, but this is not yet supported by browserEnv option. Should this be added? This is a simple modification to also check the window variable. I'll start preparing this in case it is included.

@Extersky
Copy link
Contributor Author

browserEnv option now causes exports to be checked from window global variable. This doesn't yet populate window variable from GeneratorDeclarations, but this isn't currently supported in the rule at all.

@brettz9
Copy link
Collaborator

brettz9 commented Jun 19, 2019

Yeah, sorry, I meant "checks" in the docs, but your docs for it are even better.

This looks great now. I think it is ready to merge. Though it may be helpful if you could offer a summary of any remaining to-dos for the lesser features that are not yet implemented, e.g., checking generator declarations.

@gajus , as a new feature with changes since your approval, do you still want notice? Don't mean to be annoying, just presume it is better to err on the side of caution until I have a better idea of what to pass on, as I took your earlier comment as being about smaller fixes and this is part of a new feature.

Extersky and others added 15 commits June 19, 2019 09:22
* When enabled, require all exported function bodies to have JSDoc block
* Fixes gajus#192
* Class declarations were not checked
* Added guards symbol exports, which caused errors before class declaration handling was added
* Also update README for changes from previous commit
* Change property name from publicFunctionsOnly to publicOnly
* The option was changed earlier to also check class declarations
* also generated fixes for readme assertions with allowOverrideWithoutParam
* window variables were not checked for exports
* window variable was populated incorrectly
@gajus
Copy link
Owner

gajus commented Jun 19, 2019

@gajus , as a new feature with changes since your approval, do you still want notice? Don't mean to be annoying, just presume it is better to err on the side of caution until I have a better idea of what to pass on, as I took your earlier comment as being about smaller fixes and this is part of a new feature.

I have no issues with you taking the lead. All changes you've pushed that I have reviewed did not ask for a single comment. Just take the lead and ping me if you need any help. I only ask not to do major code structure changes without first raising an issue (such as rewrite in TS, changing ESLint preset, etc).

@brettz9
Copy link
Collaborator

brettz9 commented Jun 19, 2019

Great, sounds good, thanks!

- Testing: Add more tests checking difference between `esm` and `cjs` options
- Testing: Ensure set other property to `false` to avoid potential interference
@brettz9
Copy link
Collaborator

brettz9 commented Jun 19, 2019

@Extersky : I've made one other change (discussed off list), re: switching to "esm", "cjs", and "window" for greater clarity on how the export types apply, as "exports" can, as you suggest, be confusable with exports in "cjs". I also thought browserEnv was misleading as well, since we were not checking for the environment; nor do I think we should given that some polyfills may work in a Node environment yet still expect docs on window if they set that option to true.

@brettz9 brettz9 merged commit ca3d885 into gajus:master Jun 19, 2019
@gajus
Copy link
Owner

gajus commented Jun 19, 2019

🎉 This PR is included in version 8.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@gajus gajus added the released label Jun 19, 2019
@brettz9
Copy link
Collaborator

brettz9 commented Jun 21, 2019

@Extersky : I just went through the codebase to try to get us to 100% coverage, and though I got through everything else, including some changes to hasReturnValue which would impact require-jsdoc, I was wondering if you might be able to help add tests for those lines in exportParser.js not yet covered in our tests?

Toward this end, I added an npm script, npm run test-cov which gives a printout showing a summary of which lines are still uncovered across all files. (The uncovered lines column (for exportParser.js) is truncated at the beginning, but one can start from the line numbers listed at the end.) If there really is some code which will not be covered now (and which has no public interface that we can add unit tests for), we can disable (with /* istanbul ignore next */), as I did for a handful of lines in jsdocUtils.js since they are private and are not currently possibly to be visited, but which are nevertheless probably wise to keep there as guards, e.g., in case expanded in the future for other types.

@brettz9 brettz9 mentioned this pull request Jun 22, 2019
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Require JSDoc on exported methods
4 participants