Repository navigation
[Fix] order: compare statement indices numerically so autofix respects side-effect imports - #3301
Open
nk-batman21 wants to merge 2 commits into
Open
nk-batman21 wants to merge 2 commits into
nk-batman21 wants to merge 2 commits into
Conversation
…cts side-effect imports `canReorderItems` sorted the two statement indices with a bare `.sort()`, which compares them as strings: `[2, 10]` became `[10, 2]`, the slice of statements between them was empty, and the check passed. In files with more than 10 top-level statements, depending on where the two statements sat, the autofix could then move imports across unassigned imports (polyfills, CSS) and, for CommonJS, across other statements, changing evaluation order. Shorter files were already left alone. Fixes import-js#2876.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3301 +/- ##
=======================================
Coverage 79.59% 79.60%
=======================================
Files 98 98
Lines 4549 4550 +1
Branches 1543 1575 +32
=======================================
+ Hits 3621 3622 +1
Misses 928 928 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Author
|
I checked the red macOS job on cf3af72. It had 3,154 passing tests and one failure: the CLI test for invalid JSON (#1645) exceeded Mocha’s 5-second timeout. The same test hit the same timeout on macOS with ESLint 8 in a run from Oct 18, before I opened this PR, and again on Nov 11. No |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
import/order --fixcan move an import across a side-effect import (a polyfill, a CSS file) or, in CommonJS, across a plain statement, which changes evaluation order. The rule already refuses to autofix across those in short files, but once a file has more than 10 top-level statements it can still move the import, depending on where the two imports sit (for example the 3rd and 11th statements).For example, with the default options:
--fixmovesfsabove./polyfill. With fewer statements, the same layout is reported without a fix. The module in #2876 hits the same path:./s/sand./s/w/wget moved below the CSS imports.The cause is in
canReorderItems: it sorts the two statement indices with a bare.sort(), which compares them as strings, so[2, 10]becomes[10, 2], the slice of statements in between is empty, and the check always passes. This sorts them numerically. Imports that are out of order across such a boundary are still reported, just not autofixed, which is how shorter files already behave. Moving them up to just above the boundary instead would be a separate change to how the fix is built.Fixes #2876.
Ran:
tests/src/rules/order.jswith ESLint 10 and 8; the two new cases (ESM with a side-effect import, CommonJS with a function call, 11 statements each) fail without the change and pass with it. The rest oftests/srcshows no new failures.Checked:
eslinton the changed files.