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

no-unused-modules: ignore imports in files listed in ignoreExports #2976

Open
ziserman opened this issue Mar 6, 2024 · 7 comments
Open

no-unused-modules: ignore imports in files listed in ignoreExports #2976

ziserman opened this issue Mar 6, 2024 · 7 comments

Comments

@ziserman
Copy link

ziserman commented Mar 6, 2024

Hello,

I've faced with a problem that it's impossible to warn about unused modules in case the module has imports only to test files, for instance.

Example

Suppose, we have a simple file structure like:

.
├── src/
│   └── do-something/
│       ├── do-something.js
│       └── do-something.test.js
└── .eslintrc.js

do-something.js imported only inside do-something.test.js for tests, obviously.

At the same time files with name *.test.js are ignored by import/no-unused-modules rule in .eslintrc.js:

// .eslintrc.js
module.exports = {
  ...
  settings: {
    "import/extensions": [".js"]
  },
  plugins: ['import'],
  rules: {
    'import/no-unused-modules': [
      2,
      {
        src: ['src'],
        unusedExports: true,
        ignoreExports: ['**/*.test.js'],
      },
    ],
  },
}

Finally, do-something.js has no imports anywhere, except in ignored file with tests of itself. So, it's better to delete do-something.js file and everything related to it.

Is there any way to do something like that in the current version?

@ljharb
Copy link
Member

ljharb commented Mar 6, 2024

Colocation definitely makes this trickier - however, can you try to set src to ['src', '!src/**/*.test.js']?

@ziserman
Copy link
Author

ziserman commented Mar 6, 2024

@ljharb yes, I tried and got an error

Error: Error while loading rule 'import/no-unused-modules': No files matching '!src/**/*.test.js' were found.

I think negation isn't working in src at all.

@ljharb
Copy link
Member

ljharb commented Mar 6, 2024

aha, perhaps that's the request. I wouldn't have thought of the need because I think colocation of tests is a bad idea :-)

@ziserman
Copy link
Author

ziserman commented Mar 6, 2024

Actually, it doesn't matter where you put your tests (or something else), cause ignoring hypothetical tests folder do nothing. The issue with the imports in ignored files, the plugin see them and doesn't throw any warn, if I got your message correctly.

Thank you for labels! :)

@ljharb
Copy link
Member

ljharb commented Mar 6, 2024

if the test files aren't in src (because they're in test) i don't think the problem will come up.

@ziserman
Copy link
Author

ziserman commented Mar 6, 2024

it is, I checked it in console, for sure

and here's the screenshot of simple repo
SCR-20240307-cdia

*typo at the screenshot: no warn or error line below the export

@ljharb
Copy link
Member

ljharb commented Mar 6, 2024

hm, interesting, ok - then this becomes much more important.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants