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

Alphabetize imports within groups #1105

Merged
merged 1 commit into from
Dec 7, 2019

Conversation

duncanbeevers
Copy link
Contributor

@duncanbeevers duncanbeevers commented May 17, 2018

In order to provide absolute ordering, rather than just group-wise ordering, this PR offers an alphabetize option that enforces locale-aware alphabetical intra-group ordering.

It makes no effort to provide a more robust fix experience, so multiple passes may be necessary to bring a set of imports into a stable configuration, though since an absolute ordering is available, a single-pass fix probably isn't unreasonable.

Fixes #1406.

…n groups

Fixes import-js#1406. Fixes import-js#389. Closes import-js#629. Closes import-js#1105. Closes import-js#1360.

Co-Authored-By: dannysindra <[email protected]>
Co-Authored-By: Radim Svoboda <[email protected]>
Co-Authored-By: Soma Lucz <[email protected]>
Co-Authored-By: Randall Reed, Jr <[email protected]>
Co-Authored-By: Jordan Harband <[email protected]>
@coveralls
Copy link

coveralls commented May 18, 2018

Coverage Status

Coverage decreased (-0.2%) to 96.216% when pulling 8224e51 on duncanbeevers:alphabetize into f12ae59 on benmosher:master.

@@ -1,7 +1,7 @@
# import/order: Enforce a convention in module import order

Enforce a convention in the order of `require()` / `import` statements.
+(fixable) The `--fix` option on the [command line] automatically fixes problems reported by this rule.
+(fixable) The `--fix` option on the [command line] automatically fixes problems reported by this rule. When the `alphabetize` option is used, multiple fix passes may be required.
Copy link
Member

Choose a reason for hiding this comment

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

I think it's pretty important to try to avoid multiple passes.

@@ -164,6 +163,26 @@ import index from './';
import sibling from './foo';
```

### `alphabetize`: boolean

By default, no ordering within groups is enforced. When `alphabetize: true` is used, imports must be alphabetically ordered within their groups.
Copy link
Member

Choose a reason for hiding this comment

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

This seems a bit restrictive; will people always want a-z? what about z-a? What about capital letters? Should it be a, A, b, c, D, or A, a, b, c, D, or a, b, c, A, D?

Copy link
Contributor Author

@duncanbeevers duncanbeevers May 18, 2018

Choose a reason for hiding this comment

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

Great questions. When I took a run at this in the past, I got some pushback around finding a balance between flexibility and simplicity.

Today's approach was more conservative, and the results aren't perfect. For example, we'd love to be able to differentiate webpack aliases from external node modules, and to sort asset-type resources such as .css separately from ordinary modules.

will people always want a-z? what about z-a?

People don't seem to care much about absolute ordering at all today; I'd be a little bit surprised if many people wanted z-a. My real fear is people wanting an arbitrary comparator. It's not difficult to add, but it's difficult to express declaratively, like in an .eslintrc.json file.

Basically, I want a completely stable sort, and alphabetical is pretty politically-neutral.

What about capital letters?

localeCompare accepts some options that it would be straightforward to thread through declaratively from the eslint config, if that's something users want.

Choose a reason for hiding this comment

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

Basically, I want a completely stable sort, and alphabetical is pretty politically-neutral.

I, as a person who's interested in using this lint, second this.

Copy link
Contributor

Choose a reason for hiding this comment

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

@ljharb what's your thought on @duncanbeevers response to your comment here? I am thinking a-z ordering should be good enough for majority of users.

Copy link
Member

Choose a reason for hiding this comment

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

I think a-z and z-a is a minimum, as well as an option that chooses between “z comes before/after A” and “z is right next to Z”. I definitely don’t want an arbitrary comparator.

@ljharb ljharb reopened this May 22, 2018
@duncanbeevers
Copy link
Contributor Author

We ended up writing a custom eslint plugin that we could tune to support our specific ordering rules. I closed this issue since I no longer need the functionality myself.

If someone wants to take this to completion feel free, but I'm setting it aside.

@robwierzbowski
Copy link

@duncanbeevers Can you show your custom plugin in a gist?

Recently my team has run into the shared chunk warning in the mini-css-extract-plugin documented here:

webpack-contrib/mini-css-extract-plugin#246
webpack-contrib/mini-css-extract-plugin#250

Alphabetizing and grouping (creating a deterministic, consistent order of imports across modules) may fix the shared chunk CSS import problem. Interested in this PR, and might be interested in contributing if my company will give me time to.

@stefanmaric
Copy link

@duncanbeevers Can you show your custom plugin in a gist?

I second the motion. 😄

@dannysindra
Copy link
Contributor

@ljharb Hi, I am interested to use the ordering here. Do you think @duncanbeevers 's PR is close to completion? What else would be necessary in order to get this PR merged? I probably can help if anyone can give me a direction

@ljharb
Copy link
Member

ljharb commented Jan 31, 2019

@dannysindra there’s a few open comments; it needs a rebase.

If you’d like to help, please don’t open another PR, but please do post a link to your branch here (with these rebased commits, and any new ones) and I’ll be happy to update the PR with them. Thanks!

@dannysindra
Copy link
Contributor

dannysindra commented Feb 4, 2019

@ljharb I finished making the changes in my repo: https://github.com/dannysindra/eslint-plugin-import/tree/alphabetize.

So I made the following configs for the alphabetize option:

  • order: [string] - either ignore (do not use alphabetize), asc, or desc
  • ignoreCase: [boolean] - if false put upper before lowercase (normal js sort), if true put upper and lowercase together (localeCompare)

Please let me know if you have any feedback / questions. This is my first contribution btw, so I apologize in advance if I made beginner mistakes here and there

@ljharb
Copy link
Member

ljharb commented Feb 4, 2019

@dannysindra i was hoping for a rebase, not a merge (no merge commits) but i've gone ahead and updated this branch with a rebased version of its, and your, commits. I'll give it a review once tests are passing.

@dannysindra
Copy link
Contributor

dannysindra commented Feb 4, 2019

@ljharb thanks for the quick response! My bad, I tried to rebase and somehow must have pulled and merged. I am trying to understand what causes the tests to fail here.. it passes locally and on some env in the AppVeyor; but failed on others? Also, is there a way to run the coveralls locally? I cannot tell which lines / files decreased the coverage..

@ljharb
Copy link
Member

ljharb commented Feb 4, 2019

@dannysindra make sure you're checking out the rebased version; the travis tests are failing too.

@dannysindra
Copy link
Contributor

@ljharb alright, I have rebased, and pushed again with the fix 7fa20f0

@ljharb
Copy link
Member

ljharb commented Feb 4, 2019

@dannysindra thanks, updated (next time, please delete your local branch, and re-check it out from 3db6bd7)

@dannysindra

This comment has been minimized.

@ljharb

This comment has been minimized.

@dannysindra

This comment has been minimized.

@VincentLanglet
Copy link

@dannysindra There are conflicts, so you can start by rebasing the branch.
The tests will re-run with the new push.

@ljharb
Copy link
Member

ljharb commented May 5, 2019

This still needs a rebase.

@kg-currenxie
Copy link

Definitely want this! :D

@tayler-king
Copy link

Any progress on getting this merged?

@stropho
Copy link
Contributor

stropho commented May 15, 2019

rebased : https://github.com/stropho/eslint-plugin-import-1/commit/b80dc108280b638229a6f148e1fe10795f1c3c5

--> https://github.com/stropho/eslint-plugin-import-1/commit/e6a5ac36c0c6024ae6f9f2629c96830d17ef586a
Well obviously it wasn't only about rebasing the existing code :-D I'm willing to do max. 1 more change. So please, if you have any more suggestions for improvement, give it all to me at once

@lext-7
Copy link

lext-7 commented May 17, 2019

Want it !

```js
import foo from 'foo';
import bar from 'bar';
import Baz from 'Baz';
Copy link

Choose a reason for hiding this comment

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

These examples make it unclear whether the sorting happens by module names or by bindings names. Making them different would help (this applies to all new examples in this file).

Copy link
Contributor

Choose a reason for hiding this comment

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

And here I though the code is all good and I just need to rebase it.
Totally agree with you about this one

@syashenkov
Copy link

Any progress on this? I'm ready to contribute to this feature

@stropho
Copy link
Contributor

stropho commented Jun 4, 2019

Any progress on this? I'm ready to contribute to this feature

Good to hear. Right now, it's in a state of waiting for the maintainers to express themselves - and possibly merge it #1360

@ljharb ljharb force-pushed the alphabetize branch 2 times, most recently from aae9da7 to 8224e51 Compare December 7, 2019 00:29
@ljharb ljharb closed this in 8224e51 Dec 7, 2019
@ljharb ljharb merged commit 8224e51 into import-js:master Dec 7, 2019
@duncanbeevers duncanbeevers deleted the alphabetize branch December 7, 2019 02:38
marcusdarmstrong pushed a commit to marcusdarmstrong/eslint-plugin-import that referenced this pull request Dec 9, 2019
…n groups

Fixes import-js#1406. Fixes import-js#389. Closes import-js#629. Closes import-js#1105. Closes import-js#1360.

Co-Authored-By: dannysindra <[email protected]>
Co-Authored-By: Radim Svoboda <[email protected]>
Co-Authored-By: Soma Lucz <[email protected]>
Co-Authored-By: Randall Reed, Jr <[email protected]>
Co-Authored-By: Jordan Harband <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

An alphabetical order rule?