From 7a37f9096a35694aeb45458f1baa7d2abccb0b2a Mon Sep 17 00:00:00 2001 From: Pearce Date: Sat, 23 Jul 2022 14:15:37 -0700 Subject: [PATCH] [Fix] `order`: leave more space in rankings for consecutive path groups Fixes #2494. --- CHANGELOG.md | 3 ++ src/rules/order.js | 4 +-- tests/src/rules/order.js | 68 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 01cf2e69b..3102fc869 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange - [`no-restricted-paths`]: fix an error message ([#2466], thanks [@AdriAt360]) - [`no-restricted-paths`]: use `Minimatch.match` instead of `minimatch` to comply with Windows Native paths ([#2466], thanks [@AdriAt360]) - [`order`]: require with member expression could not be fixed if alphabetize.order was used ([#2490], thanks [@msvab]) +- [`order`]: leave more space in rankings for consecutive path groups ([#2506], thanks [@Pearce-Ropion]) ### Changed - [Tests] `named`: Run all TypeScript test ([#2427], thanks [@ProdigySim]) @@ -997,6 +998,7 @@ for info on changes for earlier releases. [`memo-parser`]: ./memo-parser/README.md +[#2506]: https://github.com/import-js/eslint-plugin-import/pull/2506 [#2503]: https://github.com/import-js/eslint-plugin-import/pull/2503 [#2490]: https://github.com/import-js/eslint-plugin-import/pull/2490 [#2466]: https://github.com/import-js/eslint-plugin-import/pull/2466 @@ -1657,6 +1659,7 @@ for info on changes for earlier releases. [@panrafal]: https://github.com/panrafal [@paztis]: https://github.com/paztis [@pcorpet]: https://github.com/pcorpet +[@Pearce-Ropion]: https://github.com/Pearce-Ropion [@Pessimistress]: https://github.com/Pessimistress [@pmcelhaney]: https://github.com/pmcelhaney [@preco21]: https://github.com/preco21 diff --git a/src/rules/order.js b/src/rules/order.js index 3f033eb82..4fb178768 100644 --- a/src/rules/order.js +++ b/src/rules/order.js @@ -407,7 +407,7 @@ function convertGroupsToRanks(groups) { if (res[groupItem] !== undefined) { throw new Error('Incorrect configuration of the rule: `' + groupItem + '` is duplicated'); } - res[groupItem] = index; + res[groupItem] = index * 2; }); return res; }, {}); @@ -417,7 +417,7 @@ function convertGroupsToRanks(groups) { }); const ranks = omittedTypes.reduce(function (res, type) { - res[type] = groups.length; + res[type] = groups.length * 2; return res; }, rankObject); diff --git a/tests/src/rules/order.js b/tests/src/rules/order.js index e8a5143c5..ed4879ac4 100644 --- a/tests/src/rules/order.js +++ b/tests/src/rules/order.js @@ -2099,6 +2099,74 @@ ruleTester.run('order', rule, { }, ], }), + test({ + code: ` + import path from 'path'; + import { namespace } from '@namespace'; + import { a } from 'a'; + import { b } from 'b'; + import { c } from 'c'; + import { d } from 'd'; + import { e } from 'e'; + import { f } from 'f'; + import { g } from 'g'; + import { h } from 'h'; + import { i } from 'i'; + import { j } from 'j'; + import { k } from 'k';`, + output: ` + import path from 'path'; + + import { namespace } from '@namespace'; + + import { a } from 'a'; + + import { b } from 'b'; + + import { c } from 'c'; + + import { d } from 'd'; + + import { e } from 'e'; + + import { f } from 'f'; + + import { g } from 'g'; + + import { h } from 'h'; + + import { i } from 'i'; + + import { j } from 'j'; + import { k } from 'k';`, + options: [ + { + groups: [ + 'builtin', + 'external', + 'internal', + ], + pathGroups: [ + { pattern: '@namespace', group: 'external', position: 'after' }, + { pattern: 'a', group: 'internal', position: 'before' }, + { pattern: 'b', group: 'internal', position: 'before' }, + { pattern: 'c', group: 'internal', position: 'before' }, + { pattern: 'd', group: 'internal', position: 'before' }, + { pattern: 'e', group: 'internal', position: 'before' }, + { pattern: 'f', group: 'internal', position: 'before' }, + { pattern: 'g', group: 'internal', position: 'before' }, + { pattern: 'h', group: 'internal', position: 'before' }, + { pattern: 'i', group: 'internal', position: 'before' }, + ], + 'newlines-between': 'always', + pathGroupsExcludedImportTypes: ['builtin'], + }, + ], + settings: { + 'import/internal-regex': '^(a|b|c|d|e|f|g|h|i|j|k)(\\/|$)', + }, + errors: Array.from({ length: 11 }, () => 'There should be at least one empty line between import groups'), + }), // reorder fix cannot cross non import or require test(withoutAutofixOutput({