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

Stricter ordering of imports #456

Closed
3 tasks done
hyoban opened this issue Apr 19, 2024 · 8 comments
Closed
3 tasks done

Stricter ordering of imports #456

hyoban opened this issue Apr 19, 2024 · 8 comments
Labels
enhancement New feature or request

Comments

@hyoban
Copy link
Contributor

hyoban commented Apr 19, 2024

Clear and concise description of the problem

In some cases, users may cause the ordering of imports to change, either through manual adjustment or the editor's Organize imports feature. The current sorting rules import/order and sort-imports are not enough to unify the sorting style, which is unstable for git diff.

I am willing to submit a pull request for this change.

Suggested solution

Bring in eslint-plugin-simple-import-sort.

Alternative

Additional context

Validations

@hyoban hyoban added the enhancement New feature or request label Apr 19, 2024
@thenbe
Copy link
Contributor

thenbe commented Apr 19, 2024

This repo includes perfectionist, but you have to enable the rules manually. For example:

  {
    name: 'my-global-rules',
    rules: {
      'import/order': 'off', // handled by perfectionist
      'sort-imports': 'off', // handled by perfectionist
      'perfectionist/sort-imports': 'error',
      'perfectionist/sort-named-imports': 'error',
      'perfectionist/sort-exports': 'error',
    },
  }

I don't know how it compares to eslint-plugin-simple-import-sort, but it's much better than the vanilla rules (import/order and sort-imports) when it comes to consistency (for git diffs).

@hyoban
Copy link
Contributor Author

hyoban commented Apr 19, 2024

Thanks for the reminder. This should also be an option. There is no need to introduce additional plug-ins now.

@hyoban hyoban changed the title Introduce eslint-plugin-simple-import-sort Stricter ordering of imports Apr 19, 2024
@antfu
Copy link
Owner

antfu commented Apr 22, 2024

Would you share some examples of the result of reach rule? To me, the current state seems to work fine.

@thenbe
Copy link
Contributor

thenbe commented May 5, 2024

the current state seems to work fine.

I don't quite remember the details, but there were scenarios where the vanilla sort rules would not handle, whereas the alternative rules from perfectionist was able to handle.

It's been a while, but I think this was my main issue with it: import-js/eslint-plugin-import#1787

@hyoban
Copy link
Contributor Author

hyoban commented May 14, 2024

Close as it may not be a good idea. If you want it, you can try to use eslint-plugin-perfectionist or eslint-plugin-simple-import-sort.

Example of how to use eslint-plugin-simple-import-sort:

import antfu from '@antfu/eslint-config'
import simpleImportSort from 'eslint-plugin-simple-import-sort'

export default antfu(
  {},
  {
    plugins: {
      'simple-import-sort': simpleImportSort,
    },
    rules: {
      'import/order': 'off',
      'sort-imports': 'off',
      'simple-import-sort/imports': 'error',
      'simple-import-sort/exports': 'error',
    },
  },
)

@hyoban hyoban closed this as not planned Won't fix, can't repro, duplicate, stale May 14, 2024
@JohnCampionJr
Copy link

Hoping maybe this will help someone out who like me wants to use perfectionist but didn't understand why the above config didn't do much. The default perfectionist sort-imports doesn't define any groups. So my config pulls in the rules from the natural sort config.

https://gist.github.com/JohnCampionJr/a082f8967dd6341c41d923ad8502e999

@JohnCampionJr
Copy link

@antfu Would you be amenable to a PR adding an option to enable import sorting by perfectionist?

Basically make this available by option (could be set to alpha, natural or line length) per perfectionist settings

rules: {
      'import/order': 'off', // handled by perfectionist
      'sort-imports': 'off', // handled by perfectionist
      'perfectionist/sort-imports': perfectionistNatural.rules['perfectionist/sort-imports'],
      'perfectionist/sort-exports': perfectionistNatural.rules['perfectionist/sort-exports'],
      'perfectionist/sort-named-imports': perfectionistNatural.rules['perfectionist/sort-named-imports'],
      'perfectionist/sort-named-exports': perfectionistNatural.rules['perfectionist/sort-named-exports'],
    },

@mefengl
Copy link

mefengl commented Sep 6, 2024

@JohnCampionJr

My approach is to install perfectionist separately and then use its recommend rules like this:

import antfu from '@antfu/eslint-config'
import perfectionist from 'eslint-plugin-perfectionist'

export default antfu(
  // perfectionist
  {
    rules: {
      'import/order': 'off',
      ...perfectionist.configs['recommended-natural'].rules,
    },
  },
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants