Skip to content

eslint doesn't catch some unused imports #694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kumarharsh opened this issue Jul 20, 2016 · 6 comments · Fixed by #704
Closed

eslint doesn't catch some unused imports #694

kumarharsh opened this issue Jul 20, 2016 · 6 comments · Fixed by #704
Labels

Comments

@kumarharsh
Copy link

When I'm importing a component (say Input) which has other subcomponents as it's properties (like Input.Placeholder, Input.Button, etc), and there is another different component imported by a same name (say Button), and then if I use <Input.Button> in my code then eslint doesn't tell that <Button> is unused.

Reproducible code:

import React from 'react';
import Button from './Button';
import Input from './Input';

export default class X extends React.Component {
  render() {
    return <Input.Button />;
  }
}

What should be expected?

The import Button from './Button'; line should be highlighted with a no-unused-vars error. You can see this if you just use <Input> in the render function instead of <Input.Button>.

@ljharb
Copy link
Member

ljharb commented Jul 20, 2016

no-unused-vars is a core eslint rule. The use of jsx here shouldn't interfere with it, but if it does, I'd say it might need filing as an issue directly on eslint?

@kumarharsh
Copy link
Author

Actually, I was in two minds where to file it, and I tried to reproduce the same with normal javascript, but can't get eslint to show the same behaviour...

For example, eslint does show proper errors in this case:

import Button from './Button';
import Input from './Input';

export function f() {
  return new Input.Button();
}

@lencioni
Copy link
Collaborator

This might be related to the jsx-uses-vars rule: https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-uses-vars.md

@ljharb
Copy link
Member

ljharb commented Jul 24, 2016

@kumarharsh do you see the same behavior when you enable the react/jsx-uses-vars rule?

@lencioni
Copy link
Collaborator

I believe I found the bug. PR coming soon.

lencioni added a commit to lencioni/eslint-plugin-react that referenced this issue Jul 24, 2016
When I'm importing a component (say Input) which has other subcomponents
as it's properties (like Input.Placeholder, Input.Button, etc), and
there is another different component imported by a same name (say
Button), and then if I use <Input.Button> in my code then eslint doesn't
tell that <Button> is unused.

Reproducible code:

```
import React from 'react';
import Button from './Button';
import Input from './Input';

export default class X extends React.Component {
  render() {
    return <Input.Button />;
  }
}
```

What should be expected?

The import Button from './Button'; line should be highlighted with a
no-unused-vars error. You can see this if you just use <Input> in the
render function instead of <Input.Button>.

This was happening because the rule was checking for all JSXIdentifiers,
which included the object properties. I fixed this by having it check
for JSXOpeningElements and then drilling down to get the variable name
used from there.

Fixes jsx-eslint#694
@kumarharsh
Copy link
Author

@ljharb yes, jsx-uses-vars is already set in eslintconfig. @lencioni's PR seems good :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

3 participants