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

Bug(sort-imports): Misleading Missed spacing errors #322

Open
2 tasks done
hugop95 opened this issue Oct 15, 2024 · 3 comments · May be fixed by #323
Open
2 tasks done

Bug(sort-imports): Misleading Missed spacing errors #322

hugop95 opened this issue Oct 15, 2024 · 3 comments · May be fixed by #323
Labels
bug Something isn't working

Comments

@hugop95
Copy link
Contributor

hugop95 commented Oct 15, 2024

Describe the bug

Not a breaking bug but can be a minor inconvenience: Missed spacing errors may be reported way more than what the sorted result would require. See code below

Code example

Configuration:

{
  "newlinesBetween": "always",
  "groups": [
    "external", "unknown"
  ]
}

Code:

import a1 from "./a"
import a2 from "a"
import b1 from "./b"
import b2 from "b"
import c1 from "./c"
import c2 from "c"
import d1 from "./d"
import d2 from "d"

The sorted result is:

import a2 from "a"
import b2 from "b"
import c2 from "c"
import d2 from "d"

import a1 from "./a"
import b1 from "./b"
import c1 from "./c"
import d1 from "./d"

but the following Missed spacing are reported (order-errors are not shown here):

  3:1  error  Missed spacing between "a" and "./b" imports            perfectionist/sort-imports
  5:1  error  Missed spacing between "b" and "./c" imports            perfectionist/sort-imports
  7:1  error  Missed spacing between "c" and "./d" imports            perfectionist/sort-imports

ESLint version

8.57.0

ESLint Plugin Perfectionist version

3.8.0

Additional comments

No response

Validations

  • Read the docs.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
@hugop95 hugop95 added the bug Something isn't working label Oct 15, 2024
@azat-io
Copy link
Owner

azat-io commented Oct 15, 2024

I think that's normal behavior. Don't you think so?

@hugop95
Copy link
Contributor Author

hugop95 commented Oct 15, 2024

@azat-io It is misleading in my opinion because it tells the user to make fixes that in the end will be erased when the imports will eventually be sorted.

I think that each error reported (and as such fix) should bring the code closer to the expected state.

In the example above, I believe that there is no need to throw a Missed spacing error until the code shows

import d2 from "d"
import a1 from "./a"

That's how I see it, but it's nitpicking, I understand if you see it differently!

@OlivierZal, what do you think?

@OlivierZal
Copy link

OlivierZal commented Oct 15, 2024

I understand both points of view.

My way of using eslint (preference for autofix) would make me vote for @hugop95's proposal.

However I think it's good to warn the users about the errors he actually has, and not about what these errors could be after a potential fix.

I say potential because maybe some developers don't enable autofix, so they could fix manually based on (all) the error messages.

In the example given in the previous comment, maybe one's fix wouldn't be the one proposed, but another one based on the missing space warnings:

// comment separator
import a1 from "a"

import a2 from "./a"

// comment separator
import b1 from "b"

import b2 from "./b"

// comment separator
import c1 from "c"

import c2 from "./c"

// comment separator
import d1 from "d"

import d2 from "./d"

Then displaying all the warning messages could have some interest, so the user could fix accordingly as wanted.

Again, it's not my way to proceed, so maybe my example is too fictive to be relevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants