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

[web] Add eslint-plugin-simple-import-sort #49368

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

kiosion
Copy link
Contributor

@kiosion kiosion commented Nov 22, 2024

  • Add config for eslint-plugin-simple-import-sort: Serves same purpose as current import/order rule, but has better support for autofix, prevents newlines accumulating (currently, running import auto fix can lead to extra newlines being left in place), and has 0 dependencies
    • New import sorting order is identical to the prior config, but now sorts within import groups alphabetically, and within destructured imports alphabetically.

@kiosion kiosion added do-not-merge no-changelog Indicates that a PR does not require a changelog entry labels Nov 22, 2024
@ravicious ravicious self-requested a review November 25, 2024 09:25
Copy link
Member

@ravicious ravicious left a comment

Choose a reason for hiding this comment

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

Looks good for the most part, though the sorting of gen-proto-ts seems to not work properly.

package.json Outdated Show resolved Hide resolved
web/packages/build/.eslintrc.js Show resolved Hide resolved
web/packages/build/.eslintrc.js Show resolved Hide resolved
// Side-effect imports (e.g. CSS)
['^\u0000(?!.*\u0000$)'],
// Any 3rd-party imports (e.g. 'react', 'styled-components')
[`^(?![./])(?!(?:${excludedPackagesPattern})(?:/|$))(?!.*\u0000$).+`],
Copy link
Member

Choose a reason for hiding this comment

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

What's the deal with \u0000 in every import group? The diff that I posted, which was based on the default import groups, had it only in side effect imports.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That represents a byte present at the end of type-only imports, so in order to keep those sorted to the bottom we need a negative lookahead at the end of previous import groups

Comment on lines +139 to +150
// Allow unused vars/args that start with an underscore
{
varsIgnorePattern: '^_',
argsIgnorePattern: '^_',
},
Copy link
Member

Choose a reason for hiding this comment

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

👍, Bartosz was complaining about this recently.

web/packages/build/.eslintrc.js Outdated Show resolved Hide resolved
@ravicious
Copy link
Member

ravicious commented Nov 25, 2024

At some point (maybe once you think it's ready to be reviewed) it'd be good to post about it on Slack to see what the rest of the team thinks about this.

@kiosion
Copy link
Contributor Author

kiosion commented Nov 25, 2024

At some point (maybe once you think it's ready to be reviewed) it'd be good to post about it on Slack to see what the rest of the team thinks about this.

For sure, was planning to. Don't just want to open this big a PR (diff-wise) without asking some feedback first.

@kiosion kiosion force-pushed the maxim/simple-import-sort branch 2 times, most recently from fdf8f4c to 0853d45 Compare November 25, 2024 16:25
* Add config for `eslint-plugin-simple-import-sort`: Serves same purpose
  as current `import/order` rule, but has better support for autofix,
  prevents newlines accumulating, and has 0 dependencies
@kiosion kiosion force-pushed the maxim/simple-import-sort branch from 0853d45 to 70acdd3 Compare November 25, 2024 16:28
Copy link
Member

@ravicious ravicious left a comment

Choose a reason for hiding this comment

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

LGTM once gen-proto-ts and node imports are addressed.

import zlib from 'node:zlib';

import tarFs from 'tar-fs';
Copy link
Member

@ravicious ravicious Nov 25, 2024

Choose a reason for hiding this comment

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

Node imports are grouped with 3rd-party libs.

This is what I found in the docs:

Each import is matched against all regexes on the from string. The import ends up at the regex with the longest match. In case of a tie, the first matching regex wins.

https://github.com/lydell/eslint-plugin-simple-import-sort?tab=readme-ov-file#custom-grouping

I think the current groups might work if we get rid of excludedPackagesPattern? The default sort groups have this:

          // 3rd-party packages.

          // Things that start with a letter (or digit or underscore), or `@` followed by a letter.

          ['^@?\\w'],

…so I believe we have to make sure we match just on the first letter (but idk how easy that is when type imports come into play).

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Assuming that we want to enable some kind of rule which would automatically pluck out type imports from regular imports, I wonder if putting type imports last within each group would make sense?

https://github.com/lydell/eslint-plugin-simple-import-sort/blob/66d84f742959bf68f4575b6ac5a65978ceb7cfbc/examples/.eslintrc.js#L210-L228

Copy link
Member

Choose a reason for hiding this comment

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

At the moment, no matter which option we pick wrt type imports isn't going to seem like a big deal. But down the road, assuming that their usage will be more common in the codebase, I wonder if it wouldn't look more tidy if type imports were within individual groups rather their own group.

Personally, when I look at imports, the most important information for me is whether the package is from the stdlib, or a 3rd-party lib, or maybe one of our packages. From that perspective, it'd make sense for type imports to be put within each particular group as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gotcha, that makes sense to me. Will take another look at this later, and see how things look grouping type imports at the end of groups.

Part of the issue we have currently is allowing importing types/interfaces alongside functions/enums/consts; I think enforcing using import type syntax would help clean things up as well.

Copy link
Member

Choose a reason for hiding this comment

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

I figured out how to put type imports at the end of each group in #50619 (left a comment in the ESLint config explaining what's going on) and credited you in the commits under both OSS and ent PRs. I also based this on our updated ESLint 9 config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do-not-merge no-changelog Indicates that a PR does not require a changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants