From 795a9b17d66edfc23b56cfbbc47e86efcada2d5f Mon Sep 17 00:00:00 2001 From: Mihkel Eidast Date: Tue, 26 Sep 2023 14:13:04 +0300 Subject: [PATCH 1/2] fix(import/order): do not compare first sibling and parent path segment --- src/rules/order.js | 2 ++ tests/src/rules/order.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/rules/order.js b/src/rules/order.js index 44d25be63..b45d8557d 100644 --- a/src/rules/order.js +++ b/src/rules/order.js @@ -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; } } diff --git a/tests/src/rules/order.js b/tests/src/rules/order.js index a6a8735a6..223806af6 100644 --- a/tests/src/rules/order.js +++ b/tests/src/rules/order.js @@ -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'; + `, + options: [{ groups: [ + ['builtin', 'external'], + ], alphabetize: { order: 'asc', caseInsensitive: true } }], + }), // Omitted types should implicitly be considered as the last type test({ code: ` From 5e12ad0d252c42a81ee1e6cd44a0c12bdf51b777 Mon Sep 17 00:00:00 2001 From: Mihkel Eidast Date: Wed, 27 Sep 2023 10:09:34 +0300 Subject: [PATCH 2/2] fix: add and fix repro case from issue, --- src/rules/order.js | 6 +++++- tests/src/rules/order.js | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/rules/order.js b/src/rules/order.js index b45d8557d..19dc9edfa 100644 --- a/src/rules/order.js +++ b/src/rules/order.js @@ -303,7 +303,11 @@ function getSorter(alphabetizeOptions) { 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; } + if (i === 0 && ((A[i] === '.' || A[i] === '..') && (B[i] === '.' || B[i] === '..'))) { + // If one is sibling and the other parent import, no need to compare at all, since the paths belong in different groups + if (A[i] !== B[i]) { break; } + continue; + } result = compareString(A[i], B[i]); if (result) { break; } } diff --git a/tests/src/rules/order.js b/tests/src/rules/order.js index 223806af6..c2d659f83 100644 --- a/tests/src/rules/order.js +++ b/tests/src/rules/order.js @@ -185,6 +185,18 @@ ruleTester.run('order', rule, { ['builtin', 'external'], ], alphabetize: { order: 'asc', caseInsensitive: true } }], }), + test({ + code: ` + import { fooz } from '../baz.js' + import { foo } from './bar.js' + `, + options: [{ + alphabetize: { order: 'asc', caseInsensitive: true }, + groups: ['builtin', 'external', 'internal', ['parent', 'sibling', 'index'], 'object'], + 'newlines-between': 'always', + warnOnUnassignedImports: true, + }], + }), // Omitted types should implicitly be considered as the last type test({ code: `