import-js/eslint-plugin-import

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

Open

#2976 aperta il 6 mar 2024

Vedi su GitHub
 (7 commenti) (2 reazioni) (0 assegnatari)JavaScript (1540 fork)batch import
enhancementhelp wanted

Metriche repository

Star
 (4946 star)
Metriche merge PR
 (Merge medio 138g 22h) (3 PR mergiate in 30 g)

Descrizione

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?

Guida contributor