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

style(v2): reduce number of ESLint warnings #4993

Merged
merged 16 commits into from
Jun 24, 2021

Conversation

Josh-Cena
Copy link
Collaborator

@Josh-Cena Josh-Cena commented Jun 17, 2021

Motivation

When I'm working on the project I was constantly overwhelmed (exaggeration here; there aren't so many) by ESLint warnings. Although they aren't serious issues, I decide it's better that we address these issues, since this is what we have the linter for.

There are some warnings that I probably need help with, especially regarding any types. (I realize how intractable typing objects can become.) I don't want to easily resort to eslint-disable comments.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

These are mostly style changes that don't really impact functionality to any degree.

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Jun 17, 2021
@Josh-Cena Josh-Cena marked this pull request as draft June 17, 2021 09:15
@netlify
Copy link

netlify bot commented Jun 17, 2021

✔️ [V2]
Built without sensitive environment variables

🔨 Explore the source changes: 269c971

🔍 Inspect the deploy log: https://app.netlify.com/sites/docusaurus-2/deploys/60d490fd4a60f80007cc98af

😎 Browse the preview: https://deploy-preview-4993--docusaurus-2.netlify.app

@github-actions
Copy link

github-actions bot commented Jun 17, 2021

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 82
🟢 Accessibility 96
🟢 Best practices 100
🟢 SEO 100
🟢 PWA 95

Lighthouse ran on https://deploy-preview-4993--docusaurus-2.netlify.app/

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, not agreeing with everything but overwall happy to reduce eslint warnings

I personally like TS function return type inference but it seems to be a best practice to type return types explicitly so why not

packages/docusaurus-plugin-content-blog/src/blogUtils.ts Outdated Show resolved Hide resolved
@@ -127,7 +127,10 @@ function assertIsCategory(
);
}
// "collapsed" is an optional property
if (item.hasOwnProperty('collapsed') && typeof item.collapsed !== 'boolean') {
if (
Object.prototype.hasOwnProperty.call(item, 'collapsed') &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too much ceremony, I'd just replace by typeof item.collapsed !== "undefined" ?

Copy link
Collaborator Author

@Josh-Cena Josh-Cena Jun 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is actually a consideration about collapsed existing on the prototype chain of item. Is it safe to assume here that typeof item.collapsed !== "undefined" works the same?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it looks to me as the sidebar is a regular object

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you replace with typeof item.collapsed !== "undefined" ? it has not been done here

packages/docusaurus-plugin-content-docs/src/versions.ts Outdated Show resolved Hide resolved
Josh-Cena and others added 2 commits June 18, 2021 18:41
Co-authored-by: Sébastien Lorber <slorber@users.noreply.github.com>
…x.tsx

Co-authored-by: Sébastien Lorber <slorber@users.noreply.github.com>
@Josh-Cena
Copy link
Collaborator Author

@slorber If I'm delving into typing (the any types), this PR is likely to shift its focus away from some simple style improvements. Should I do it here or should I open a new PR?

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
@slorber
Copy link
Collaborator

slorber commented Jun 18, 2021

@slorber If I'm delving into typing (the any types), this PR is likely to shift its focus away from some simple style improvements. Should I do it here or should I open a new PR?

smaller PRs are always easier to review/merge, so splitting the work is not a bad idea.

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
@Josh-Cena
Copy link
Collaborator Author

Josh-Cena commented Jun 18, 2021

@slorber I think I'm mostly done—reduced warning count by >50. The rest of them (33) contain:

  • iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations

// async process made sequential on purpose! order matters
for (const doc of docs) {
// eslint-disable-next-line no-await-in-loop
await handleDocItem(doc);
}

Need some help here... Airbnb is complaining about for...of loops being too heavy. Would this be transformed to a reduce loop like this? I'm not so confident with async functions.

await docs.reduce(
  (p, doc) => p.then(() => handleDocItem(doc)),
  Promise.resolve(),
);
  • Visible, non-interactive elements with click handlers must have at least one keyboard listener

<div
className="react-toggle-track"
role="button"
tabIndex={-1}
onClick={() => inputRef.current?.click()}>

I see that we are planning to add hotkey binding maps. This warning will remind us of this feature :P

  • Lots of Unexpected any. Specify a different type errors. Need substantial typing improvements

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
@Josh-Cena Josh-Cena marked this pull request as ready for review June 21, 2021 01:49
@Josh-Cena Josh-Cena requested a review from slorber June 21, 2021 02:04
@slorber
Copy link
Collaborator

slorber commented Jun 24, 2021

Need some help here... Airbnb is complaining about for...of loops being too heavy. Would this be transformed to a reduce loop like this? I'm not so confident with async functions.

Honestly don't like Airbnb preset, it's way too opinionated with weird rules, but it's the current docu preset so let's just disable what is annoying. This rule can also be disabled, it's not really applicable for NodeJS code IMHO.

I'll merge this PR as is, any further improvements are welcome

Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finally, asking for a few changes 🤪

// eslint-disable-next-line no-await-in-loop
await handleDocItem(doc);
}
await docs.reduce(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually I'd prefer to revert this change

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I reverted this, I'm pretty confident that the new code works correctly😂 Just that it is less readable

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes definitively works with reduce but much more complicated :)

@@ -127,7 +127,10 @@ function assertIsCategory(
);
}
// "collapsed" is an optional property
if (item.hasOwnProperty('collapsed') && typeof item.collapsed !== 'boolean') {
if (
Object.prototype.hasOwnProperty.call(item, 'collapsed') &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you replace with typeof item.collapsed !== "undefined" ? it has not been done here

packages/docusaurus/src/server/configValidation.ts Outdated Show resolved Hide resolved
@Josh-Cena
Copy link
Collaborator Author

Okay cool. Lemme address those...

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
@slorber
Copy link
Collaborator

slorber commented Jun 24, 2021

thanks 👍

@slorber slorber merged commit 462b1cf into facebook:master Jun 24, 2021
@slorber slorber added the pr: polish This PR adds a very minor behavior improvement that users will enjoy. label Jun 24, 2021
@Josh-Cena Josh-Cena deleted the eslint-warn branch June 24, 2021 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: polish This PR adds a very minor behavior improvement that users will enjoy.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants