Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions crates/oxc_formatter/src/parentheses/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,14 +406,20 @@ fn is_in_for_initializer(expr: &AstNode<'_, BinaryExpression<'_>>) -> bool {
match parent {
AstNodes::ExpressionStatement(stmt) => {
if stmt.is_arrow_function_body() {
// Expression body: `() => expr`
// Skip `FunctionBody` and `ArrowFunctionExpression`
let skipped = ancestors.by_ref().nth(1);
debug_assert!(matches!(skipped, Some(AstNodes::ArrowFunctionExpression(_))));
continue;
}
// Block body: `() => { expr; }` - check if we're inside a FunctionBody
if matches!(stmt.parent, AstNodes::FunctionBody(_)) {
continue;
Comment on lines +415 to +417
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new matches!(stmt.parent, AstNodes::FunctionBody(_)) check will be true for ExpressionStatements inside any function body (regular functions/methods as well as arrows), not just arrow-function block bodies. If this change is meant to target arrow functions only, consider narrowing the condition to verify the enclosing FunctionBody belongs to an ArrowFunctionExpression (and optionally arrow.expression == false) to avoid changing parentheses behavior in other function bodies nested under ForStatement.init.

Suggested change
// Block body: `() => { expr; }` - check if we're inside a FunctionBody
if matches!(stmt.parent, AstNodes::FunctionBody(_)) {
continue;
// Block body: `() => { expr; }` - check if we're inside an arrow function's FunctionBody
if let AstNodes::FunctionBody(body) = stmt.parent {
if let AstNodes::ArrowFunctionExpression(arrow) = body.parent {
// Only treat block-body arrow functions specially (`() => { ... }`)
if !arrow.expression {
continue;
}
}

Copilot uses AI. Check for mistakes.
}
Comment on lines +415 to +418
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new behavior (treating in expressions inside arrow-function block bodies as requiring parentheses when nested under ForStatement.init) doesn’t appear to have a dedicated formatter fixture regression test. Please add a small crates/oxc_formatter/tests/fixtures/js case covering an arrow with a block body inside for (...;...;...) where the block contains an in expression statement, so future refactors to is_in_for_initializer don’t regress this again.

Copilot uses AI. Check for mistakes.

return false;
}
// FunctionBody: Continue checking - could be inside arrow function in ForStatement.init
AstNodes::ForStatement(stmt) => {
Comment on lines +422 to 423
Copy link

Copilot AI Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment mentions FunctionBody, but it’s placed on the ForStatement match arm. Consider moving/rewording it so it documents the intended behavior (e.g., that FunctionBody is intentionally ignored via the default arm) rather than implying this arm handles FunctionBody.

Copilot uses AI. Check for mistakes.
return stmt
.init
Expand Down
5 changes: 2 additions & 3 deletions tasks/prettier_conformance/snapshots/prettier.js.snap.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
js compatibility: 737/754 (97.75%)
js compatibility: 738/754 (97.88%)

# Failed

Expand All @@ -11,8 +11,7 @@ js compatibility: 737/754 (97.75%)
| js/comments/return-statement.js | 💥💥 | 98.85% |
| js/explicit-resource-management/valid-await-using-comments.js | 💥 | 80.00% |
| js/for/9812-unstable.js | 💥 | 63.64% |
| js/for/for-in-with-initializer.js | 💥 | 37.50% |
| js/for/parentheses.js | 💥 | 97.96% |
| js/for/for-in-with-initializer.js | 💥 | 31.25% |
| js/last-argument-expansion/dangling-comment-in-arrow-function.js | 💥 | 22.22% |
| js/quote-props/objects.js | 💥💥✨✨ | 48.04% |
| js/quote-props/with_numbers.js | 💥💥✨✨ | 46.43% |
Expand Down
Loading