1 | 'use strict';
|
2 | const getDocumentationUrl = require('./utils/get-documentation-url');
|
3 | const isMethodNamed = require('./utils/is-method-named');
|
4 |
|
5 | const MESSAGE_ID_FLATMAP = 'flat-map';
|
6 | const MESSAGE_ID_SPREAD = 'spread';
|
7 |
|
8 | const SELECTOR_SPREAD = [
|
9 |
|
10 |
|
11 | 'CallExpression',
|
12 |
|
13 |
|
14 |
|
15 | '[callee.object.type="ArrayExpression"]',
|
16 | '[callee.object.elements.length=0]',
|
17 |
|
18 |
|
19 |
|
20 | '[callee.type="MemberExpression"]',
|
21 | '[callee.computed=false]',
|
22 | '[callee.property.type="Identifier"]',
|
23 | '[callee.property.name="concat"]',
|
24 |
|
25 |
|
26 |
|
27 | '[arguments.0.type="SpreadElement"]',
|
28 |
|
29 |
|
30 |
|
31 | '[arguments.0.argument.type="CallExpression"]',
|
32 |
|
33 |
|
34 |
|
35 | '[arguments.0.argument.callee.type="MemberExpression"]',
|
36 | '[arguments.0.argument.callee.computed=false]',
|
37 |
|
38 |
|
39 |
|
40 | '[arguments.0.argument.callee.property.type="Identifier"]',
|
41 | '[arguments.0.argument.callee.property.name="map"]'
|
42 | ].join('');
|
43 |
|
44 | const reportFlatMap = (context, nodeFlat, nodeMap) => {
|
45 | const source = context.getSourceCode();
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | const flatIdentifer = nodeFlat.callee.property;
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | const dot = source.getTokenBefore(flatIdentifer);
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | const maybeSemicolon = source.getTokenAfter(nodeFlat);
|
67 | const hasSemicolon = Boolean(maybeSemicolon) && maybeSemicolon.value === ';';
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | const tokenBetween = source.getLastTokenBetween(nodeMap, dot);
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | const beforeSemicolon = tokenBetween || nodeMap;
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | const fixEnd = nodeFlat.range[1];
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | const fixStart = dot.range[0];
|
94 |
|
95 | const mapProperty = nodeMap.callee.property;
|
96 |
|
97 | context.report({
|
98 | node: nodeFlat,
|
99 | messageId: MESSAGE_ID_FLATMAP,
|
100 | fix: fixer => {
|
101 | const fixings = [
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | fixer.removeRange([fixStart, fixEnd]),
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | fixer.replaceText(mapProperty, 'flatMap')
|
115 | ];
|
116 |
|
117 | if (hasSemicolon) {
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | fixings.push(fixer.insertTextAfter(beforeSemicolon, ';'));
|
124 | fixings.push(fixer.remove(maybeSemicolon));
|
125 | }
|
126 |
|
127 | return fixings;
|
128 | }
|
129 | });
|
130 | };
|
131 |
|
132 | const create = context => ({
|
133 | CallExpression: node => {
|
134 | if (!isMethodNamed(node, 'flat')) {
|
135 | return;
|
136 | }
|
137 |
|
138 | if (node.arguments.length > 1) {
|
139 | return;
|
140 | }
|
141 |
|
142 | if (
|
143 | node.arguments.length === 1 &&
|
144 | node.arguments[0].type === 'Literal' &&
|
145 | node.arguments[0].value !== 1
|
146 | ) {
|
147 | return;
|
148 | }
|
149 |
|
150 | const parent = node.callee.object;
|
151 |
|
152 | if (!isMethodNamed(parent, 'map')) {
|
153 | return;
|
154 | }
|
155 |
|
156 | reportFlatMap(context, node, parent);
|
157 | },
|
158 | [SELECTOR_SPREAD]: node => {
|
159 | context.report({
|
160 | node,
|
161 | messageId: MESSAGE_ID_SPREAD
|
162 | });
|
163 | }
|
164 | });
|
165 |
|
166 | module.exports = {
|
167 | create,
|
168 | meta: {
|
169 | type: 'suggestion',
|
170 | docs: {
|
171 | url: getDocumentationUrl(__filename)
|
172 | },
|
173 | fixable: 'code',
|
174 | messages: {
|
175 | [MESSAGE_ID_FLATMAP]: 'Prefer `.flatMap(…)` over `.map(…).flat()`.',
|
176 | [MESSAGE_ID_SPREAD]: 'Prefer `.flatMap(…)` over `[].concat(...foo.map(…))`.'
|
177 | }
|
178 | }
|
179 | };
|