Skip to content

Commit f2cd61f

Browse files
committed
fix(linter/no-accumulating-spread): false positive in nested callbacks within reduce
1 parent 90add74 commit f2cd61f

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

crates/oxc_linter/src/rules/oxc/no_accumulating_spread.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl Rule for NoAccumulatingSpread {
143143
let declaration_id = symbols.symbol_declaration(referenced_symbol_id);
144144
let declaration = ctx.nodes().parent_node(declaration_id);
145145

146-
check_reduce_usage(declaration, referenced_symbol_id, spread.span, ctx);
146+
check_reduce_usage(declaration, referenced_symbol_id, spread.span, node.id(), ctx);
147147
check_loop_usage(
148148
declaration,
149149
ctx.nodes().get_node(declaration_id),
@@ -159,6 +159,7 @@ fn check_reduce_usage<'a>(
159159
declaration: &AstNode<'a>,
160160
referenced_symbol_id: SymbolId,
161161
spread_span: Span,
162+
spread_node_id: NodeId,
162163
ctx: &LintContext<'a>,
163164
) {
164165
let AstKind::FormalParameters(params) = declaration.kind() else {
@@ -183,6 +184,13 @@ fn check_reduce_usage<'a>(
183184
for parent in ctx.nodes().ancestors(declaration.id()) {
184185
if let AstKind::CallExpression(call_expr) = parent.kind()
185186
&& is_method_call(call_expr, None, Some(&["reduce", "reduceRight"]), Some(1), Some(2))
187+
&& ctx
188+
.nodes()
189+
.ancestors(spread_node_id)
190+
.take_while(|n| !n.kind().span().contains_inclusive(declaration.span()))
191+
.all(|n| {
192+
!matches!(n.kind(), AstKind::ArrowFunctionExpression(_) | AstKind::Function(_))
193+
})
186194
{
187195
ctx.diagnostic(get_reduce_diagnostic(call_expr, spread_span));
188196
return;
@@ -449,6 +457,7 @@ fn test() {
449457
"let foo = {}; for (let i of [1,2,3]) { foo[i] = i; }",
450458
"let foo = {}; for (const i of [1,2,3]) { foo[i] = i; }",
451459
"let foo = {}; while (Object.keys(foo).length < 10) { foo[Object.keys(foo).length] = Object.keys(foo).length; }",
460+
"function doSomething(list) { return list.reduce((acc, each) => { return each.subList.flatMap((subEach) => { return [...acc, subEach.subList] }) }, []) }",
452461
];
453462

454463
let fail = vec![

0 commit comments

Comments
 (0)