Skip to content

Commit

Permalink
Fix 'No Spreading Accumulators' Rule for Multiple Spreads
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorUllmann committed May 31, 2023
1 parent c5e4938 commit 7c49be5
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions lib/rules/no-spreading-accumulators.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = {
_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]);
},
};
Expand All @@ -49,28 +50,29 @@ module.exports = {
const expression = arrowFn.body;
if (!expression) return;

const shouldReport = spreadNode => {
if (!isSpreadNode(spreadNode)) return false;

const spreadName = spreadNode.argument.name;
const isAccumulatorBeingSpread = spreadName === arrowFnFirstArgName;
return isAccumulatorBeingSpread;
};

if (expression.type === 'ObjectExpression') {
let spreadIndex = null;
let hasNonFirstArgumentSpread = true;
for (let i = 0; i < expression.properties.length; i++) {
if (isSpreadNode(expression.properties[i])) {
spreadIndex = i;
break;
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 (!shouldReport(spreadNode)) return;
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;
Expand All @@ -94,18 +96,27 @@ module.exports = {
return context.report(reportConfig);
} else if (expression.type === 'ArrayExpression') {
let spreadIndex = null;
let hasNonFirstArgumentSpread = true;
for (let i = 0; i < expression.elements.length; i++) {
if (isSpreadNode(expression.elements[i])) {
spreadIndex = i;
break;
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 (!shouldReport(spreadNode)) return;
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;
Expand All @@ -131,4 +142,4 @@ module.exports = {
},
};
},
};
};

0 comments on commit 7c49be5

Please sign in to comment.