Skip to content

[import/order] Regression upgrading from 2.26.0 to 2.27.1+ #2669

@MarcAndre-Rivet

Description

@MarcAndre-Rivet

I have the following setup and code experiencing a linting issue with 2.27.1 but not 2.26.0 for the import/order rule and what I think is the root issue.

.eslintrc fragment:

    'import/order': [
        2,
        {
            alphabetize: {
                order: 'asc',
                caseInsensitive: true
            },
            'newlines-between': 'always'
        }
    ],

tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "esModuleInterop": true,
        "declaration": true,
        "removeComments": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "allowSyntheticDefaultImports": true,
        "strictBindCallApply": true,
        "strictNullChecks": true,
        "target": "es2022",
        "sourceMap": true,
        "outDir": "./dist",
        "baseUrl": "./",
        "incremental": true,
        "skipLibCheck": true,
        "paths": {
            "@core/*": ["core/src/*"],
            "@schedule/*": ["schedule/src/*"],
        }
    }
}

I have a TS file that has the following structure:

import CoreA from '@core/A';
import CoreB from '@core/B';
import CoreC from '@core/C';

import PackageA from '@schedule/A';
import PackageB from '@schedule/B';
import PackageC from '@schedule/C';
import PackageD from '@schedule/D';
import PackageE from '@schedule/E';
import PackageF from '@schedule/F';
import PackageG from '@schedule/G';

export default () => {};

Interestingly enough this error only occurs once there is 10+ imports in the file, no `import/order' error occurs otherwise.

Digging into the rule's code, it seems the issue comes from a default .sort() that implicitly cast numbers into strings for ordering and will order 10 before 2 (in my case).

This results into this ranking assignment:

{
  '@schedule/A|value': 10,
  '@schedule/B|value': 11,
  '@schedule/C|value': 12,
  '@schedule/D|value': 13,
  '@schedule/E|value': 14,
  '@schedule/F|value': 15,
  '@schedule/G|value': 16,
  '@core/A|value': 9, // smaller than previous item, causing error reporting
  '@core/B|value': 10,
  '@core/C|value': 11
}

Changing it to .sort((lhs, rhs) => lhs - rhs) seems to resolve the issue although ranking has gaps (not sure this could cause issues elsewhere).

This results in this ranking:

{
  '@core/A|value': 2,
  '@core/B|value': 3,
  '@core/C|value': 4,
  '@schedule/A|value': 13,
  '@schedule/B|value': 14,
  '@schedule/C|value': 15,
  '@schedule/D|value': 16,
  '@schedule/E|value': 17,
  '@schedule/F|value': 18,
  '@schedule/G|value': 19
}

Let me know if you know more info/config to repro 😄

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions