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

Fix no-unused-state crash #2098

Merged
merged 1 commit into from Dec 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This and the next test case were what I could find that would reproduce the error. It seems like weird usages, but it won't error out.

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