Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: no-extraneous-import does not work correctly with tsconfig.json paths that start with ~ or @ #379

Open
1 task done
MorevM opened this issue Nov 3, 2024 · 4 comments · May be fixed by #408
Open
1 task done
Labels

Comments

@MorevM
Copy link

MorevM commented Nov 3, 2024

Environment

It probably doesn't matter, but

Node version: 20.9.0
npm version: 10.1.0
ESLint version: 9.14.0
eslint-plugin-n version: 17.12.0
Operating System: Windows 11

What rule do you want to report?

n/no-extraneous-import

Link to Minimal Reproducible Example

https://github.com/MorevM/eslint-plugin-n-no-extraneous-import-problem

What did you expect to happen?

Check the repository - the rule warns about extraneous import, although according to the path defined in the tsconfig.json this is an internal import.

This only happens for paths that contain the ~ or @ character at the beginning.
E.g. alias #configurations/* is working as expected, but ~configurations/* is not.

The reason (probably) is that such paths are interpreted as npm modules here, leading to the determination of the module name here, which in turn makes the report condition truthy here.

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

I'm not sure the best way to solve this and would be happy if someone more familiar with the architecture of the project could suggest how to do it better - I can form a PR.

I'd probably do it this way:

  1. In the class ImportTarget, move the resolverConfig definition from the getFilePath method into a separate method (this is where the aliases are defined that need to be accounted for).
  2. In the getModuleType, check the name against resolverConfig.alias (if any) before the RegExp that checks for npm modules.

This will solve the problem.
If someone with the right permissions confirms that this is indeed the problem and that the solution is appropriate, I will make a PR with the fix.

@MorevM MorevM added the bug label Nov 3, 2024
@scagood scagood added typescript rule:update An update to a current rule help wanted labels Nov 4, 2024
@OmarJaroudi
Copy link

Hello, any updates on this?

@MorevM
Copy link
Author

MorevM commented Feb 13, 2025

I'm still willing to fix this if someone more familiar with the codebase confirms that my proposed solution will work, or suggests a better alternative solution :)

@aladdin-add
Copy link

@MorevM , thansk! it seems a good direction to fix. Please feel free to send a PR! 😄 (and sorry for the late!)

@lsj162
Copy link

lsj162 commented Feb 15, 2025

Added: If you're using TypeScript, you may want to ignore type imports. The description in the document is invalid, and it is recommended to update it synchronously.

{
    "rules": {
        "n/no-missing-import": ["error", {
            "ignoreTypeImport": true
        }]
    }
}

⬇️
https://github.com/eslint-community/eslint-plugin-n/blob/master/docs/rules/no-missing-import.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
5 participants