import-js/eslint-plugin-import

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

Open

#2,976 opened on Mar 6, 2024

View on GitHub
 (7 comments) (2 reactions) (0 assignees)JavaScript (1,540 forks)batch import
enhancementhelp wanted

Repository metrics

Stars
 (4,946 stars)
PR merge metrics
 (Avg merge 103d 23h) (3 merged PRs in 30d)

Description

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?

Contributor guide