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 · 0 comments
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants