Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
Add support for the postfix ! operator (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 authored Jun 23, 2021
1 parent 375d729 commit 1426c2d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
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 `Expression.spread` for the spread operator `...`.
* Add support 'late' field modifier.
* Add support for `Expression.nullChecked` to add a null assertion operator.

## 4.0.0

Expand Down
11 changes: 11 additions & 0 deletions lib/src/specs/expression.dart
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,17 @@ abstract class Expression implements Spec {
addSpace: false,
);

/// Applies the null check operator on this expression, returning `this` `!`.
///
/// Please note that this is only valid when emitting code with the null
/// safety syntax enabled.
Expression get nullChecked => BinaryExpression._(
this,
const LiteralExpression._('!'),
'',
addSpace: false,
);

/// This expression preceded by `return`.
Expression get returned => BinaryExpression._(
const LiteralExpression._('return'),
Expand Down
4 changes: 4 additions & 0 deletions test/specs/code/expression_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ void main() {
);
});

test('should emit a null check', () {
expect(refer('foo').nullChecked, equalsDart('foo!'));
});

test('should emit an if null index operator set', () {
expect(
refer('bar')
Expand Down

0 comments on commit 1426c2d

Please sign in to comment.