Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(linter/unicorn): add partial fixer for prefer-array-flat #5143

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
conditional_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(|v| Span::new(v.0.start, call_expr.span.end));

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(|v| Span::new(v.0.start, call_expr.span.end));

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(|v| Span::new(v.0.start, call_expr.span.end));

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();
}