From 8acf528613553d4cd7377b84665c9ffb3ae436d8 Mon Sep 17 00:00:00 2001 From: Mihkel Eidast Date: Tue, 26 Sep 2023 14:03:53 +0300 Subject: [PATCH] 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 44d25be63c..b45d8557d0 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 a6a8735a6f..223806af6f 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: `