Skip to content

Commit

Permalink
no-fn-reference-in-iterator: Ignore this. and Vue.filter (#699)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Apr 26, 2020
1 parent dae5107 commit b02a9c6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
14 changes: 12 additions & 2 deletions rules/no-fn-reference-in-iterator.js
Expand Up @@ -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'],
Expand Down Expand Up @@ -46,6 +50,7 @@ const iteratorMethods = [
parameters: ['element', 'index', 'array'],
ignore: ['Boolean'],
minParameters: 1,
extraSelector: '',
...options
};
return [method, options];
Expand All @@ -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;
Expand Down Expand Up @@ -138,6 +147,7 @@ const create = context => {
min: 1,
max: 2
}),
options.extraSelector,
ignoredCalleeSelector,
ignoredFirstArgumentSelector
].join('');
Expand Down
5 changes: 5 additions & 0 deletions test/no-fn-reference-in-iterator.js
Expand Up @@ -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)`),

Expand Down Expand Up @@ -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(() => {})',
Expand Down

0 comments on commit b02a9c6

Please sign in to comment.