/
no-fn-reference-in-iterator.js
175 lines (156 loc) · 3.97 KB
/
no-fn-reference-in-iterator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'use strict';
const {isParenthesized} = require('eslint-utils');
const getDocumentationUrl = require('./utils/get-documentation-url');
const methodSelector = require('./utils/method-selector');
const {notFunctionSelector} = require('./utils/not-function');
const ERROR_WITH_NAME_MESSAGE_ID = 'error-with-name';
const ERROR_WITHOUT_NAME_MESSAGE_ID = 'error-without-name';
const REPLACE_WITH_NAME_MESSAGE_ID = 'replace-with-name';
const REPLACE_WITHOUT_NAME_MESSAGE_ID = 'replace-without-name';
const iteratorMethods = [
['every'],
[
'filter', {
extraSelector: '[callee.object.name!="Vue"]'
}
],
['find'],
['findIndex'],
['flatMap'],
['forEach'],
['map'],
[
'reduce', {
parameters: [
'accumulator',
'element',
'index',
'array'
],
minParameters: 2,
ignore: []
}
],
[
'reduceRight', {
parameters: [
'accumulator',
'element',
'index',
'array'
],
minParameters: 2,
ignore: []
}
],
['some']
].map(([method, options]) => {
options = {
parameters: ['element', 'index', 'array'],
ignore: ['Boolean'],
minParameters: 1,
extraSelector: '',
...options
};
return [method, options];
});
const ignoredCallee = [
'Promise',
'React.children',
'lodash',
'underscore',
'_',
'Async',
'async'
];
const toSelector = name => {
const splitted = name.split('.');
return `[callee.${'object.'.repeat(splitted.length)}name!="${splitted.shift()}"]`;
};
// Select all the call expressions except the ones present in the blacklist
const ignoredCalleeSelector = [
// `this.{map, filter, …}()`
'[callee.object.type!="ThisExpression"]',
...ignoredCallee.map(name => toSelector(name))
].join('');
function check(context, node, method, options) {
const {type} = node;
const name = type === 'Identifier' ? node.name : '';
if (type === 'Identifier' && options.ignore.includes(name)) {
return;
}
const problem = {
node,
messageId: name ? ERROR_WITH_NAME_MESSAGE_ID : ERROR_WITHOUT_NAME_MESSAGE_ID,
data: {
name,
method
},
suggest: []
};
const {parameters, minParameters} = options;
for (let parameterLength = minParameters; parameterLength <= parameters.length; parameterLength++) {
const suggestionParameters = parameters.slice(0, parameterLength).join(', ');
const suggest = {
messageId: name ? REPLACE_WITH_NAME_MESSAGE_ID : REPLACE_WITHOUT_NAME_MESSAGE_ID,
data: {
name,
parameters: suggestionParameters
},
fix: fixer => {
const sourceCode = context.getSourceCode();
let nodeText = sourceCode.getText(node);
if (isParenthesized(node, sourceCode) || type === 'ConditionalExpression') {
nodeText = `(${nodeText})`;
}
return fixer.replaceText(
node,
`(${suggestionParameters}) => ${nodeText}(${suggestionParameters})`
);
}
};
problem.suggest.push(suggest);
}
context.report(problem);
}
const ignoredFirstArgumentSelector = [
notFunctionSelector('arguments.0'),
'[arguments.0.type!="FunctionExpression"]',
'[arguments.0.type!="ArrowFunctionExpression"]'
].join('');
const create = context => {
const sourceCode = context.getSourceCode();
const rules = {};
for (const [method, options] of iteratorMethods) {
const selector = [
methodSelector({
name: method,
min: 1,
max: 2
}),
options.extraSelector,
ignoredCalleeSelector,
ignoredFirstArgumentSelector
].join('');
rules[selector] = node => {
const [iterator] = node.arguments;
check(context, iterator, method, options, sourceCode);
};
}
return rules;
};
module.exports = {
create,
meta: {
type: 'problem',
docs: {
url: getDocumentationUrl(__filename)
},
messages: {
[ERROR_WITH_NAME_MESSAGE_ID]: 'Do not pass function `{{name}}` directly to `.{{method}}(…)`.',
[ERROR_WITHOUT_NAME_MESSAGE_ID]: 'Do not pass function directly to `.{{method}}(…)`.',
[REPLACE_WITH_NAME_MESSAGE_ID]: 'Replace function `{{name}}` with `… => {{name}}({{parameters}})`.',
[REPLACE_WITHOUT_NAME_MESSAGE_ID]: 'Replace function with `… => …({{parameters}})`.'
}
}
};