From eb7e152704dac25582b158615169fd791fd539da Mon Sep 17 00:00:00 2001 From: Patrick Hayes Date: Fri, 15 Dec 2017 15:51:11 -0800 Subject: [PATCH 1/2] Handle nodes without init ``` 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 () 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) ``` --- lib/rules/no-access-state-in-setstate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/no-access-state-in-setstate.js b/lib/rules/no-access-state-in-setstate.js index 72e8081520..5a8d2e36ea 100644 --- a/lib/rules/no-access-state-in-setstate.js +++ b/lib/rules/no-access-state-in-setstate.js @@ -143,7 +143,7 @@ module.exports = { }, ObjectPattern(node) { - const isDerivedFromThis = node.parent.init.type === 'ThisExpression'; + const isDerivedFromThis = node.parent.init && node.parent.init.type === 'ThisExpression'; node.properties.forEach(property => { if (property.key.name === 'state' && isDerivedFromThis) { vars.push({ From bb343723274921500a05eba1a9e68481b7fca3e1 Mon Sep 17 00:00:00 2001 From: Patrick Hayes Date: Wed, 20 Dec 2017 08:03:19 +0000 Subject: [PATCH 2/2] Add test --- tests/lib/rules/no-access-state-in-setstate.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/lib/rules/no-access-state-in-setstate.js b/tests/lib/rules/no-access-state-in-setstate.js index 784c6f6ed7..e908c04ec8 100644 --- a/tests/lib/rules/no-access-state-in-setstate.js +++ b/tests/lib/rules/no-access-state-in-setstate.js @@ -75,6 +75,13 @@ ruleTester.run('no-access-state-in-setstate', rule, { '});' ].join('\n'), parserOptions: parserOptions + }, { + // /~https://github.com/yannickcr/eslint-plugin-react/pull/1611 + code: ` + function testFunction({a, b}) { + }; + `, + parserOptions: parserOptions }], invalid: [{