mskelton / eslint-plugin-sort

Auto-fixable sort rules for ESLint.
https://www.npmjs.com/package/eslint-plugin-sort
ISC License
24 stars 1 forks source link

[sort/imports] Bug: relative paths more than one layer sorted out of order #62

Closed abannachGrafana closed 3 months ago

abannachGrafana commented 3 months ago

Description

Relative paths greater than one layer deep e.g. ../../some/path imports are not sorted with other relative path imports.

Example config

"sort/imports": [
      "error",
      {
        "groups": [
          { "regex": "^react$", "order": 10 },
          { "type": "dependency", "order": 15 },
          { "type": "side-effect", "order": 20 },
          { "regex": "^@grafana", "order": 30 },
          { "regex": "^.+\\.s?css$", "order": 50 },
          { "type": "other", "order": 40 }
        ],
        "separator": "\n"
      }
    ],

Example sorted imports

import currentRules from '../../jest/data/current-rules.json'; // outside of source dir where alias does not work
import exemptions from '../../jest/data/exemptions.json';
import labels from '../../jest/data/labels.json';
import metrics from '../../jest/data/metrics.json';
import recommendations from '../../jest/data/recommendations.json';
import recommendationsConfig from '../../jest/data/recommendations-config.json';
import recommendationsVerbose from '../../jest/data/recommendations-verbose.json';
import { rest } from 'msw'; // normal dependency

import { paths } from '@/util/constants'; // path alias

Example sorted imports with single layer relative

import currentRules from '../../jest/data/current-rules.json';
import exemptions from '../../jest/data/exemptions.json';
import labels from '../../jest/data/labels.json';
import metrics from '../../jest/data/metrics.json';
import recommendations from '../../jest/data/recommendations.json';
import recommendationsVerbose from '../../jest/data/recommendations-verbose.json';
import { rest } from 'msw';

import recommendationsConfig from '../jest/data/recommendations-config.json';
import { paths } from '@/util/constants';

Expected behavior

mskelton commented 3 months ago

@abannachGrafana I tried reproducing this but couldn't. Could you setup a codesandbox that reproduces it?

abannachGrafana commented 3 months ago

I'll see what I can do 👍

abannachGrafana commented 3 months ago

Whoop! Was able to recreate with a simple example and multiple file locations https://codesandbox.io/p/github/abannachGrafana/eslint-sort-repro/draft/agitated-rosalind

abannachGrafana commented 3 months ago

Also I just created a public repo in GitHub that recreates so you could just check it out if you want https://github.com/abannachGrafana/eslint-sort-repro

mskelton commented 3 months ago

@abannachGrafana So, this happens in instances where you have an import with a relative path that is resolvable by the isomorphic-resolve package in node_modules. In your reproduction repo, this happened with ../../jest... which was resolvable since isomorphic-resolve was 2 levels deep and thus could resolve the relative path, and incorrectly assumed it was a dependency. The solution here is to detect relative imports and always flag those as non-dependencies before attempting to call require.resolve.

github-actions[bot] commented 3 months ago

:tada: This issue has been resolved in version 3.0.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket: