Skip to content

Commit 8c3880e

Browse files
jason-mayermosuem
andauthored
[code_builder] Add control-flow support (#2135)
Co-authored-by: Jason Mayer <[email protected]> Co-authored-by: Moritz <[email protected]>
1 parent e1db6c8 commit 8c3880e

17 files changed

Lines changed: 4542 additions & 112 deletions

‎pkgs/code_builder/.gitignore‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
.dart_tool
33
.packages
44
.pub
5-
pubspec.lock
5+
pubspec.lock

‎pkgs/code_builder/CHANGELOG.md‎

Lines changed: 79 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
## 4.12.0-wip
22

3+
* Add `ForLoop`, `ForInLoop`, and `WhileLoop` classes to support control-flow loops.
4+
* Add `Conditional` and `BranchBuilder` classes to support `if` statements and trees.
5+
* Add `TryCatch` and `CatchBlock` classes to support `try`/`catch` blocks.
6+
* Add `Case`, `SwitchExpression`, and `SwitchStatement` classes to support `switch`
7+
expressions/statements.
8+
* Add `yielded`, `yieldStarred`, `ifThen`, `ifThenReturn`, `loopWhile`, `loopDoWhile`,
9+
and `loopForIn` methods to `Expression` via `ControlFlow` extension.
10+
* Add static constants `breakVoid`, `continueVoid`, `returnVoid`, and `rethrowVoid`
11+
(on `ControlFlow`) and `wildcard` (on `Expression`).
12+
* Add static methods `breakLabel`, `continueLabel`, `collectionIf`, `collectionElse`,
13+
`collectionFor`, `collectionForIn`, and `ifCase` to `ControlFlow`.
14+
* Simplify usage examples on the README.
315
* Ensure scoped lint ignores
416
(such as `// ignore_for_file: no_leading_underscores_for_library_prefixes`)
517
are emitted at the top of the file before library annotations and directives.
@@ -184,7 +196,7 @@ void main() {
184196
## 3.4.1
185197

186198
* Fix confusing mismatch description from `equalsDart`.
187-
https://github.com/dart-lang/code_builder/issues/293
199+
<https://github.com/dart-lang/code_builder/issues/293>
188200

189201
## 3.4.0
190202

@@ -237,7 +249,6 @@ void main() {
237249
* `Expression.asA` is now wrapped with parenthesis so that further calls may be
238250
made on it as an expression.
239251

240-
241252
## 3.1.0
242253

243254
* Added `Expression.asA` for creating explicit casts:
@@ -284,7 +295,7 @@ void main() {
284295

285296
## 2.4.0
286297

287-
* Add `equalTo`, `notEqualTo`, `greaterThan`, `lessThan`, `greateOrEqualTo`, and
298+
* Add `equalTo`, `notEqualTo`, `greaterThan`, `lessThan`, `greaterOrEqualTo`, and
288299
`lessOrEqualTo` to `Expression`.
289300

290301
## 2.3.0
@@ -374,7 +385,7 @@ void main() {
374385
```
375386

376387
* Added `nullSafeProperty` to `Expression` to access properties with `?.`
377-
* Added `conditional` to `Expression` to use the ternary operator `? : `
388+
* Added `conditional` to `Expression` to use the ternary operator `? :`
378389
* Methods taking `positionalArguments` accept `Iterable<Expression>`
379390
* **BUG FIX**: Parameters can take a `FunctionType` as a `type`.
380391
`Reference.type` now returns a `Reference`. Note that this change is
@@ -600,7 +611,7 @@ final animal = new Class((b) => b
600611
## 2.0.0-alpha
601612

602613
* Complete re-write to not use `package:analyzer`.
603-
* Code generation now properly uses the _builder_ pattern (via `built_value`).
614+
* Code generation now properly uses the *builder* pattern (via `built_value`).
604615
* See examples and tests for details.
605616

606617
## 1.0.4
@@ -634,38 +645,38 @@ that the entire Dart language is buildable with our API, though.
634645

635646
**Contributions are welcome.**
636647

637-
- Exposed `uri` in `ImportBuilder`, `ExportBuilder`, and `Part[Of]Builder`.
648+
* Exposed `uri` in `ImportBuilder`, `ExportBuilder`, and `Part[Of]Builder`.
638649

639650
## 1.0.0-beta+7
640651

641-
- Added `ExpressionBuilder#ternary`.
652+
* Added `ExpressionBuilder#ternary`.
642653

643654
## 1.0.0-beta+6
644655

645-
- Added `TypeDefBuilder`.
646-
- Added `FunctionParameterBuilder`.
647-
- Added `asAbstract` to various `MethodBuilder` constructors.
656+
* Added `TypeDefBuilder`.
657+
* Added `FunctionParameterBuilder`.
658+
* Added `asAbstract` to various `MethodBuilder` constructors.
648659

649660
## 1.0.0-beta+5
650661

651-
- Re-published the package without merge conflicts.
662+
* Re-published the package without merge conflicts.
652663

653664
## 1.0.0-beta+4
654665

655-
- Renamed `PartBuilder` to `PartOfBuilder`.
656-
- Added a new class, `PartBuilder`, to represent `part '...dart'` directives.
657-
- Added the `HasAnnotations` interface to all library/part/directive builders.
658-
- Added `asFactory` and `asConst` to `ConstructorBuilder`.
659-
- Added `ConstructorBuilder.redirectTo` for a redirecting factory constructor.
660-
- Added a `name` getter to `ReferenceBuilder`.
661-
- Supplying an empty constructor name (`''`) is equivalent to `null` (default).
662-
- Automatically encodes string literals with multiple lines as `'''`.
663-
- Added `asThrow` to `ExpressionBuilder`.
664-
- Fixed a bug that prevented `FieldBuilder` from being used at the top-level.
666+
* Renamed `PartBuilder` to `PartOfBuilder`.
667+
* Added a new class, `PartBuilder`, to represent `part '...dart'` directives.
668+
* Added the `HasAnnotations` interface to all library/part/directive builders.
669+
* Added `asFactory` and `asConst` to `ConstructorBuilder`.
670+
* Added `ConstructorBuilder.redirectTo` for a redirecting factory constructor.
671+
* Added a `name` getter to `ReferenceBuilder`.
672+
* Supplying an empty constructor name (`''`) is equivalent to `null` (default).
673+
* Automatically encodes string literals with multiple lines as `'''`.
674+
* Added `asThrow` to `ExpressionBuilder`.
675+
* Fixed a bug that prevented `FieldBuilder` from being used at the top-level.
665676

666677
## 1.0.0-beta+3
667678

668-
- Added support for `genericTypes` parameter for `ExpressionBuilder#invoke`:
679+
* Added support for `genericTypes` parameter for `ExpressionBuilder#invoke`:
669680

670681
```dart
671682
expect(
@@ -678,7 +689,7 @@ expect(
678689
);
679690
```
680691

681-
- Added a `castAs` method to `ExpressionBuilder`:
692+
* Added a `castAs` method to `ExpressionBuilder`:
682693

683694
```dart
684695
expect(
@@ -691,7 +702,7 @@ expect(
691702

692703
### BREAKING CHANGES
693704

694-
- Removed `namedNewInstance` and `namedConstInstance`, replaced with `constructor: `:
705+
* Removed `namedNewInstance` and `namedConstInstance`, replaced with `constructor:`:
695706

696707
```dart
697708
expect(
@@ -702,7 +713,7 @@ expect(
702713
);
703714
```
704715

705-
- Renamed `named` parameter to `namedArguments`:
716+
* Renamed `named` parameter to `namedArguments`:
706717

707718
```dart
708719
expect(
@@ -724,25 +735,25 @@ expect(
724735

725736
Avoid creating symbols that can collide with the Dart language:
726737

727-
- `MethodModifier.async` -> `MethodModifier.asAsync`
728-
- `MethodModifier.asyncStar` -> `MethodModifier.asAsyncStar`
729-
- `MethodModifier.syncStar` -> `MethodModifier.asSyncStar`
738+
* `MethodModifier.async` -> `MethodModifier.asAsync`
739+
* `MethodModifier.asyncStar` -> `MethodModifier.asAsyncStar`
740+
* `MethodModifier.syncStar` -> `MethodModifier.asSyncStar`
730741

731742
## 1.0.0-beta+1
732743

733-
- Add support for `switch` statements
734-
- Add support for a raw expression and statement
735-
- `new ExpressionBuilder.raw(...)`
736-
- `new StatemnetBuilder.raw(...)`
744+
* Add support for `switch` statements
745+
* Add support for a raw expression and statement
746+
* `new ExpressionBuilder.raw(...)`
747+
* `new StatementBuilder.raw(...)`
737748

738749
This should help cover any cases not covered with builders today.
739750

740-
- Allow referring to a `ClassBuilder` and `TypeBuilder` as an expression
741-
- Add support for accessing the index `[]` operator on an expression
751+
* Allow referring to a `ClassBuilder` and `TypeBuilder` as an expression
752+
* Add support for accessing the index `[]` operator on an expression
742753

743754
### BREAKING CHANGES
744755

745-
- Changed `ExpressionBuilder.asAssign` to always take an `ExpressionBuilder` as
756+
* Changed `ExpressionBuilder.asAssign` to always take an `ExpressionBuilder` as
746757
target and removed the `value` property. Most changes are pretty simple, and
747758
involve just using `reference(...)`. For example:
748759

@@ -754,34 +765,34 @@ literal(true).asAssign(reference('flag'))
754765

755766
## 1.0.0-beta
756767

757-
- Add support for `async`, `sync`, `sync*` functions
758-
- Add support for expression `asAwait`, `asYield`, `asYieldStar`
759-
- Add `toExportBuilder` and `toImportBuilder` to types and references
760-
- Fix an import scoping bug in `return` statements and named constructor invocations.
761-
- Added constructor initializer support
762-
- Add `while` and `do {} while` loop support
763-
- Add `for` and `for-in` support
764-
- Added a `name` getter for `ParameterBuilder`
768+
* Add support for `async`, `sync`, `sync*` functions
769+
* Add support for expression `asAwait`, `asYield`, `asYieldStar`
770+
* Add `toExportBuilder` and `toImportBuilder` to types and references
771+
* Fix an import scoping bug in `return` statements and named constructor invocations.
772+
* Added constructor initializer support
773+
* Add `while` and `do {} while` loop support
774+
* Add `for` and `for-in` support
775+
* Added a `name` getter for `ParameterBuilder`
765776

766777
## 1.0.0-alpha+7
767778

768-
- Make use of the new analyzer APIs in preparation for analyzer version 0.30.
779+
* Make use of the new analyzer APIs in preparation for analyzer version 0.30.
769780

770781
## 1.0.0-alpha+6
771782

772-
- `MethodBuilder.closure` emits properly as a top-level function
783+
* `MethodBuilder.closure` emits properly as a top-level function
773784

774785
## 1.0.0-alpha+5
775786

776-
- MethodBuilder with no statements will create an empty block instead of
787+
* MethodBuilder with no statements will create an empty block instead of
777788
a semicolon.
778789

779790
```dart
780791
// main() {}
781792
method('main')
782793
```
783794

784-
- Fix lambdas and closures to not include a trailing semicolon when used
795+
* Fix lambdas and closures to not include a trailing semicolon when used
785796
as an expression.
786797

787798
```dart
@@ -791,29 +802,29 @@ method('main')
791802

792803
## 1.0.0-alpha+4
793804

794-
- Add support for the latest `pkg/analyzer`.
805+
* Add support for the latest `pkg/analyzer`.
795806

796807
## 1.0.0-alpha+3
797808

798-
- BREAKING CHANGE: Added generics support to `TypeBuilder`:
809+
* BREAKING CHANGE: Added generics support to `TypeBuilder`:
799810

800-
`importFrom` becomes a _named_, not a positional argument, and the named
811+
`importFrom` becomes a *named*, not a positional argument, and the named
801812
argument `genericTypes` is added (`Iterable<TypeBuilder>`).
802813

803814
```dart
804815
// List<String>
805816
new TypeBuilder('List', genericTypes: [reference('String')])
806817
```
807818

808-
- Added generic support to `ReferenceBuilder`:
819+
* Added generic support to `ReferenceBuilder`:
809820

810821
```dart
811822
// List<String>
812823
reference('List').toTyped([reference('String')])
813824
```
814825

815-
- Fixed a bug where `ReferenceBuilder.buildAst` was not implemented
816-
- Added `and` and `or` methods to `ExpressionBuilder`:
826+
* Fixed a bug where `ReferenceBuilder.buildAst` was not implemented
827+
* Added `and` and `or` methods to `ExpressionBuilder`:
817828

818829
```dart
819830
// true || false
@@ -823,7 +834,7 @@ literal(true).or(literal(false));
823834
literal(true).and(literal(false));
824835
```
825836

826-
- Added support for creating closures - `MethodBuilder.closure`:
837+
* Added support for creating closures - `MethodBuilder.closure`:
827838

828839
```dart
829840
// () => true
@@ -835,21 +846,21 @@ new MethodBuilder.closure(
835846

836847
## 1.0.0-alpha+2
837848

838-
- Added `returnVoid` to well, `return;`
839-
- Added support for top-level field assignments:
849+
* Added `returnVoid` to well, `return;`
850+
* Added support for top-level field assignments:
840851

841852
```dart
842853
new LibraryBuilder()..addMember(literal(false).asConst('foo'))
843854
```
844855

845-
- Added support for specifying a `target` when using `asAssign`:
856+
* Added support for specifying a `target` when using `asAssign`:
846857

847858
```dart
848859
// Outputs bank.bar = goldBar
849860
reference('goldBar').asAssign('bar', target: reference('bank'))
850861
```
851862

852-
- Added support for the cascade operator:
863+
* Added support for the cascade operator:
853864

854865
```dart
855866
// Outputs foo..doThis()..doThat()
@@ -859,7 +870,7 @@ reference('foo').cascade((c) => <ExpressionBuilder> [
859870
]);
860871
```
861872

862-
- Added support for accessing a property
873+
* Added support for accessing a property
863874

864875
```dart
865876
// foo.bar
@@ -868,25 +879,25 @@ reference('foo').property('bar');
868879

869880
## 1.0.0-alpha+1
870881

871-
- Slight updates to confusing documentation.
872-
- Added support for null-aware assignments.
873-
- Added `show` and `hide` support to `ImportBuilder`
874-
- Added `deferred` support to `ImportBuilder`
875-
- Added `ExportBuilder`
876-
- Added `list` and `map` literals that support generic types
882+
* Slight updates to confusing documentation.
883+
* Added support for null-aware assignments.
884+
* Added `show` and `hide` support to `ImportBuilder`
885+
* Added `deferred` support to `ImportBuilder`
886+
* Added `ExportBuilder`
887+
* Added `list` and `map` literals that support generic types
877888

878889
## 1.0.0-alpha
879890

880-
- Large refactor that makes the library more feature complete.
891+
* Large refactor that makes the library more feature complete.
881892

882893
## 0.1.1
883894

884-
- Add the concept of `Scope` and change `toAst` to support it
895+
* Add the concept of `Scope` and change `toAst` to support it
885896

886897
Now your entire AST tree can be scoped and import directives
887898
automatically added to a `LibraryBuilder` for you if you use
888899
`LibraryBuilder.scope`.
889900

890901
## 0.1.0
891902

892-
- Initial version
903+
* Initial version

0 commit comments

Comments
 (0)