Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Add support for named arguments in `enum` classes
* Add support for external keyword on fields.
* Add `Expression.parenthesized` to manually wrap an expression in parenthesis.

## 4.5.0

Expand Down
1 change: 1 addition & 0 deletions lib/code_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export 'src/specs/expression.dart'
InvokeExpressionType,
LiteralExpression,
LiteralListExpression,
ParenthesizedExpression,
ToCodeExpression,
declareConst,
declareFinal,
Expand Down
3 changes: 3 additions & 0 deletions lib/src/specs/expression.dart
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ abstract class Expression implements Spec {
/// May be overridden to support other types implementing [Expression].
@visibleForOverriding
Expression get expression => this;

/// Returns this expression wrapped in parenthesis.
ParenthesizedExpression get parenthesized => ParenthesizedExpression._(this);
}

/// Declare a const variable named [variableName].
Expand Down
9 changes: 9 additions & 0 deletions test/specs/code/expression_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -731,4 +731,13 @@ void main() {
.assign(refer('bar')),
equalsDart('late String foo = bar'));
});

test('should emit a perenthesized epression', () {
expect(
refer('foo').ifNullThen(refer('FormatException')
.newInstance([literalString('missing foo')])
.thrown
.parenthesized),
equalsDart('foo ?? (throw FormatException(\'missing foo\'))'));
});
}