import-js/eslint-plugin-import

[no-unused-modules] unusedExports false positives in multi-line imports and multiple files

Open

#1745 aperta il 3 mag 2020

Vedi su GitHub
 (10 commenti) (0 reazioni) (0 assegnatari)JavaScript (1540 fork)batch import
bughelp wanted

Metriche repository

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

Descrizione

First of all, let me say how much I appreciate all the hard work that went into this plugin, and how helpful it is in all of my projects. Now, to the issue at hand.

If I turn on the import/no-unused-modules rule, with the unusedExports option set to true, I get false positives for unused exports when I have multi-line imports from that file via an interim file.

In my main repo, where this issue first happened, the use case is in a React application, a component directory has a components sub-directory, with an index file exporting different helper components from different directories within that directory. When I have too many components exported from that components sub-directory, I like to split the import statement into two (or more), instead of having a single long line, or several very short ones, each with a single name of a component imported (I use prettier in that repo as well). In such cases, ESLint warns me about unused exports from the second import statement and onwards.

To simplify matters, if for example I have in foo.js:

export const foo = 1;
export const bar = 'baz';

and in its sibling bar.js:

export { foo, bar } from './foo.js';

and finally in baz.js:

import { foo } from './foo.js';
import { bar } from './foo.js';

console.log(foo, bar);

ESLint warns me in line 1 of bar.js about unused exports - exported declaration 'bar' not used within other modules import/no-unused-modules.

A reproduction can be found in https://github.com/roikoren755/eslint-plugin-import-bug-reproduction. To see the bug simply clone the repo, run yarn and yarn lint, and voila, an error message will appear, even though it shouldn't.

Guida contributor