Skip to content

Commit fc45082

Browse files
authored
Allow this state and props in secondary callback.
Allow this state and props in secondary callback.
2 parents 2c15742 + e016332 commit fc45082

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/rules/no-this-state-props.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ module.exports = {
3333
}
3434
}
3535

36+
function isThisSetStateCallback (node) {
37+
if ( node.parent && node.parent.type === 'CallExpression' ) {
38+
var parent = node.parent;
39+
var isMatching = (
40+
isThisSetState(parent) &&
41+
parent.arguments &&
42+
parent.arguments.length > 1 &&
43+
parent.arguments[1] === node
44+
)
45+
return isMatching
46+
}
47+
}
48+
3649
function isThisState(node) {
3750
var isMatching = (
3851
node.type === 'MemberExpression' &&
@@ -79,7 +92,7 @@ module.exports = {
7992
MemberExpression: function(node) {
8093
if ( isThisProps(node) || isThisState(node) ) {
8194
var ancestors = context.getAncestors()
82-
if ( ancestors.some(isThisSetState) ) {
95+
if ( ancestors.some(isThisSetState) && !ancestors.some(isThisSetStateCallback) ) {
8396
var setStateUsage = ancestors.find(isThisSetState)
8497
setStateUsages.push(setStateUsage);
8598
}

tests/lib/rules/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ ruleTester.run("no-this-state-props", rule, {
3131
code: "this.setState( ( state, props ) => ( { opened: ! props.open } ) )",
3232
parserOptions: { ecmaVersion: 6 }
3333
},
34+
{
35+
code: "this.setState( ( state, props ) => ( { opened: ! props.open } ), () => ( { state: this.state } ) )",
36+
parserOptions: { ecmaVersion: 6 }
37+
},
3438
{
3539
code: "var yolo = this.state.yolo; this.setState( ( state, props ) => ( { opened: ! props.open } ) )",
3640
parserOptions: { ecmaVersion: 6 }

0 commit comments

Comments
 (0)