-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprefer-flat-map.js
50 lines (48 loc) · 1.68 KB
/
prefer-flat-map.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
/**
* @license MIT
* @author Martin Giger
*/
export default {
meta: {
docs: {
description: "Prefer using the flatMap over an immediate .flat() call after a .map().",
recommended: true,
},
fixable: "code",
schema: [],
type: "suggestion",
messages: {
preferFlatMap: "Use flatMap instead of .map().flat()",
},
},
create(context) {
return {
'CallExpression[callee.type="MemberExpression"]:matches([arguments.length=0],[arguments.0.type="Literal"][arguments.0.value=1]) > MemberExpression[property.name="flat"] > CallExpression[callee.type="MemberExpression"][callee.property.name="map"]'(node) {
const callee = node.parent.parent;
context.report({
node: callee.property,
loc: {
start: node.callee.property.loc.start,
end: callee.loc.end,
},
messageId: "preferFlatMap",
fix(fixer) {
const [
, endOfMap,
] = node.range,
[
, endOfFlat,
] = callee.range;
return [
fixer.replaceTextRange(node.callee.property.range, 'flatMap'),
fixer.removeRange([
endOfMap,
endOfFlat,
]),
];
},
});
},
};
},
};