import-js/eslint-plugin-import

no-unused-modules needs an ignoreImports option

開放

#2,400 建立於 2022年3月8日

 (19 則留言) (4 個反應) (0 位負責人)JavaScript (1,548 個分叉)batch import
bughelp wanted

倉庫指標

星標
 (5,940 顆星)
PR 合併指標
 (PR 指標待抓取)

描述

While ignoreExports allows us to ignore unused exports for specific files, we need to be able to ignore used imports.

Why?

Let's assume there's three files:

- entry.js
- utility.js
- utility.spec.js

Both entry.js and utility.spec.js import from utility.js. Here, we'd want to include entry.js and utility.spec.js in ignoreExports since one is an entry point and the other is a test. We're ensured if both files stop importing from utility.js then the eslint rule will error, which is good!

The issue is when just entry.js stops importing from utility.js, which is what we want this rule to identify. Right now, the tests will keep utility.js from ever being identified as unused, even though practically it's unused by the application. I'd have to manually discover that the application isn't using utility.js. This will never happen; I'd assume that the change in entry.js would identify this for me, but I'd have to manually check if utility.spec.js is the only one importing it, which negates the value of this rule. If I could declare in options to ignoreImports from **/*.spec.*, then utility.js will be identified as unused, which would be good!.

Currently, there's no way to accomplish this. Including **/*.spec.* in ignoreExports, setting src to ['**/!(*.spec.js)'], or having an override where excludedFiles includes **/*.spec.* all do the same thing, which isn't what I want. We specifically need to identify files where we want to ignore their imports. The moment we write a unit test or a storybook story, we're essentially disabling the useful of this rule.

貢獻者指南