Skip to content

Commit d51eb51

Browse files
committed
Fallback to the first solution
1 parent 126d094 commit d51eb51

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

Diff for: src/rules/order.js

+28-8
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,34 @@ function getSorter(ascending) {
246246
return function importsSorter(importA, importB) {
247247
let result = 0;
248248

249-
const A = importA.split('/');
250-
const B = importB.split('/');
251-
252-
if (A < B) {
253-
result = -1;
254-
} else if (A > B) {
255-
result = 1;
256-
}
249+
if (!importA.includes('/') && !importB.includes('/')) {
250+
if (importA < importB) {
251+
result = -1;
252+
} else if (importA > importB) {
253+
result = 1;
254+
} else {
255+
result = 0;
256+
}
257+
} else {
258+
const A = importA.split('/');
259+
const B = importB.split('/');
260+
const a = A.length
261+
const b = B.length
262+
263+
for (var i = 0; i < Math.min(a,b); i++) {
264+
if (A[i] < B[i]) {
265+
result = -1;
266+
break;
267+
} else if (A[i] > B[i]) {
268+
result = 1;
269+
break;
270+
}
271+
}
272+
273+
if (!result && a != b) {
274+
result = a < b ? -1 : 1
275+
}
276+
}
257277

258278
return result * multiplier;
259279
};

Diff for: tests/src/rules/order.js

+11
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,17 @@ ruleTester.run('order', rule, {
705705
`,
706706
options: [{ alphabetize: { order: 'desc' } }],
707707
}),
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+
}),
708719
// Option alphabetize with newlines-between: {order: 'asc', newlines-between: 'always'}
709720
test({
710721
code: `

0 commit comments

Comments
 (0)