Skip to content

Commit

Permalink
import/order: leave more space in rankings for consecutive path groups
Browse files Browse the repository at this point in the history
  • Loading branch information
Pearce-Ropion committed Jul 24, 2022
1 parent 0ef8cba commit abbbf57
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}, {});
Expand All @@ -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);

Expand Down
68 changes: 68 additions & 0 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: new Array(11).fill('There should be at least one empty line between import groups'),
}),

// reorder fix cannot cross non import or require
test(withoutAutofixOutput({
Expand Down

0 comments on commit abbbf57

Please sign in to comment.