diff --git a/src/rules/order.js b/src/rules/order.js index 827beb9238..bf1fdbb2b6 100644 --- a/src/rules/order.js +++ b/src/rules/order.js @@ -246,14 +246,34 @@ function getSorter(ascending) { return function importsSorter(importA, importB) { let result = 0; - const A = importA.split('/'); - const B = importB.split('/'); - - if (A < B) { - result = -1; - } else if (A > B) { - result = 1; - } + if (!importA.includes('/') && !importB.includes('/')) { + if (importA < importB) { + result = -1; + } else if (importA > importB) { + result = 1; + } else { + result = 0; + } + } else { + const A = importA.split('/'); + const B = importB.split('/'); + const a = A.length + const b = B.length + + for (var i = 0; i < Math.min(a,b); i++) { + if (A[i] < B[i]) { + result = -1; + break; + } else if (A[i] > B[i]) { + result = 1; + break; + } + } + + if (!result && a != b) { + result = a < b ? -1 : 1 + } + } return result * multiplier; }; diff --git a/tests/src/rules/order.js b/tests/src/rules/order.js index 04bfdeac80..da3e96104d 100644 --- a/tests/src/rules/order.js +++ b/tests/src/rules/order.js @@ -705,6 +705,17 @@ ruleTester.run('order', rule, { `, options: [{ alphabetize: { order: 'desc' } }], }), + // Option alphabetize: {order: 'desc'} and move nested import entries closer to the main import entry with file names having non-alphanumeric characters. + test({ + code: ` + import b from "foo-bar"; + import c from "foo,bar"; + import d from "foo/barfoo"; + import a from "foo";`, + options: [{ + alphabetize: { order: 'desc' }, + }], + }), // Option alphabetize with newlines-between: {order: 'asc', newlines-between: 'always'} test({ code: `