Skip to content

Commit 72982f6

Browse files
committed
Fix no-unused-state crash
Don't assume `classInfo.aliases` is a set since there are some cases where it is `null` for a `ClassProperty`.
1 parent a83d65c commit 72982f6

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

lib/rules/no-unused-state.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ module.exports = {
265265
node.arguments[0].type === 'ArrowFunctionExpression' &&
266266
node.arguments[0].body.type === 'ObjectExpression'
267267
) {
268-
if (node.arguments[0].params.length > 0) {
268+
if (node.arguments[0].params.length > 0 && classInfo.aliases) {
269269
classInfo.aliases.add(getName(node.arguments[0].params[0]));
270270
}
271271
addStateFields(node.arguments[0].body);

tests/lib/rules/no-unused-state.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,64 @@ eslintTester.run('no-unused-state', rule, {
636636
}
637637
`,
638638
parser: 'babel-eslint'
639+
}, {
640+
code: `
641+
var Foo = createReactClass({
642+
getInitialState: function() {
643+
return { initial: 'foo' };
644+
},
645+
handleChange: function() {
646+
this.setState(state => ({
647+
current: state.initial
648+
}));
649+
},
650+
render() {
651+
const { current } = this.state;
652+
return <div>{current}</div>
653+
}
654+
});
655+
`
656+
}, {
657+
code: `
658+
var Foo = createReactClass({
659+
getInitialState: function() {
660+
return { initial: 'foo' };
661+
},
662+
handleChange: function() {
663+
this.setState((state, props) => ({
664+
current: state.initial
665+
}));
666+
},
667+
render() {
668+
const { current } = this.state;
669+
return <div>{current}</div>
670+
}
671+
});
672+
`
673+
}, {
674+
// Don't error out
675+
code: `
676+
class Foo extends Component {
677+
handleChange = function() {
678+
this.setState(state => ({ foo: value }));
679+
}
680+
render() {
681+
return <SomeComponent foo={this.state.foo} />;
682+
}
683+
}`,
684+
parser: 'babel-eslint'
685+
}, {
686+
// Don't error out
687+
code: `
688+
class Foo extends Component {
689+
static handleChange = () => {
690+
this.setState(state => ({ foo: value }));
691+
}
692+
render() {
693+
return <SomeComponent foo={this.state.foo} />;
694+
}
695+
}`,
696+
parser: 'babel-eslint'
639697
}
640698
],
641699

0 commit comments

Comments
 (0)