import-js/eslint-plugin-import

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

Open

#1,745 opened on May 3, 2020

View on GitHub
 (10 comments) (0 reactions) (0 assignees)JavaScript (1,540 forks)batch import
bughelp wanted

Repository metrics

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

Description

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.

Contributor guide