Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit a51bd20

Browse files
authored
Add Expression.parenthesized (#425)
We use `ParenthesizedExpression` internally in a few places where it's know that they are required, but some corners of the syntax require extra parenthesis in ways that are not feasible to detect in this library. Allow for explicit manual parenthesis.
1 parent 7707960 commit a51bd20

File tree

4 files changed

+14
-0
lines changed

4 files changed

+14
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

67
## 4.5.0
78

lib/code_builder.dart

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export 'src/specs/expression.dart'
2525
InvokeExpressionType,
2626
LiteralExpression,
2727
LiteralListExpression,
28+
ParenthesizedExpression,
2829
ToCodeExpression,
2930
declareConst,
3031
declareFinal,

lib/src/specs/expression.dart

+3
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ abstract class Expression implements Spec {
331331
/// May be overridden to support other types implementing [Expression].
332332
@visibleForOverriding
333333
Expression get expression => this;
334+
335+
/// Returns this expression wrapped in parenthesis.
336+
ParenthesizedExpression get parenthesized => ParenthesizedExpression._(this);
334337
}
335338

336339
/// Declare a const variable named [variableName].

test/specs/code/expression_test.dart

+9
Original file line numberDiff line numberDiff line change
@@ -731,4 +731,13 @@ void main() {
731731
.assign(refer('bar')),
732732
equalsDart('late String foo = bar'));
733733
});
734+
735+
test('should emit a perenthesized epression', () {
736+
expect(
737+
refer('foo').ifNullThen(refer('FormatException')
738+
.newInstance([literalString('missing foo')])
739+
.thrown
740+
.parenthesized),
741+
equalsDart('foo ?? (throw FormatException(\'missing foo\'))'));
742+
});
734743
}

0 commit comments

Comments
 (0)