Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(import/order): do not compare first path segment for relative paths #2885

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/rules/order.js
Expand Up @@ -302,6 +302,8 @@ function getSorter(alphabetizeOptions) {
const b = B.length;

for (let i = 0; i < Math.min(a, b); i++) {
// Skip comparing the first path segment, if they are relative segments for both imports
if (i === 0 && ((A[i] === '.' || A[i] === '..') && (B[i] === '.' || B[i] === '..'))) { continue; }
result = compareString(A[i], B[i]);
if (result) { break; }
}
Expand Down
16 changes: 16 additions & 0 deletions tests/src/rules/order.js
Expand Up @@ -169,6 +169,22 @@ ruleTester.run('order', rule, {
['sibling', 'parent', 'external'],
] }],
}),
// Grouping import types and alphabetize
test({
code: `
import async from 'async';
import fs from 'fs';
import path from 'path';

import index from '.';
import relParent3 from '../';
import relParent1 from '../foo';
import sibling from './foo';
Comment on lines +179 to +182
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test confuses me. The default group order is has index last so why is it first here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this configuration ends up so that all other groups are grouped, so the imports are mingled together. Then they are sorted alphabetically, which puts this first.

That's also how it worked in 2.26.0, which is why I added it here. Ideally I would also sort it last, as that would be most logical - for that we can use either pathGroups and distinctGroups options, or your other PR.

`,
options: [{ groups: [
['builtin', 'external'],
], alphabetize: { order: 'asc', caseInsensitive: true } }],
}),
// Omitted types should implicitly be considered as the last type
test({
code: `
Expand Down