import-js / eslint-plugin-import

ESLint plugin with rules that help validate proper imports.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

ziserman opened this issue · comments

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?

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

@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.

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

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! :)

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

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

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