-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 WordPress JSDoc ESLint configuration #4245
Conversation
Maybe we should enable this as a "warning" to start fixing those as we add/update code? |
This is ready to be reviewed. |
"processors": { | ||
"downgrade-unmodified-lines": { | ||
"remote": "origin/master", | ||
"rulesNotToDowngrade": ["no-unused-vars"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this rule is different?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Via https://www.npmjs.com/package/eslines#how-to-use-it:
"The
no-unused-vars
rule won't be downgraded, though - this is one case where changing one line can cause a linting error in a different one so we recommend preventing it from downgrading."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is working pretty well for me. looking forward getting rid of all these warnings.
In e352c97 I updated to a more recent commit of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great, thanks @atimmer 👍
This implementation shows errors for lines that differ from Is that intentional? I read @youknowriad's comment as that we only ever want warnings. |
@noisysocks That comment was before |
Cool, just thought I'd check since it wasn't explicitly mentioned in the PR description! 😄 |
I'm not convinced this rule suits the project well, at least in its current form. Even with
/**
* Returns a menu of tools representing actions on the editor's content.
*
* @returns {WPElement} Element.
*/
export default function EditorActions() {
return (
<MenuItemsGroup className="editor-actions" label={ __( 'Tools' ) }>
<CopyContentButton />
</MenuItemsGroup>
);
} Assuming the component takes props, that will require an additional Large or otherwise significant or hard-to-grasp components will obviously warrant JSDoc, but one of the strengths of our compositional paradigm is that it encourages breaking down functionality in layers and spawning small components. If
I think the main point of this is that, whenever we're talking about components, we're dealing with a standard interface:
With that premise that it is a standard interface, it follows that it's wasteful to document said interface every time it's implemented. A trivial but potentially naïve way forward would be to give components a free pass by excluding any function declaration whose identifier has an uppercase letter as the leading character. For class components, perhaps exclude classes defining a |
👍 to @mcsf concerns, I've already shared similar concerns in #core-js meetings but now facing them in reality shows that it's really too much for a standard interface. |
This is simply laziness, I'm sorry. It's obvious what is in the code if you're well versed in the code, if you're not, this:
Is not necessarily self-explanatory. Spending two to three seconds to write that it returns a copy content button really does help. |
In a large open source project like WordPress, good documentation is key. Maybe in smaller projects or in projects internal to a company, the documentation effort may have a different priority and may adapt to specific momentary needs. In WordPress, I'd strongly suggest to not repeat the same mistakes done in the past and document everything. I'd suggest to try to not think as an expert developer looking at code that was coded now by yourself or your colleagues. Instead, I'd recommend to make an effort to think at someone with a low level of expertise who, in 2 years from now, reads for the first time your code. Expecting that they understand what is |
I wholeheartedly agree, but in order for that argument to be useful here, it needs to be established that adding JSDoc to the entities at hand would constitute adding good documentation, which is the subject of debate. Otherwise, the implication would've been that I'd argued against good documentation.
I also don't refute this. I expect that I and others should be called out on whenever we push code that's hard to grasp as blind spots develop from our experience with the codebase.
IMO, if a codebase needs to be entirely documented with natural language, that's symptomatic of a pretty unhealthy project. The key in that sentence is natural language; code should act as documentation. An easy instance of this is function signatures, even in a untyped language like JS. JSDoc absolutely should fill the gaps, but aiming to fill more than the gaps means we have information duplication. That duplication entails some added cost of maintenance, as well, arguably, as some overhead for the reader of the codebase. Circling back to @jdevalk's highlight:
We mustn't omit the function signature, as it is vital: export default function EditorActions() {
return (
That I disagree with, because the specifics of what it contains shouldn't have to bleed into the documenting description.
At the personal level, it's not my place to argue against this claim. However, having made my point above, I posit that it can actually be counterproductive to port everything to comments. Duplicating is easy; a bit of editor fu and a quick sentence do the trick. I'm more concerned with the maintenance overhead and, paradoxically, the reading overhead. Finally, I should probably reiterate that I don't think JSDoc to be generally useless. Not at all. But there are trade-offs to globally enforcing it. |
Aside, what are your thoughts on method-level enforcement? |
I think that the component model used in modern JavaScript apps negates a lot of the need for stringent inline documentation. The strength of this model is that a component is isolated from the rest of the application: a mortal developer is realistically able to visualise the inputs, state and output of a single component. My worry with this rule is that it comes at the expense of discouraging developers from changing how a component works internally. In my experience, a constantly changing codebase is a healthy codebase. My preference is that we require JSDoc comments on only the contract that a module or component has with its outside world. That is:
|
Just opened #4504, before I was aware of this conversation. Please let's remove at least the requirement for JSDocs on on React component methods. 🙂 |
@noisysocks, this sounds like a good default for Eslint verification. It would still allow to auto-generate docs for all code that is consumed by contributors in most of the cases. @mtias I'm wondering how this new Eslint rules will impact README files. They were often used to present API methods, which duplicates what JSDoc does. Should we standardize that and create scripts to generate those sections? Actually, this is how things are maintained in WP-CLI. @schlessera or @danielbachhuber can give us more details on that. |
@ntwb, we can remove |
In case there are others like me using the DocBlockr Sublime Text plugin, here's a user configuration which can be added to adhere to the updated documentation styling: Preferences > Package Settings > DocBlockr > Settings - User {
"jsdocs_return_tag": "@returns",
"jsdocs_spacer_between_sections": true,
"jsdocs_per_section_indent": true,
"jsdocs_lower_case_primitives": true
} |
Thanks @aduth! |
Re. generated documentation like referenced above, you generally do this for everything that can be considered an "interface". So, you have the public interface, which is what external developers will use to create third-party blocks or integrations, and then you have the internal interface which deals with the intra-communication between your individual components/modules. Ideally, both of these categories should be documented in the code without fail and the external documentation should be generated out of this source code. This is the only reliable way of keeping the documentation in sync with the actual code. As to the usefulness of the docblocks for simplistic methods... You would not only add a short description of what and why (the why normally being something that is not obvious from the code alone), but also historical information that can inform future decisions... Knowing when something was introduced will help with debugging version-dependent bugs and be smart about deprecations. Knowing what discourse led to the current implementation avoids switching between two good-enough approaches every few months. There is a lot of potential information that you can put into such a docblock that will not necessarily help you understand the one or two lines of code, but help you make better decisions two years down the road. |
Description
The WordPress ESLint configuration plugin has a JSDoc configuration. I've added this to the ESLint configuration to see how much would fail. And start the discussion about how to integrate this.
Current stats: