[code_builder] control-flow refinements - #2484
Conversation
* 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
|
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. |
There was a problem hiding this comment.
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.
- allow omitting `catch` clauses with `finally` - add more validation to `Conditional`, move existing validation to build stage instead of `accept` - add relevant tests
Adds some refinements for control-flow support (#2135). None of these changes are breaking as the control-flow APIs have not been published yet.
bodyfield on control-flow blocks is now typeCode?instead ofBlock. This brings it into alignment with thebodyfield on other specs.ControlBlockvia a common shared one.BranchandControlExpressionare now also exported, as they were being leaked.TryCatch->TryandCatchBlock->Catchto be more idiomatic and concise.ControFlowextension and moved all its methods toExpressionproper to reduce scope noise.fromconstructors andaddCode/addExpression.Contribution guidelines:
dart format.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.