Eslint-plugin-react: jsx-uses-vars and no-unused-vars causes false warning

Created on 5 Oct 2018  路  4Comments  路  Source: yannickcr/eslint-plugin-react

I'm filing this one here, though it could be something that will need followup elsewhere.

[email protected]
[email protected]

Given the following code:

import React from 'react';
import foo from 'foo';

export const AccessibilityDecorator = foo((config, Wrapped) => {
    return class t extends React.Component {
        render = () => {
            return <Wrapped />;
        }
    };
});

And the following .eslintconfig.json:

{
    "parser": "babel-eslint",
    "plugins": [
        "babel",
        "react"
    ],
    "extends": ["plugin:react/recommended"],
    "rules": {
        "no-unused-vars": 1
    }
}

The following warning is given:

  4:44  warning  'config' is defined but never used  no-unused-vars

Expected output: No warning.

The default behavior of eslint is to only warn on unused parameters that come _after_ the last used parameter. jsx-uses-vars flags Wrapped as used but it leaves config marked as unused. Should this be picked up automatically by eslint? Or, will this require some heroics on the part of the react plugin to find these and fix them?

eslint

Most helpful comment

It was just a minimal sample, that's all. It's not reflective of any actual code. I will cross-file this to eslint.

All 4 comments

Why on earth is render a class field instead of an instance method?

Separately, I would expect config to be left marked as unused, because it is unused - the default settings for no-unused-vars, for the "args" setting is indeed after-used, but I would assume this is something eslint can figure out.

If it's not figuring it out, then I think that's a bug in no-unused-vars - specifically, it might have to wait to do its checking on program exit to ensure all the variables have time to be marked as used.

It was just a minimal sample, that's all. It's not reflective of any actual code. I will cross-file this to eslint.

fwiw, I made a PR with eslint that fixes this issue, so I'll close this.

Was this page helpful?
0 / 5 - 0 ratings