diff --git a/lib/rules/no-unused-state.js b/lib/rules/no-unused-state.js index 0804d5650b..9904991563 100644 --- a/lib/rules/no-unused-state.js +++ b/lib/rules/no-unused-state.js @@ -265,7 +265,7 @@ module.exports = { node.arguments[0].type === 'ArrowFunctionExpression' && node.arguments[0].body.type === 'ObjectExpression' ) { - if (node.arguments[0].params.length > 0) { + if (node.arguments[0].params.length > 0 && classInfo.aliases) { classInfo.aliases.add(getName(node.arguments[0].params[0])); } addStateFields(node.arguments[0].body); diff --git a/tests/lib/rules/no-unused-state.js b/tests/lib/rules/no-unused-state.js index 2742500589..282f32cd2c 100644 --- a/tests/lib/rules/no-unused-state.js +++ b/tests/lib/rules/no-unused-state.js @@ -636,6 +636,64 @@ eslintTester.run('no-unused-state', rule, { } `, parser: 'babel-eslint' + }, { + code: ` + var Foo = createReactClass({ + getInitialState: function() { + return { initial: 'foo' }; + }, + handleChange: function() { + this.setState(state => ({ + current: state.initial + })); + }, + render() { + const { current } = this.state; + return
{current}
+ } + }); + ` + }, { + code: ` + var Foo = createReactClass({ + getInitialState: function() { + return { initial: 'foo' }; + }, + handleChange: function() { + this.setState((state, props) => ({ + current: state.initial + })); + }, + render() { + const { current } = this.state; + return
{current}
+ } + }); + ` + }, { + // Don't error out + code: ` + class Foo extends Component { + handleChange = function() { + this.setState(state => ({ foo: value })); + } + render() { + return ; + } + }`, + parser: 'babel-eslint' + }, { + // Don't error out + code: ` + class Foo extends Component { + static handleChange = () => { + this.setState(state => ({ foo: value })); + } + render() { + return ; + } + }`, + parser: 'babel-eslint' } ],