Skip to content

Commit 9cbd67e

Browse files
committed
Fix crash in no-access-state-in-setstate
With an IIFE in the return statement of render that uses `this.state`, there is no parent with a name. In this rule, the method names are later checked to see if they are being called elsewhere in the file. Since there is no intention of this being called anywhere else, the IIFE will just be skipped being added to the list of methods.
1 parent f06db3d commit 9cbd67e

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/rules/no-access-state-in-setstate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ module.exports = {
9595
node: node
9696
});
9797
break;
98-
} else if (current.type === 'FunctionExpression') {
98+
} else if (current.type === 'FunctionExpression' && current.parent.key) {
9999
methods.push({
100100
methodName: current.parent.key.name,
101101
node: node

tests/lib/rules/no-access-state-in-setstate.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ ruleTester.run('no-access-state-in-setstate', rule, {
4646
'});'
4747
].join('\n'),
4848
parserOptions: parserOptions
49+
}, {
50+
// issue 1559: don't crash
51+
code: `
52+
var SearchForm = createReactClass({
53+
render: function () {
54+
return (
55+
<div>
56+
{(function () {
57+
if (this.state.prompt) {
58+
return <div>{this.state.prompt}</div>
59+
}
60+
}).call(this)}
61+
</div>
62+
);
63+
}
64+
});
65+
`,
66+
parserOptions: parserOptions
4967
}],
5068

5169
invalid: [{

0 commit comments

Comments
 (0)