Skip to content

Commit 347d78b

Browse files
pri1311ljharb
authored andcommitted
[Fix] order: move nested imports closer to main import entry
1 parent 995c12c commit 347d78b

File tree

3 files changed

+97
-8
lines changed

3 files changed

+97
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
99
### Added
1010
- [`newline-after-import`]: add `considerComments` option ([#2399], thanks [@pri1311])
1111

12+
### Fixed
13+
- [`order`]: move nested imports closer to main import entry ([#2396], thanks [@pri1311])
14+
1215
### Changed
1316
- [Tests] `named`: Run all TypeScript test ([#2427], thanks [@ProdigySim])
1417

@@ -987,6 +990,7 @@ for info on changes for earlier releases.
987990
[#2417]: https://github.com/import-js/eslint-plugin-import/pull/2417
988991
[#2411]: https://github.com/import-js/eslint-plugin-import/pull/2411
989992
[#2399]: https://github.com/import-js/eslint-plugin-import/pull/2399
993+
[#2396]: https://github.com/import-js/eslint-plugin-import/pull/2396
990994
[#2393]: https://github.com/import-js/eslint-plugin-import/pull/2393
991995
[#2388]: https://github.com/import-js/eslint-plugin-import/pull/2388
992996
[#2381]: https://github.com/import-js/eslint-plugin-import/pull/2381

src/rules/order.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22

33
import minimatch from 'minimatch';
4+
import includes from 'array-includes';
5+
46
import importType from '../core/importType';
57
import isStaticRequire from '../core/staticRequire';
68
import docsUrl from '../docsUrl';
@@ -244,16 +246,37 @@ function getSorter(ascending) {
244246
const multiplier = ascending ? 1 : -1;
245247

246248
return function importsSorter(importA, importB) {
247-
let result;
248-
249-
if (importA < importB) {
250-
result = -1;
251-
} else if (importA > importB) {
252-
result = 1;
249+
let result = 0;
250+
251+
if (!includes(importA, '/') && !includes(importB, '/')) {
252+
if (importA < importB) {
253+
result = -1;
254+
} else if (importA > importB) {
255+
result = 1;
256+
} else {
257+
result = 0;
258+
}
253259
} else {
254-
result = 0;
255-
}
260+
const A = importA.split('/');
261+
const B = importB.split('/');
262+
const a = A.length;
263+
const b = B.length;
264+
265+
for (let i = 0; i < Math.min(a, b); i++) {
266+
if (A[i] < B[i]) {
267+
result = -1;
268+
break;
269+
} else if (A[i] > B[i]) {
270+
result = 1;
271+
break;
272+
}
273+
}
256274

275+
if (!result && a !== b) {
276+
result = a < b ? -1 : 1;
277+
}
278+
}
279+
257280
return result * multiplier;
258281
};
259282
}

tests/src/rules/order.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,47 @@ ruleTester.run('order', rule, {
675675
alphabetize: { order: 'desc' },
676676
}],
677677
}),
678+
// Option alphabetize: {order: 'asc'} and move nested import entries closer to the main import entry
679+
test({
680+
code: `
681+
import a from "foo";
682+
import c from "foo/bar";
683+
import d from "foo/barfoo";
684+
import b from "foo-bar";
685+
`,
686+
options: [{ alphabetize: { order: 'asc' } }],
687+
}),
688+
// Option alphabetize: {order: 'asc'} and move nested import entries closer to the main import entry
689+
test({
690+
code: `
691+
import a from "foo";
692+
import c from "foo/foobar/bar";
693+
import d from "foo/foobar/barfoo";
694+
import b from "foo-bar";
695+
`,
696+
options: [{ alphabetize: { order: 'asc' } }],
697+
}),
698+
// Option alphabetize: {order: 'desc'} and move nested import entries closer to the main import entry
699+
test({
700+
code: `
701+
import b from "foo-bar";
702+
import d from "foo/barfoo";
703+
import c from "foo/bar";
704+
import a from "foo";
705+
`,
706+
options: [{ alphabetize: { order: 'desc' } }],
707+
}),
708+
// Option alphabetize: {order: 'desc'} and move nested import entries closer to the main import entry with file names having non-alphanumeric characters.
709+
test({
710+
code: `
711+
import b from "foo-bar";
712+
import c from "foo,bar";
713+
import d from "foo/barfoo";
714+
import a from "foo";`,
715+
options: [{
716+
alphabetize: { order: 'desc' },
717+
}],
718+
}),
678719
// Option alphabetize with newlines-between: {order: 'asc', newlines-between: 'always'}
679720
test({
680721
code: `
@@ -2230,6 +2271,27 @@ ruleTester.run('order', rule, {
22302271
message: '`bar` import should occur before import of `Bar`',
22312272
}],
22322273
}),
2274+
// Option alphabetize: {order: 'asc'} and move nested import entries closer to the main import entry
2275+
test({
2276+
code: `
2277+
import a from "foo";
2278+
import b from "foo-bar";
2279+
import c from "foo/bar";
2280+
import d from "foo/barfoo";
2281+
`,
2282+
options: [{
2283+
alphabetize: { order: 'asc' },
2284+
}],
2285+
output: `
2286+
import a from "foo";
2287+
import c from "foo/bar";
2288+
import d from "foo/barfoo";
2289+
import b from "foo-bar";
2290+
`,
2291+
errors: [{
2292+
message: '`foo-bar` import should occur after import of `foo/barfoo`',
2293+
}],
2294+
}),
22332295
// Option alphabetize {order: 'asc': caseInsensitive: true}
22342296
test({
22352297
code: `

0 commit comments

Comments
 (0)