Skip to content

Commit b02a9c6

Browse files
authored
no-fn-reference-in-iterator: Ignore this. and Vue.filter (#699)
1 parent dae5107 commit b02a9c6

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

rules/no-fn-reference-in-iterator.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ const REPLACE_WITHOUT_NAME_MESSAGE_ID = 'replace-without-name';
1010

1111
const iteratorMethods = [
1212
['every'],
13-
['filter'],
13+
[
14+
'filter', {
15+
extraSelector: '[callee.object.name!="Vue"]'
16+
}
17+
],
1418
['find'],
1519
['findIndex'],
1620
['flatMap'],
@@ -46,6 +50,7 @@ const iteratorMethods = [
4650
parameters: ['element', 'index', 'array'],
4751
ignore: ['Boolean'],
4852
minParameters: 1,
53+
extraSelector: '',
4954
...options
5055
};
5156
return [method, options];
@@ -67,7 +72,11 @@ const toSelector = name => {
6772
};
6873

6974
// Select all the call expressions except the ones present in the blacklist
70-
const ignoredCalleeSelector = `${ignoredCallee.map(name => toSelector(name)).join('')}`;
75+
const ignoredCalleeSelector = [
76+
// `this.{map, filter, …}()`
77+
'[callee.object.type!="ThisExpression"]',
78+
...ignoredCallee.map(name => toSelector(name))
79+
].join('');
7180

7281
function check(context, node, method, options) {
7382
const {type} = node;
@@ -138,6 +147,7 @@ const create = context => {
138147
min: 1,
139148
max: 2
140149
}),
150+
options.extraSelector,
141151
ignoredCalleeSelector,
142152
ignoredFirstArgumentSelector
143153
].join('');

test/no-fn-reference-in-iterator.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ ruleTester.run('no-fn-reference-in-iterator', rule, {
5757
...simpleMethods.map(method => `foo.${method}(element => fn(element))`),
5858
...reduceLikeMethods.map(method => `foo.${method}((accumulator, element) => fn(element))`),
5959

60+
// `this.{map, filter, …}`
61+
...simpleMethods.map(method => `this.${method}(fn)`),
62+
...reduceLikeMethods.map(method => `this.${method}(fn)`),
63+
6064
// `Boolean`
6165
...simpleMethods.map(method => `foo.${method}(Boolean)`),
6266

@@ -84,6 +88,7 @@ ruleTester.run('no-fn-reference-in-iterator', rule, {
8488
'Async.map(list, fn)',
8589
'async.map(list, fn)',
8690
'React.children.forEach(children, fn)',
91+
'Vue.filter(name, fn)',
8792

8893
// Ignored
8994
'foo.map(() => {})',

0 commit comments

Comments
 (0)