Skip to content

no-unused-prop-types crashes when using a setState updater with no second param defined #1685

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
gbedardsiceupgrade opened this issue Feb 9, 2018 · 6 comments

Comments

@gbedardsiceupgrade
Copy link

gbedardsiceupgrade commented Feb 9, 2018

When using setState with an arrow function like so:

this.setState((prevState) => ({ count: prevState.count + 1 });

The plugin crashes with the following stack trace:

[Error - 11:49:56 AM] TypeError: Cannot read property 'properties' of undefined
    at markPropTypesAsUsed (/path/to/project/node_modules/eslint-plugin-react/lib/rules/no-unused-prop-types.js:658:40)
    at handleSetStateUpdater (/path/to/project/node_modules/eslint-plugin-react/lib/rules/no-unused-prop-types.js:955:7)
    at Object.handleFunctionLikeExpressions (/path/to/project/node_modules/eslint-plugin-react/lib/rules/no-unused-prop-types.js:964:7)
    at updatedRuleInstructions.(anonymous function) (/path/to/project/node_modules/eslint-plugin-react/lib/util/Components.js:654:75)
    at listeners.(anonymous function).forEach.listener (/path/to/project/node_modules/eslint/lib/util/safe-emitter.js:47:58)
    at Array.forEach (native)
    at Object.emit (/path/to/project/node_modules/eslint/lib/util/safe-emitter.js:47:38)
    at NodeEventGenerator.applySelector (/path/to/project/node_modules/eslint/lib/util/node-event-generator.js:251:26)
    at NodeEventGenerator.applySelectors (/path/to/project/node_modules/eslint/lib/util/node-event-generator.js:280:22)
    at NodeEventGenerator.enterNode (/path/to/project/node_modules/eslint/lib/util/node-event-generator.js:294:14)
    at CodePathAnalyzer.enterNode (/path/to/project/node_modules/eslint/lib/code-path-analysis/code-path-analyzer.js:608:23)
    at Traverser.enter (/path/to/project/node_modules/eslint/lib/linter.js:959:32)

The plugin seems to assume that the second props param will be always defined on the function, when it actually won't always be the case.

@koba04
Copy link

koba04 commented Feb 20, 2018

@gbedardsiceupgrade Do you have an example code to reproduce this?

ljharb added a commit that referenced this issue Feb 20, 2018
@ljharb
Copy link
Member

ljharb commented Feb 20, 2018

I added a passing test linked to this issue; we’d definitely need a repro case.

@gbedardsiceupgrade can you try to reproduce this on v7.7.0?

@gbedardsiceupgrade
Copy link
Author

Ah, looks like updating from 7.5.0 to 7.7.0 fixes it. Thanks for the awesome plugin! Closing.

@Leonard-Li777
Copy link

I updated to 7.10.0 still get the error!

prettier-eslint [ERROR]: eslint fix failed due to an eslint error
prettier-eslint-cli [ERROR]: There was an error formatting "/Users/lilun/workspace/supply-scm/src/layout/components/index.jsx":
TypeError: Cannot read property 'properties' of undefined
at markPropTypesAsUsed (/Users/lilun/workspace/supply-scm/node_modules/eslint-plugin-react/lib/rules/no-unused-prop-types.js:670:41)
at markDestructuredFunctionArgumentsAsUsed (/Users/lilun/workspace/supply-scm/node_modules/eslint-plugin-react/lib/rules/no-unused-prop-types.js:953:9)
at Object.handleFunctionLikeExpressions (/Users/lilun/workspace/supply-scm/node_modules/eslint-plugin-react/lib/rules/no-unused-prop-types.js:982:7)
at updatedRuleInstructions.(anonymous function) (/Users/lilun/workspace/supply-scm/node_modules/eslint-plugin-react/lib/util/Components.js:698:75)
at listeners.(anonymous function).forEach.listener (/Users/lilun/workspace/supply-scm/node_modules/eslint/lib/util/safe-emitter.js:47:58)
at Array.forEach ()
at Object.emit (/Users/lilun/workspace/supply-scm/node_modules/eslint/lib/util/safe-emitter.js:47:38)
at NodeEventGenerator.applySelector (/Users/lilun/workspace/supply-scm/node_modules/eslint/lib/util/node-event-generator.js:251:26)
at NodeEventGenerator.applySelectors (/Users/lilun/workspace/supply-scm/node_modules/eslint/lib/util/node-event-generator.js:280:22)
at NodeEventGenerator.enterNode (/Users/lilun/workspace/supply-scm/node_modules/eslint/lib/util/node-event-generator.js:294:14)
failure formatting 1 file with prettier-eslint
1 file was unchanged

@ljharb ljharb reopened this Aug 6, 2018
@ljharb
Copy link
Member

ljharb commented Aug 6, 2018

@Leonard-Li777 can you share the code it’s crashing on?

@ljharb
Copy link
Member

ljharb commented Feb 4, 2022

Happy to fix it with a repro; please file a new issue.

@ljharb ljharb closed this as completed Feb 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants