Skip to content

Commit

Permalink
Fallback to the first solution
Browse files Browse the repository at this point in the history
  • Loading branch information
pri1311 committed Mar 18, 2022
1 parent 126d094 commit d51eb51
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/rules/order.js
Expand Up @@ -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;
};
Expand Down
11 changes: 11 additions & 0 deletions tests/src/rules/order.js
Expand Up @@ -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: `
Expand Down

0 comments on commit d51eb51

Please sign in to comment.