-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathno-spreading-accumulators.js
146 lines (127 loc) · 6.24 KB
/
no-spreading-accumulators.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
/**
* @fileoverview no-spreading-accumulators
* @author Connor Ullman
* @author Joe Pikowski
*/
'use strict';
const moreInfo = `More info: https://github.com/criteo/eslint-plugin-criteo#no-spreading-accumulators`;
module.exports = {
meta: {
type: 'problem',
fixable: 'code',
schema: [],
messages: {
objectMessage: `Creating a new object by spreading the previous accumulator at every iteration of a .reduce() call has O(n^2) time & spatial complexity. If possible, make this O(n) by assigning to the existing accumulator or use mappedBy() or mappedByUnique() instead of .reduce(). ${moreInfo}`,
arrayMessage: `Creating a new array by spreading the previous accumulator at every iteration of a .reduce() call has O(n^2) time & spatial complexity. If possible, make this O(n) by pushing to the existing accumulator instead. ${moreInfo}`,
},
},
create: function (context) {
return {
'CallExpression:has([callee.property.name=reduce])[arguments] > ArrowFunctionExpression'(node) {
// only fetch the source code if getNodeText is called, and only fetch it once if we do
const sourceCode = {
_source: null,
getNodeText(node) {
if (this._source == null) this._source = context.getSourceCode().text;
if (node == null) return null;
return this._source.slice(node.range[0], node.range[1]);
},
};
const getReportConfig = (spreadNode, fix, messageId) => ({
node: spreadNode.argument,
loc: spreadNode.loc,
messageId,
fix,
});
const isSpreadNode = (spreadNode) =>
spreadNode &&
(spreadNode.type === 'ExperimentalSpreadProperty' ||
spreadNode.type === 'SpreadProperty' ||
spreadNode.type === 'SpreadElement');
const arrowFn = node;
const arrowFnFirstArg = arrowFn.params[0];
if (!arrowFnFirstArg) return;
const arrowFnFirstArgName = arrowFnFirstArg.name;
const expression = arrowFn.body;
if (!expression) return;
if (expression.type === 'ObjectExpression') {
let spreadIndex = null;
let hasNonFirstArgumentSpread = true;
for (let i = 0; i < expression.properties.length; i++) {
const prop = expression.properties[i];
if (isSpreadNode(prop)) {
const isAccumulatorBeingSpread = prop.argument.name === arrowFnFirstArgName;
if (isAccumulatorBeingSpread) {
spreadIndex = i;
} else {
hasNonFirstArgumentSpread = false;
}
}
}
if (spreadIndex == null) return;
const spreadNode = expression.properties[spreadIndex];
if (!spreadNode) return;
const getObjectFix = () => {
// if something other than the accumulator is being spread, we won't automatically fix it
if (!hasNonFirstArgumentSpread) return undefined;
// if the spread isn't the first argument of the expression, then you can only assign to the accumulator if the entry
// isn't already present without potentially changing behavior of the reduce
if (spreadIndex !== 0) return undefined;
if (arrowFn.params <= 1) return undefined;
const paramsText = arrowFn.params.map((o) => sourceCode.getNodeText(o)).join(', ');
const nonSpreadPropertyTexts = expression.properties
.filter((o, i) => i != spreadIndex)
.map(
(o) => `${arrowFnFirstArgName}[${sourceCode.getNodeText(o.key)}] = ${sourceCode.getNodeText(o.value)}`,
);
if (nonSpreadPropertyTexts.length <= 0) return undefined;
const bodyText = `${nonSpreadPropertyTexts.join('; ')}; return ${arrowFnFirstArgName};`;
const newArrowFnText = `(${paramsText}) => { ${bodyText} }`;
const fix = (fixer) => [fixer.replaceTextRange(arrowFn.range, newArrowFnText)];
return fix;
};
const reportConfig = getReportConfig(spreadNode, getObjectFix(), 'objectMessage');
return context.report(reportConfig);
} else if (expression.type === 'ArrayExpression') {
let spreadIndex = null;
let hasNonFirstArgumentSpread = true;
for (let i = 0; i < expression.elements.length; i++) {
const prop = expression.elements[i];
if (isSpreadNode(prop)) {
const isAccumulatorBeingSpread = prop.argument.name === arrowFnFirstArgName;
if (isAccumulatorBeingSpread) {
spreadIndex = i;
} else {
hasNonFirstArgumentSpread = false;
}
}
}
if (spreadIndex == null) return;
const spreadNode = expression.elements[spreadIndex];
if (!spreadNode) return;
const getArrayFix = () => {
// if something other than the accumulator is being spread, we won't automatically fix it
if (!hasNonFirstArgumentSpread) return undefined;
// if the spread isn't the first argument of the expression, then you can only assign to the accumulator if the entry
// isn't already present without potentially changing behavior of the reduce
if (spreadIndex !== 0) return undefined;
if (arrowFn.params <= 1) return undefined;
const paramsText = arrowFn.params.map((o) => sourceCode.getNodeText(o)).join(', ');
const nonSpreadElementTexts = expression.elements
.filter((o, i) => i != spreadIndex)
.map((o) => sourceCode.getNodeText(o));
if (!nonSpreadElementTexts || nonSpreadElementTexts.length <= 0) return undefined;
const bodyText = `${arrowFnFirstArgName}.push(${nonSpreadElementTexts.join(
', ',
)}); return ${arrowFnFirstArgName};`;
const newArrowFnText = `(${paramsText}) => { ${bodyText} }`;
const fix = (fixer) => [fixer.replaceTextRange(arrowFn.range, newArrowFnText)];
return fix;
};
const reportConfig = getReportConfig(spreadNode, getArrayFix(), 'arrayMessage');
return context.report(reportConfig);
}
},
};
},
};