From b02a9c62739397677e01c867893d6c71a7faf9ee Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sun, 26 Apr 2020 11:26:00 +0800 Subject: [PATCH] `no-fn-reference-in-iterator`: Ignore `this.` and `Vue.filter` (#699) --- rules/no-fn-reference-in-iterator.js | 14 ++++++++++++-- test/no-fn-reference-in-iterator.js | 5 +++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/rules/no-fn-reference-in-iterator.js b/rules/no-fn-reference-in-iterator.js index 6072de4d5a..b17ba1e896 100644 --- a/rules/no-fn-reference-in-iterator.js +++ b/rules/no-fn-reference-in-iterator.js @@ -10,7 +10,11 @@ const REPLACE_WITHOUT_NAME_MESSAGE_ID = 'replace-without-name'; const iteratorMethods = [ ['every'], - ['filter'], + [ + 'filter', { + extraSelector: '[callee.object.name!="Vue"]' + } + ], ['find'], ['findIndex'], ['flatMap'], @@ -46,6 +50,7 @@ const iteratorMethods = [ parameters: ['element', 'index', 'array'], ignore: ['Boolean'], minParameters: 1, + extraSelector: '', ...options }; return [method, options]; @@ -67,7 +72,11 @@ const toSelector = name => { }; // Select all the call expressions except the ones present in the blacklist -const ignoredCalleeSelector = `${ignoredCallee.map(name => toSelector(name)).join('')}`; +const ignoredCalleeSelector = [ + // `this.{map, filter, …}()` + '[callee.object.type!="ThisExpression"]', + ...ignoredCallee.map(name => toSelector(name)) +].join(''); function check(context, node, method, options) { const {type} = node; @@ -138,6 +147,7 @@ const create = context => { min: 1, max: 2 }), + options.extraSelector, ignoredCalleeSelector, ignoredFirstArgumentSelector ].join(''); diff --git a/test/no-fn-reference-in-iterator.js b/test/no-fn-reference-in-iterator.js index 116f26e4a4..6f884c368a 100644 --- a/test/no-fn-reference-in-iterator.js +++ b/test/no-fn-reference-in-iterator.js @@ -57,6 +57,10 @@ ruleTester.run('no-fn-reference-in-iterator', rule, { ...simpleMethods.map(method => `foo.${method}(element => fn(element))`), ...reduceLikeMethods.map(method => `foo.${method}((accumulator, element) => fn(element))`), + // `this.{map, filter, …}` + ...simpleMethods.map(method => `this.${method}(fn)`), + ...reduceLikeMethods.map(method => `this.${method}(fn)`), + // `Boolean` ...simpleMethods.map(method => `foo.${method}(Boolean)`), @@ -84,6 +88,7 @@ ruleTester.run('no-fn-reference-in-iterator', rule, { 'Async.map(list, fn)', 'async.map(list, fn)', 'React.children.forEach(children, fn)', + 'Vue.filter(name, fn)', // Ignored 'foo.map(() => {})',