From de52c4be166db9850dce9ef51a4dcca672a07a86 Mon Sep 17 00:00:00 2001 From: pri1311 Date: Thu, 3 Mar 2022 17:30:16 +0530 Subject: [PATCH] move nested imports closer to main import entry --- src/rules/order.js | 36 +++++++++++++++++++++------ tests/src/rules/order.js | 54 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 7 deletions(-) diff --git a/src/rules/order.js b/src/rules/order.js index fce979f090..3cc706645b 100644 --- a/src/rules/order.js +++ b/src/rules/order.js @@ -1,5 +1,6 @@ 'use strict'; +import { min } from 'lodash'; import minimatch from 'minimatch'; import importType from '../core/importType'; import isStaticRequire from '../core/staticRequire'; @@ -244,14 +245,35 @@ function getSorter(ascending) { const multiplier = ascending ? 1 : -1; return function importsSorter(importA, importB) { - let result; - - if (importA < importB) { - result = -1; - } else if (importA > importB) { - result = 1; + let result = 0; + + if (!importA.includes('/') && !importB.includes('/')) { + if (importA < importB) { + result = -1; + } else if (importA > importB) { + result = 1; + } else { + result = 0; + } } else { - result = 0; + const A = importA.split('/'); + const B = importB.split('/'); + const a = A.length + const b = B.length + + for (var i = 0; i < 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; diff --git a/tests/src/rules/order.js b/tests/src/rules/order.js index e552c9a853..35cd1d99be 100644 --- a/tests/src/rules/order.js +++ b/tests/src/rules/order.js @@ -675,6 +675,39 @@ ruleTester.run('order', rule, { alphabetize: { order: 'desc' }, }], }), + // 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/bar"; + import d from "foo/barfoo"; + 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' }, + }], + }), + // 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' }, + }], + }), // Option alphabetize with newlines-between: {order: 'asc', newlines-between: 'always'} test({ code: ` @@ -2230,6 +2263,27 @@ ruleTester.run('order', rule, { message: '`bar` import should occur before import of `Bar`', }], }), + // Option alphabetize: {order: 'asc'} and move nested import entries closer to the main import entry + test({ + code: ` + import a from "foo"; + import b from "foo-bar"; + import c from "foo/bar"; + import d from "foo/barfoo"; + `, + options: [{ + alphabetize: { order: 'asc' }, + }], + output: ` + import a from "foo"; + import c from "foo/bar"; + import d from "foo/barfoo"; + import b from "foo-bar"; + `, + errors: [ + { message: '`foo-bar` import should occur after import of `foo/barfoo`'} + ] + }), // Option alphabetize {order: 'asc': caseInsensitive: true} test({ code: `