Skip to content

Commit

Permalink
no-fn-reference-in-iterator: Ignore cases obviously not a function …
Browse files Browse the repository at this point in the history
…reference (#697)
  • Loading branch information
fisker committed Apr 26, 2020
1 parent 47f2246 commit dae5107
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
16 changes: 11 additions & 5 deletions rules/no-fn-reference-in-iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ const ignoredCalleeSelector = `${ignoredCallee.map(name => toSelector(name)).joi
function check(context, node, method, options) {
const {type} = node;

if (type === 'FunctionExpression' || type === 'ArrowFunctionExpression') {
return;
}

const name = type === 'Identifier' ? node.name : '';

if (type === 'Identifier' && options.ignore.includes(name)) {
Expand Down Expand Up @@ -122,6 +118,15 @@ function check(context, node, method, options) {
context.report(problem);
}

const ignoredFirstArgumentSelector = `:not(${
[
'[arguments.0.type="FunctionExpression"]',
'[arguments.0.type="ArrowFunctionExpression"]',
'[arguments.0.type="Literal"]',
'[arguments.0.type="Identifier"][arguments.0.name="undefined"]'
].join(',')
})`;

const create = context => {
const sourceCode = context.getSourceCode();
const rules = {};
Expand All @@ -133,7 +138,8 @@ const create = context => {
min: 1,
max: 2
}),
ignoredCalleeSelector
ignoredCalleeSelector,
ignoredFirstArgumentSelector
].join('');
rules[selector] = node => {
const [iterator] = node.arguments;
Expand Down
12 changes: 11 additions & 1 deletion test/no-fn-reference-in-iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,17 @@ ruleTester.run('no-fn-reference-in-iterator', rule, {
'_.map(fn)',
'Async.map(list, fn)',
'async.map(list, fn)',
'React.children.forEach(children, fn)'
'React.children.forEach(children, fn)',

// Ignored
'foo.map(() => {})',
'foo.map(function() {})',
'foo.map(function bar() {})',
'foo.map("string")',
'foo.map(null)',
'foo.map(1)',
'foo.map(true)',
'foo.map(undefined)'
],
invalid: [
// Suggestions
Expand Down

0 comments on commit dae5107

Please sign in to comment.