Skip to content

[Fix] order: compare statement indices numerically so autofix respects side-effect imports - #3301

Open
nk-batman21 wants to merge 2 commits into
import-js:mainfrom
nk-batman21:fix-order-reorder-boundary
Open

nk-batman21 wants to merge 2 commits into
import-js:mainfrom
nk-batman21:fix-order-reorder-boundary

Conversation

@nk-batman21

Copy link
Copy Markdown

import/order --fix can 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:

import path from 'path';
import util from 'util';
import async from 'async';
import './polyfill';
import a from './a';
import b from './b';
import c from './c';
import d from './d';
import e from './e';
import f from './f';
import fs from 'fs';

--fix moves fs above ./polyfill. With fewer statements, the same layout is reported without a fix. The module in #2876 hits the same path: ./s/s and ./s/w/w get 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.js with 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 of tests/src shows no new failures.
Checked: eslint on the changed files.

…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

codecov Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.60%. Comparing base (a60d715) to head (cf3af72).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@nk-batman21

Copy link
Copy Markdown
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 import/order test failed in this job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Autofix moving a binding import from above to below a side-effecting import

1 participant