Skip to content

Commit

Permalink
Merge pull request #2098 from jomasti/issue-2096
Browse files Browse the repository at this point in the history
`no-unused-state`: Fix crash with class fields
  • Loading branch information
ljharb committed Dec 28, 2018
2 parents 835fc05 + 72982f6 commit 254a84a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rules/no-unused-state.js
Expand Up @@ -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);
Expand Down
58 changes: 58 additions & 0 deletions tests/lib/rules/no-unused-state.js
Expand Up @@ -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 <div>{current}</div>
}
});
`
}, {
code: `
var Foo = createReactClass({
getInitialState: function() {
return { initial: 'foo' };
},
handleChange: function() {
this.setState((state, props) => ({
current: state.initial
}));
},
render() {
const { current } = this.state;
return <div>{current}</div>
}
});
`
}, {
// Don't error out
code: `
class Foo extends Component {
handleChange = function() {
this.setState(state => ({ foo: value }));
}
render() {
return <SomeComponent foo={this.state.foo} />;
}
}`,
parser: 'babel-eslint'
}, {
// Don't error out
code: `
class Foo extends Component {
static handleChange = () => {
this.setState(state => ({ foo: value }));
}
render() {
return <SomeComponent foo={this.state.foo} />;
}
}`,
parser: 'babel-eslint'
}
],

Expand Down

0 comments on commit 254a84a

Please sign in to comment.