Skip to content

Commit

Permalink
add fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pri1311 committed Mar 3, 2022
1 parent 584d38a commit 2b1a911
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 39 deletions.
38 changes: 11 additions & 27 deletions src/rules/order.js
Expand Up @@ -246,33 +246,17 @@ function getSorter(ascending) {
return function importsSorter(importA, importB) {
let result = 0;

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 (let 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;
}
const A = importA.split('/');
const B = importB.split('/');
const a = A.length;
const b = B.length;

if (A < B) {
result = -1;
} else if (A > B) {
result = 1;
} else if (a !== b) {
result = a < b ? -1 : 1;
}

return result * multiplier;
Expand Down
21 changes: 9 additions & 12 deletions tests/src/rules/order.js
Expand Up @@ -681,32 +681,29 @@ ruleTester.run('order', rule, {
import a from "foo";
import c from "foo/bar";
import d from "foo/barfoo";
import b from "foo-bar";`,
options: [{
alphabetize: { order: 'asc' },
}],
import b from "foo-bar";
`,
options: [{ alphabetize: { order: 'asc' } }],
}),
// Option alphabetize: {order: 'asc'} and move nested import entries closer to the main import entry
test({
code: `
import a from "foo";
import c from "foo/foobar/bar";
import d from "foo/foobar/barfoo";
import b from "foo-bar";`,
options: [{
alphabetize: { order: 'asc' },
}],
import b from "foo-bar";
`,
options: [{ alphabetize: { order: 'asc' } }],
}),
// Option alphabetize: {order: 'desc'} and move nested import entries closer to the main import entry
test({
code: `
import b from "foo-bar";
import d from "foo/barfoo";
import c from "foo/bar";
import a from "foo";`,
options: [{
alphabetize: { order: 'desc' },
}],
import a from "foo";
`,
options: [{ alphabetize: { order: 'desc' } }],
}),
// Option alphabetize with newlines-between: {order: 'asc', newlines-between: 'always'}
test({
Expand Down

0 comments on commit 2b1a911

Please sign in to comment.