Skip to content

Commit

Permalink
feat(linter) add partial fixer for prefer-array-flat
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 committed Aug 24, 2024
1 parent d29042e commit 687b523
Showing 1 changed file with 52 additions and 5 deletions.
57 changes: 52 additions & 5 deletions crates/oxc_linter/src/rules/unicorn/prefer_array_flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ declare_oxc_lint!(
/// ```
PreferArrayFlat,
pedantic,
pending
fix
);

impl Rule for PreferArrayFlat {
Expand Down Expand Up @@ -100,7 +100,19 @@ fn check_array_flat_map_case<'a>(call_expr: &CallExpression<'a>, ctx: &LintConte
return;
}

ctx.diagnostic(prefer_array_flat_diagnostic(call_expr.span));
let target_fix_span = call_expr
.callee
.as_member_expression()
.and_then(oxc_ast::ast::MemberExpression::static_property_info)
.map(|(span, _)| span);

if let Some(span) = target_fix_span {
ctx.diagnostic_with_fix(prefer_array_flat_diagnostic(call_expr.span), |fixer| {
fixer.replace(span, "flat()")
});
} else {
ctx.diagnostic(prefer_array_flat_diagnostic(call_expr.span));
}
}

// `array.reduce((a, b) => a.concat(b), [])`
Expand Down Expand Up @@ -162,11 +174,26 @@ fn check_array_reduce_case<'a>(call_expr: &CallExpression<'a>, ctx: &LintContext
return;
}

ctx.diagnostic(prefer_array_flat_diagnostic(call_expr.span));
ctx.diagnostic_with_fix(prefer_array_flat_diagnostic(call_expr.span), |fixer| {
let target_fix_span = call_expr
.callee
.as_member_expression()
.and_then(oxc_ast::ast::MemberExpression::static_property_info)
.map(|(span, _)| span);

debug_assert!(target_fix_span.is_some());

if let Some(span) = target_fix_span {
fixer.replace(span, "flat()")
} else {
fixer.noop()
}
});
}
}
}

// `array.reduce((a, b) => [...a, ...b], [])`
if let Expression::ArrayExpression(array_expr) = &expr_stmt.expression {
if array_expr.elements.len() != 2 {
return;
Expand Down Expand Up @@ -194,7 +221,21 @@ fn check_array_reduce_case<'a>(call_expr: &CallExpression<'a>, ctx: &LintContext
return;
}

ctx.diagnostic(prefer_array_flat_diagnostic(call_expr.span));
ctx.diagnostic_with_fix(prefer_array_flat_diagnostic(call_expr.span), |fixer| {
let target_fix_span = call_expr
.callee
.as_member_expression()
.and_then(oxc_ast::ast::MemberExpression::static_property_info)
.map(|(span, _)| span);

debug_assert!(target_fix_span.is_some());

if let Some(target_fix_span) = target_fix_span {
fixer.replace(target_fix_span, "flat()")
} else {
fixer.noop()
}
});
};
}

Expand Down Expand Up @@ -405,5 +446,11 @@ fn test() {
r"[/**/].concat(some.array)",
];

Tester::new(PreferArrayFlat::NAME, pass, fail).test_and_snapshot();
let fix = vec![
("array.flatMap(x => x)", "array.flat()"),
("array.reduce((a, b) => a.concat(b), [])", "array.flat()"),
("array.reduce((a, b) => [...a, ...b], [])", "array.flat()"),
];

Tester::new(PreferArrayFlat::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
}

0 comments on commit 687b523

Please sign in to comment.