-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out no-<method>-set-state rule code to a utility
- Loading branch information
William Holloway
committed
Apr 6, 2017
1 parent
877e6dc
commit 58519ae
Showing
4 changed files
with
76 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* @fileoverview Prevent usage of setState in lifecycle methods | ||
* @author Yannick Croissant | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
function makeNoMethodSetStateRule(methodName) { | ||
return { | ||
meta: { | ||
docs: { | ||
description: 'Prevent usage of setState in ' + methodName, | ||
category: 'Best Practices', | ||
recommended: false | ||
}, | ||
|
||
schema: [{ | ||
enum: ['disallow-in-func'] | ||
}] | ||
}, | ||
|
||
create: function(context) { | ||
|
||
var mode = context.options[0] || 'allow-in-func'; | ||
|
||
// -------------------------------------------------------------------------- | ||
// Public | ||
// -------------------------------------------------------------------------- | ||
|
||
return { | ||
|
||
CallExpression: function(node) { | ||
var callee = node.callee; | ||
if ( | ||
callee.type !== 'MemberExpression' || | ||
callee.object.type !== 'ThisExpression' || | ||
callee.property.name !== 'setState' | ||
) { | ||
return; | ||
} | ||
var ancestors = context.getAncestors(callee).reverse(); | ||
var depth = 0; | ||
for (var i = 0, j = ancestors.length; i < j; i++) { | ||
if (/Function(Expression|Declaration)$/.test(ancestors[i].type)) { | ||
depth++; | ||
} | ||
if ( | ||
(ancestors[i].type !== 'Property' && ancestors[i].type !== 'MethodDefinition') || | ||
ancestors[i].key.name !== methodName || | ||
(mode !== 'disallow-in-func' && depth > 1) | ||
) { | ||
continue; | ||
} | ||
context.report({ | ||
node: callee, | ||
message: 'Do not use setState in ' + methodName | ||
}); | ||
break; | ||
} | ||
} | ||
}; | ||
|
||
} | ||
}; | ||
} | ||
|
||
module.exports = makeNoMethodSetStateRule; |