Skip to content

refactor(formatter): improve and simplify the handling of AssignmentLikeLayout::BreakAfterOperator#16869

Merged
graphite-app[bot] merged 1 commit intomainfrom
12-15-refactor_formatter_improve_and_simplify_the_handling_of_assignmentlikelayout_breakafteroperator
Dec 15, 2025
Merged

refactor(formatter): improve and simplify the handling of AssignmentLikeLayout::BreakAfterOperator#16869
graphite-app[bot] merged 1 commit intomainfrom
12-15-refactor_formatter_improve_and_simplify_the_handling_of_assignmentlikelayout_breakafteroperator

Conversation

@Dunqing
Copy link
Copy Markdown
Member

@Dunqing Dunqing commented Dec 15, 2025

Align handling of AssignmentLikeLayout::BreakAfterOperator with newst Prettier's shouldBreakAfterOperator logic

The initial implementation is ported from Biome; however, some logic is outdated and diverges from Prettier, so we align exactly with Prettier to avoid unexpected edge cases.

Copy link
Copy Markdown
Member Author

Dunqing commented Dec 15, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions github-actions bot added A-formatter Area - Formatter C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior labels Dec 15, 2025
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Dec 15, 2025

CodSpeed Performance Report

Merging #16869 will not alter performance

Comparing 12-15-refactor_formatter_improve_and_simplify_the_handling_of_assignmentlikelayout_breakafteroperator (5ff1074) with main (d2f9461)

Summary

✅ 38 untouched
⏩ 7 skipped1

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@Dunqing Dunqing marked this pull request as ready for review December 15, 2025 08:27
Copilot AI review requested due to automatic review settings December 15, 2025 08:27
@Dunqing Dunqing force-pushed the 12-15-refactor_formatter_improve_and_simplify_the_handling_of_assignmentlikelayout_breakafteroperator branch from fd3a045 to 5ff1074 Compare December 15, 2025 08:28
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the AssignmentLikeLayout::BreakAfterOperator handling in the formatter to align with Prettier's latest shouldBreakAfterOperator logic. The changes simplify the code by removing outdated logic originally ported from Biome and consolidating expression unwrapping logic into a more comprehensive helper function.

Key Changes

  • Simplified layout determination logic by moving string literal and poorly breakable chain checks into should_break_after_operator
  • Enhanced get_innermost_expression helper (formerly get_last_non_unary_argument) to handle more expression types including AwaitExpression, YieldExpression, and TSNonNullExpression
  • Added is_left_short parameter to should_break_after_operator to support Prettier-aligned decision-making

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

@charliecreates charliecreates bot left a comment

Choose a reason for hiding this comment

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

The refactor improves readability and aligns the high-level structure with Prettier, but get_innermost_expression(...) broadens unwrapping to include await/yield, which can change BreakAfterOperator decisions in non-trivial ways and may not be “exact Prettier alignment” without tests. The is_left_short rule is now split across get_layout and should_break_after_operator, increasing coupling and drift risk. Tightening the helper’s scope and adding targeted regression tests would make the behavior change safer and easier to audit.

Additional notes (1)
  • Maintainability | crates/oxc_formatter/src/utils/assignment_like.rs:404-404
    The earlier is_left_short early-return (NeverBreakAfterOperator) was removed and replaced with _ if is_left_short => false inside should_break_after_operator, plus an if !left_may_break && (is_left_short || matches!(...)) condition.

This changes control flow subtly: is_left_short now prevents BreakAfterOperator even in cases that previously might have been decided earlier in get_layout (depending on surrounding conditions like left_may_break). This may be intended to match Prettier, but it also means the “short left” rule is now distributed across two places, increasing the chance of divergence later.

If is_left_short is truly part of the should_break_after_operator heuristic (as in Prettier), consider fully centralizing it there and avoiding extra coupling in get_layout’s later NeverBreakAfterOperator clause.

Summary of changes

What changed

  • Removed the now-unused std::iter import.
  • Updated AssignmentLikeLayout decision logic to pass is_left_short into should_break_after_operator(...) and to consolidate the NeverBreakAfterOperator early-return conditions.
  • Refactored should_break_after_operator to more closely follow Prettier’s shouldBreakAfterOperator behavior by:
    • short-circuiting when is_left_short is true (after handling a few explicit cases)
    • simplifying the fallback checks to use a new helper get_innermost_expression(...).
  • Replaced get_last_non_unary_argument(...) with get_innermost_expression(...), expanding the “unwrap” behavior to include TSNonNullExpression, AwaitExpression, and YieldExpression.

Links:

  • Prettier reference noted in-code: assignment.js#L235-L263.

@charliecreates charliecreates bot removed the request for review from CharlieHelps December 15, 2025 08:30
@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label Dec 15, 2025
graphite-app bot pushed a commit that referenced this pull request Dec 15, 2025
…LikeLayout::BreakAfterOperator` (#16869)

Align handling of `AssignmentLikeLayout::BreakAfterOperator` with newst [Prettier's shouldBreakAfterOperator](https://github.com/prettier/prettier/blob/0273e33fc691e28e4ab3f3c8ee86918b65cf823d/src/language-js/print/assignment.js#L195-L264) logic

The initial implementation is ported from Biome; however, some logic is outdated and diverges from Prettier, so we align exactly with Prettier to avoid unexpected edge cases.
@graphite-app graphite-app bot force-pushed the 12-15-refactor_formatter_improve_and_simplify_the_handling_of_assignmentlikelayout_breakafteroperator branch from 5ff1074 to 95ef465 Compare December 15, 2025 08:35
@graphite-app
Copy link
Copy Markdown
Contributor

graphite-app bot commented Dec 15, 2025

Merge activity

@Dunqing Dunqing force-pushed the 12-15-refactor_formatter_improve_and_simplify_the_handling_of_assignmentlikelayout_breakafteroperator branch from 95ef465 to 5ff1074 Compare December 15, 2025 08:37
…LikeLayout::BreakAfterOperator` (#16869)

Align handling of `AssignmentLikeLayout::BreakAfterOperator` with newst [Prettier's shouldBreakAfterOperator](https://github.com/prettier/prettier/blob/0273e33fc691e28e4ab3f3c8ee86918b65cf823d/src/language-js/print/assignment.js#L195-L264) logic

The initial implementation is ported from Biome; however, some logic is outdated and diverges from Prettier, so we align exactly with Prettier to avoid unexpected edge cases.
@graphite-app graphite-app bot force-pushed the 12-15-refactor_formatter_improve_and_simplify_the_handling_of_assignmentlikelayout_breakafteroperator branch from 5ff1074 to 58bced1 Compare December 15, 2025 08:37
@graphite-app graphite-app bot merged commit 58bced1 into main Dec 15, 2025
20 checks passed
@graphite-app graphite-app bot deleted the 12-15-refactor_formatter_improve_and_simplify_the_handling_of_assignmentlikelayout_breakafteroperator branch December 15, 2025 08:42
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Dec 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-formatter Area - Formatter C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants