From dae5107fa86c58342112026450dddb57b240d20d Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sun, 26 Apr 2020 10:20:30 +0800 Subject: [PATCH] `no-fn-reference-in-iterator`: Ignore cases obviously not a function reference (#697) --- rules/no-fn-reference-in-iterator.js | 16 +++++++++++----- test/no-fn-reference-in-iterator.js | 12 +++++++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/rules/no-fn-reference-in-iterator.js b/rules/no-fn-reference-in-iterator.js index 608c7f54a2..6072de4d5a 100644 --- a/rules/no-fn-reference-in-iterator.js +++ b/rules/no-fn-reference-in-iterator.js @@ -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)) { @@ -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 = {}; @@ -133,7 +138,8 @@ const create = context => { min: 1, max: 2 }), - ignoredCalleeSelector + ignoredCalleeSelector, + ignoredFirstArgumentSelector ].join(''); rules[selector] = node => { const [iterator] = node.arguments; diff --git a/test/no-fn-reference-in-iterator.js b/test/no-fn-reference-in-iterator.js index 2d2a62e84b..116f26e4a4 100644 --- a/test/no-fn-reference-in-iterator.js +++ b/test/no-fn-reference-in-iterator.js @@ -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