Skip to content

import/order does not respect consecutive pathGroups when 9 or 10 groups are defined #2494

Closed
@Pearce-Ropion

Description

@Pearce-Ropion

Import types can be assigned the same rank when using 1 or more after patterns and 9-10 before patterns on consecutive import types.

Repro

Given the following config

module.exports = {
  parser: '@babel/eslint-parser',
  parserOptions: {
    requireConfigFile: false,
  },
  settings: {
    'import/internal-regex': '^(a|b|c|d|e|f|g|h|i|j|k|l|m)(\\/|$)',
  },
  plugins: ['import'],
  rules: {
    'import/order': [
      'error',
      {
        groups: ['builtin', 'external', 'internal', 'parent', ['sibling', 'index']],
        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' },
        ],
        pathGroupsExcludedImportTypes: ['builtin'],
        'newlines-between': 'always',
      },
    ],
  },
};

The above config will result in the following file. With the 9 before patterns, a gets grouped with the @namespace imports. This doesn't happen with 8 or 11 before patterns.

// Notice how `import { a } from 'a'` gets grouped with the `@namespace` imports.

import React from 'react';

import { bar } from '@namespace/bar';
import { foo } from '@namespace/foo';
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';
import { l } from 'l';
import { m } from 'm';

import parent from '../parent';

import sibling from './sibling';

The expected output with 9 before patterns should be :

import React from 'react';

import { bar } from '@namespace/bar';
import { foo } from '@namespace/foo';

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';
import { l } from 'l';
import { m } from 'm';

import parent from '../parent';

import sibling from './sibling';

Similarly, with the 10 before patterns, a gets grouped with the external imports. This doesn't happen with 8 or 11 before patterns.

// Notice how `import { a } from 'a'` gets grouped with the `external` imports.

import React from 'react';
import { a } from 'a';

import { bar } from '@namespace/bar';
import { foo } from '@namespace/foo';
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';
import { l } from 'l';
import { m } from 'm';

import parent from '../parent';

import sibling from './sibling';

I also tried logging the generated rankings from computeRank for each amount of before patterns.

8 pathGroups
react external 1
@namespace/bar external 1.1
@namespace/foo external 1.1
a internal 1.2
b internal 1.3
c internal 1.4
d internal 1.5
e internal 1.6
f internal 1.7
g internal 1.8
h internal 1.9
i internal 2
j internal 2
k internal 2
l internal 2
m internal 2
../parent parent 3
./sibling sibling 4

9 pathGroups
react external 1
@namespace/bar external 1.1
@namespace/foo external 1.1
a internal 1.1
b internal 1.2
c internal 1.3
d internal 1.4
e internal 1.5
f internal 1.6
g internal 1.7
h internal 1.8
i internal 1.9
j internal 2
k internal 2
l internal 2
m internal 2
../parent parent 3
./sibling sibling 4
10 pathGroups
a internal 1
react external 1
@namespace/bar external 1.1
@namespace/foo external 1.1
b internal 1.1
c internal 1.2
d internal 1.3
e internal 1.4
f internal 1.5
g internal 1.6
h internal 1.7
i internal 1.8
j internal 1.9
k internal 2
l internal 2
m internal 2
../parent parent 3
./sibling sibling 4
11 pathGroups
react external 1
@namespace/bar external 1.01
@namespace/foo external 1.01
a internal 1.89
b internal 1.9
c internal 1.91
d internal 1.92
e internal 1.93
f internal 1.94
g internal 1.95
h internal 1.96
i internal 1.97
j internal 1.98
k internal 1.99
l internal 2
m internal 2
../parent parent 3
./sibling sibling 4

I had a lot of trouble coming up with a generalized description for this since its a very specific issue.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions