-
-
Notifications
You must be signed in to change notification settings - Fork 403
no-array-for-each
: Ignore React.Children.forEach
#1088
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
Changes from all commits
81b5f03
d34fa26
06c2813
6349d83
f800f82
b0b8f3e
6d9b895
3cced09
beced37
dce86dd
ed5e6e0
c407fca
f742d6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ const shouldAddParenthesesToExpressionStatementExpression = require('./utils/sho | |
const getParenthesizedTimes = require('./utils/get-parenthesized-times'); | ||
const extendFixRange = require('./utils/extend-fix-range'); | ||
const isFunctionSelfUsedInside = require('./utils/is-function-self-used-inside'); | ||
const isNodeMatches = require('./utils/is-node-matches'); | ||
|
||
const MESSAGE_ID = 'no-array-for-each'; | ||
const messages = { | ||
|
@@ -315,6 +316,11 @@ function isFixable(callExpression, sourceCode, {scope, functionInfo, allIdentifi | |
return true; | ||
} | ||
|
||
const ignoredObjects = [ | ||
'React.Children', | ||
'Children' | ||
]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking we could have a shared utility method that is like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good idea, like the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That package isn't very actively maintained. I suggest we just do the utilities here for now and we can move them there later on if it becomes maintained again. Alternatively, you could fork |
||
|
||
const create = context => { | ||
const functionStack = []; | ||
const callExpressions = []; | ||
|
@@ -349,6 +355,10 @@ const create = context => { | |
returnStatements.push(node); | ||
}, | ||
[arrayForEachCallSelector](node) { | ||
if (isNodeMatches(node.callee.object, ignoredObjects)) { | ||
return; | ||
} | ||
|
||
callExpressions.push({ | ||
node, | ||
scope: context.getScope() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
|
||
function isNodeMatchesNameOrPath(node, nameOrPath) { | ||
const names = nameOrPath.split('.'); | ||
for (let index = names.length - 1; index >= 0; index--) { | ||
const name = names[index]; | ||
|
||
if (index === 0) { | ||
return node.type === 'Identifier' && node.name === name; | ||
} | ||
|
||
if ( | ||
node.type !== 'MemberExpression' || | ||
node.optional || | ||
node.property.type !== 'Identifier' || | ||
node.property.name !== name | ||
) { | ||
return false; | ||
} | ||
|
||
node = node.object; | ||
} | ||
} | ||
|
||
/** | ||
Check if node matches any object name or key path. | ||
|
||
@param {Node} node - The AST node to check. | ||
@param {string[]} nameOrPaths - The object name or key paths. | ||
@returns {boolean} | ||
*/ | ||
function isNodeMatches(node, nameOrPaths) { | ||
return nameOrPaths.some(nameOrPath => isNodeMatchesNameOrPath(node, nameOrPath)); | ||
} | ||
|
||
module.exports = isNodeMatches; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this mean to be
React.Children
.