Skip to content

[code_builder] control-flow refinements - #2484

Open
jason-mayer wants to merge 9 commits into
dart-lang:mainfrom
jason-mayer:refine-control-flow
Open

[code_builder] control-flow refinements#2484
jason-mayer wants to merge 9 commits into
dart-lang:mainfrom
jason-mayer:refine-control-flow

Conversation

@jason-mayer

Copy link
Copy Markdown
Contributor

Adds some refinements for control-flow support (#2135). None of these changes are breaking as the control-flow APIs have not been published yet.

  • Moved almost all logic to visitors, making the code much easier to follow. This also allowed for the removal of many internal-only intermediary classes.
  • The body field on control-flow blocks is now type Code? instead of Block. This brings it into alignment with the body field on other specs.
  • Reduced API leakage. Each control flow class now has its own visitor (with logic shared internally) rather than leaking ControlBlock via a common shared one. Branch and ControlExpression are now also exported, as they were being leaked.
  • Renamed TryCatch -> Try and CatchBlock -> Catch to be more idiomatic and concise.
  • Removed the ControFlow extension and moved all its methods to Expression proper to reduce scope noise.
  • Added some quality of life functions to control flow classes such as from constructors and addCode/addExpression.
  • Updated changelog, documentation, and tests to reflect these changes.

  • [✅️ ] I’ve reviewed the contributor guide and applied the relevant portions to this PR.
Contribution guidelines:

Many Dart repos have a weekly cadence for reviewing PRs - please allow for some latency before initial review feedback.

Note: The Dart team is trialing Gemini Code Assist. Don't take its comments as final Dart team feedback. Use the suggestions if they're helpful; otherwise, wait for a human reviewer.

* move all logic to visitors
* remove 'buildable' classes
* implement visitors for each control flow class
* stop leaking `ControlBlock`
* use `Code?` instead of `Block` for body types on control-flow classes
* add `.from` factory constructors to `Conditional`, `Branch`, and `Case`, as well as other helper functions
* export `Branch`
* update tests and add a couple new cases
* rename `CatchBlock` to `Catch`
* add helpers for control blow block bodies
* add more tests
- move the `ControlFlow` extension to `Expression` proper to reduce scope noise
- update tests and changelog  to reflect this
- fix some examples in docs
@jason-mayer
jason-mayer requested a review from a team as a code owner July 31, 2026 22:16
@google-cla

google-cla Bot commented Jul 31, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the control-flow specifications in code_builder to simplify their structure and usage. Key changes include renaming TryCatch to Try, CatchBlock to Catch, and ControlFlow to ControlExpression, refactoring Conditional and Branch classes, and moving extension methods directly onto Expression. Additionally, loop and block bodies have been updated from Block to Code? to allow more flexible code generation. Feedback on these changes highlights a bug in Try that prevents valid try-finally blocks without catch clauses, a need for validation in Conditional to ensure only the last branch can omit a condition, and a request to expand documentation and provide usage examples for the newly public Branch, Catch, and Try APIs to align with the repository style guide.

Comment thread pkgs/code_builder/lib/src/specs/control/handling.dart
Comment thread pkgs/code_builder/lib/src/specs/control/branches.dart Outdated
Comment thread pkgs/code_builder/lib/src/specs/control/branches.dart
Comment thread pkgs/code_builder/lib/src/specs/control/handling.dart
Comment thread pkgs/code_builder/lib/src/specs/control/handling.dart
- allow omitting `catch` clauses with `finally`
- add more validation to `Conditional`, move existing validation to build stage instead of `accept`
- add relevant tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant