Eslint-plugin-import: [no-unused-modules] unusedExports false posivitve when import from index.js used by component with .jsx extension

Created on 24 Jun 2020  ยท  5Comments  ยท  Source: benmosher/eslint-plugin-import

I created minimal repo https://github.com/ferogot/eslint-plugin-import-bug-reproduction/tree/jsx_extension_problem

.
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ .eslintrc
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ eslinttest
โ”‚   โ”œโ”€โ”€ Component
โ”‚   โ”‚   โ”œโ”€โ”€ Component.jsx
โ”‚   โ”‚   โ””โ”€โ”€ index.js
โ”‚   โ”œโ”€โ”€ use_default_index
โ”‚   โ”‚   โ””โ”€โ”€ Comp2.jsx
โ”‚   โ””โ”€โ”€ index.js

files content is:
eslinttest/Component/Components.jsx

import React from 'react';
export function Component(props){
  return (<></>);
}

eslinttest/Component/index.js

export {Component} from './Component';

eslinttest/use_default_index/Comp2.jsx

import React from 'react';
import {Component} from '../Component/index';

function Comp2(props){
  return (<Component />);
}

export default Comp2;

eslinttest/index.js

import React from 'react';
import ReactDom from 'react-dom';
import Comp2 from './use_default_index/Comp2';

ReactDom.render(
  <Comp2 />,
  document.getElementById('root'),
);

And eslint config file:

{
    "env": {
        "browser": true,
        "es2020": true
    },
    "parser": "babel-eslint",
    "extends": [
        "eslint:recommended"
    ],
    "parserOptions": {
        "ecmaFeatures": {
            "jsx": true
        },
        "ecmaVersion": 11,
        "sourceType": "module"
    },
    "plugins": [
        "react",
        "import"
    ],
    "rules": {
        "no-unused-vars": "off",
        "react/jsx-uses-react": "error",
        "react/jsx-uses-vars": "error",
        "import/no-extraneous-dependencies": 2,
        "import/no-unresolved": 1,
        "import/no-unused-modules": [1, {"unusedExports": true}]
    },
    "settings": {
        "import/resolver": {
            "node": {
                "extensions" : [".js", ".jsx"],
                "moduleDirectory": ["node_modules", "src/"]
            }
        },
        "import/parser": "babel-eslint"
    }
}

So when i `eslint ./eslinttest --ext .js,.jsx' i get following:

> eslint ./eslinttest --ext .js,.jsx

/home/stranger/repo/eslint-plugin-import-bug-reproduction/min-app/eslinttest/Component/index.js
  1:1  warning  exported declaration 'Component' not used within other modules  import/no-unused-modules

โœ– 1 problem (0 errors, 1 warning)

But if i change Comp2.jsx into Comp2.js i get no warnings anymore!
Thats strange. Is my eslint config right? I added .js and .jsx extensions everywhere which i could.

I need help with this problem, in my project i have same behavior and want add this rule with error level to CI.

question

All 5 comments

Ah, i forgot about versions. In minimal repo it is:

     "eslint": "^7.3.1",
     "eslint-plugin-import": "^2.21.2"

And i got same problem with:

    "eslint": "^6.8.0",
    "eslint-plugin-import": "^2.21.2"

If you try with the default branch of this plugin, is the issue already fixed?

@ljharb i updated plugin to 2.22.0 (it is equal to master branch for now), and issue still reproduce

Is there an update on this? I am currently facing an issue with false positives when exports in .js files are imported into .tsx (and possibly .ts) files, and was wondering if this might be the same as the issue at hand.

Edit:
Turns out I forgot to add extends: ["plugin:import/typescript"] to my eslintrc file ๐Ÿ˜…

@ferogot you're missing jsx in https://github.com/benmosher/eslint-plugin-import#importextensions. you can get that for free by extending plugin:import/typescript.

When I edit your repro repo's eslint config to also extend that config, everything works fine. Closing as resolved.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ljharb picture ljharb  ยท  29Comments

thewilkybarkid picture thewilkybarkid  ยท  36Comments

love-intent picture love-intent  ยท  80Comments

johanneswilm picture johanneswilm  ยท  21Comments

ThomasdenH picture ThomasdenH  ยท  31Comments