From e56376eb7a4de0e82a5736b009b7fac943da9062 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sat, 27 Jan 2018 00:42:44 -0800 Subject: [PATCH] [Fix] `no-access-state-in-setstate`: handle object spread Fixes #1657 --- lib/rules/no-access-state-in-setstate.js | 2 +- tests/lib/rules/no-access-state-in-setstate.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/rules/no-access-state-in-setstate.js b/lib/rules/no-access-state-in-setstate.js index 9065250b13..0bebebafde 100644 --- a/lib/rules/no-access-state-in-setstate.js +++ b/lib/rules/no-access-state-in-setstate.js @@ -148,7 +148,7 @@ module.exports = { ObjectPattern(node) { const isDerivedFromThis = node.parent.init && node.parent.init.type === 'ThisExpression'; node.properties.forEach(property => { - if (property.key.name === 'state' && isDerivedFromThis) { + if (property && property.key && property.key.name === 'state' && isDerivedFromThis) { vars.push({ node: property.key, scope: context.getScope(), diff --git a/tests/lib/rules/no-access-state-in-setstate.js b/tests/lib/rules/no-access-state-in-setstate.js index e908c04ec8..3117113247 100644 --- a/tests/lib/rules/no-access-state-in-setstate.js +++ b/tests/lib/rules/no-access-state-in-setstate.js @@ -12,8 +12,9 @@ const rule = require('../../../lib/rules/no-access-state-in-setstate'); const RuleTester = require('eslint').RuleTester; const parserOptions = { - ecmaVersion: 6, + ecmaVersion: 2018, ecmaFeatures: { + experimentalObjectRestSpread: true, jsx: true } }; @@ -125,7 +126,7 @@ ruleTester.run('no-access-state-in-setstate', rule, { code: [ 'var Hello = React.createClass({', ' onClick: function() {', - ' var {state} = this', + ' var {state, ...rest} = this', ' this.setState({value: state.value + 1})', ' }', '});'