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

Commit 258fbc8

Browse files
authored
Add conditional operator (#172)
1 parent c2768bb commit 258fbc8

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ void main() {
8585
```
8686

8787
* Added `nullSafeProperty` to `Expression` to access properties with `?.`
88+
* Added `conditional` to `Expression` to use the ternary operator `? : `
8889

8990
## 2.2.0
9091

lib/src/specs/expression.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ abstract class Expression implements Spec {
7676
);
7777
}
7878

79+
Expression conditional(Expression whenTrue, Expression whenFalse) {
80+
return new BinaryExpression._(
81+
expression,
82+
new BinaryExpression._(whenTrue, whenFalse, ':'),
83+
'?',
84+
);
85+
}
86+
7987
/// This expression preceded by `await`.
8088
Expression get awaited {
8189
return new BinaryExpression._(

test/specs/code/expression_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,11 @@ void main() {
358358
equalsDart('foo is! String'),
359359
);
360360
});
361+
362+
test('should emit a conditional', () {
363+
expect(
364+
refer('foo').conditional(literal(1), literal(2)),
365+
equalsDart('foo ? 1 : 2'),
366+
);
367+
});
361368
}

0 commit comments

Comments
 (0)