Skip to content
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

Handle nodes without init #1611

Merged
merged 2 commits into from Dec 20, 2017
Merged

Handle nodes without init #1611

merged 2 commits into from Dec 20, 2017

Conversation

pfhayes
Copy link
Contributor

@pfhayes pfhayes commented Dec 15, 2017

Avoids this error which appears in master in the no-access-state-in-setstate rule

TypeError: Cannot read property 'type' of undefined
    at ObjectPattern (/Users/patrick/dev/sigopt-api/node_modules/eslint-plugin-react/lib/rules/no-access-state-in-setstate.js:158:52)
    at listeners.(anonymous function).forEach.listener (/Users/patrick/dev/sigopt-api/node_modules/eslint/lib/util/safe-emitter.js:47:58)
    at Array.forEach (<anonymous>)
    at Object.emit (/Users/patrick/dev/sigopt-api/node_modules/eslint/lib/util/safe-emitter.js:47:38)
    at NodeEventGenerator.applySelector (/Users/patrick/dev/sigopt-api/node_modules/eslint/lib/util/node-event-generator.js:251:26)
    at NodeEventGenerator.applySelectors (/Users/patrick/dev/sigopt-api/node_modules/eslint/lib/util/node-event-generator.js:280:22)
    at NodeEventGenerator.enterNode (/Users/patrick/dev/sigopt-api/node_modules/eslint/lib/util/node-event-generator.js:294:14)
    at CodePathAnalyzer.enterNode (/Users/patrick/dev/sigopt-api/node_modules/eslint/lib/code-path-analysis/code-path-analyzer.js:608:23)
    at Traverser.enter (/Users/patrick/dev/sigopt-api/node_modules/eslint/lib/linter.js:956:32)
    at Traverser.__execute (/Users/patrick/dev/sigopt-api/node_modules/estraverse/estraverse.js:397:31)

```
TypeError: Cannot read property 'type' of undefined
    at ObjectPattern (/Users/patrick/dev/sigopt-api/node_modules/eslint-plugin-react/lib/rules/no-access-state-in-setstate.js:158:52)
    at listeners.(anonymous function).forEach.listener (/Users/patrick/dev/sigopt-api/node_modules/eslint/lib/util/safe-emitter.js:47:58)
    at Array.forEach (<anonymous>)
    at Object.emit (/Users/patrick/dev/sigopt-api/node_modules/eslint/lib/util/safe-emitter.js:47:38)
    at NodeEventGenerator.applySelector (/Users/patrick/dev/sigopt-api/node_modules/eslint/lib/util/node-event-generator.js:251:26)
    at NodeEventGenerator.applySelectors (/Users/patrick/dev/sigopt-api/node_modules/eslint/lib/util/node-event-generator.js:280:22)
    at NodeEventGenerator.enterNode (/Users/patrick/dev/sigopt-api/node_modules/eslint/lib/util/node-event-generator.js:294:14)
    at CodePathAnalyzer.enterNode (/Users/patrick/dev/sigopt-api/node_modules/eslint/lib/code-path-analysis/code-path-analyzer.js:608:23)
    at Traverser.enter (/Users/patrick/dev/sigopt-api/node_modules/eslint/lib/linter.js:956:32)
    at Traverser.__execute (/Users/patrick/dev/sigopt-api/node_modules/estraverse/estraverse.js:397:31)
```
Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks; this is great - could you also include a regression test, so it doesn't happen again?

@pfhayes
Copy link
Contributor Author

pfhayes commented Dec 17, 2017

While I would love to, to be clear this was the state of the codebase when I checked it out. I don't really know what caused the error or how to reproduce, but this did fix the issue and seems worth checking in to resolve the issue for future developers

@ljharb
Copy link
Member

ljharb commented Dec 17, 2017

@pfhayes i'm confused; you're saying that npm test failed with this error? O.o

@pfhayes
Copy link
Contributor Author

pfhayes commented Dec 17, 2017

I observed this when running eslint-plugin-react master on our company's codebase, and the actual case that causes this was hard to track down

@jomasti
Copy link
Contributor

jomasti commented Dec 18, 2017

A test that would crash before this change is an SFC using destructuring in the parameters.

@ljharb
Copy link
Member

ljharb commented Dec 18, 2017

@pfhayes generally it helps to temporarily add console.log(context.getFilename()) right before the line that throws in the plugin; that way you can find the code triggering it. @jomasti's example is great too, but it'd be helpful to confirm that that's what caused your issue too.

@grajagandev
Copy link

Would you be interested in software that automatically finds TypeErrors like this? I am building Fuzz Stati0n to do that (free for OSS) - please take a look and consider signing up for our newsletter to keep in touch.

@ljharb
Copy link
Member

ljharb commented Dec 19, 2017

@grajagandev do you mean any unchecked property accesses? Generally in eslint plugins, eslint is supposed to provide some guarantees in the node structures returned, so there'll be tons of them in this repo that aren't a problem. What would be most useful is something that knows about eslint/babel-eslint node types, and warns when those accesses are incorrect.

@grajagandev
Copy link

Thank you - great feedback. We instrument the code and run npm test and we record functions called including arguments. Then we mutate those args (including objects) and re-run the tests many times in an attempt to force uncaught exceptions or other critical conditions. But it probably wouldn't have full knowledge of eslint/babel-eslint node types.

Your suggestion makes me think that it could be smarter - we are very early stage so your input means a lot.

@pfhayes
Copy link
Contributor Author

pfhayes commented Dec 20, 2017

Thanks, I confirmed the trigger on my end was object destructuring in function params and added a test

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming the test fails without the fix, LGTM!

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

Successfully merging this pull request may close these issues.

None yet

4 participants