import-js / eslint-plugin-import

ESLint plugin with rules that help validate proper imports.
MIT License
5.55k stars 1.56k forks source link

fix long imports by exchanging with alias/paths from tsconfig #3007

Open LeLunZ opened 6 months ago

LeLunZ commented 6 months ago

Hey.

I am currently working on an nx project. Where in the tsconfig something like that is configured:

    "compilerOptions": {
        "paths": {
            "@auth/feature-auth": ["libs/auth/feature-auth/src/index.ts"],
            "@employee-view/api": ["libs/employee-view/api/src/index.ts"],
            "@employee-view/shared": ["libs/employee-view/shared/src/index.ts"],
            "@shared/map": ["libs/shared/map/src/index.ts"],
            "@ui-library/*": ["libs/ui-library/src/lib/*"],
        }
    }

Then in the code somewhere in the project there is an import like:

import { ZipCodeUtilsService } from '../../../../../shared/map/src/lib/services/zip-code-utils.service';

Which could be shortened to

import { ZipCodeUtilsService } from '@shared/map';

I guess I want something similar to what my IDE is doing. Currently my IDE (WebStorm) shows a suggestion like that, but I guess thats WebStorm specific. And I can't run eslint --fix to do that: Screenshot 2024-05-14 at 13 03 43

Is that already possible with a eslint rule or is that a possible improvement to this package? (I guess it would fit into import/no-useless-path-segments)

ljharb commented 5 months ago

no-useless-path-segments can only check relative filesystem paths. There's no good way to be sure what aliases might be doing and how they can be altered in a reliable way, that I can think of.

WebStorm's suggestions could be made as eslint rule suggestions, but not as an autofix. Additionally, that'd be a rule option that only applied when using TS, so I'm not sure it's a good fit for this plugin.

Does the TS-eslint plugin not have a rule that can cover this?