Skip to content

Commit

Permalink
Wrap as expressions with parens (#214)
Browse files Browse the repository at this point in the history
Closes #213
  • Loading branch information
natebosch authored and matanlurey committed Jun 26, 2018
1 parent 3f098d4 commit 823aff9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 3.1.1

* `Expression.asA` is now wrapped with parenthesis so that further calls may be
made on it as an expression.


## 3.1.0

* Added `Expression.asA` for creating explicit casts:
Expand Down
14 changes: 9 additions & 5 deletions lib/src/specs/expression.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ abstract class Expression implements Spec {

/// Returns the result of `this` `as` [other].
Expression asA(Expression other) {
return new BinaryExpression._(
expression,
other,
'as',
);
return new CodeExpression(new Block.of([
const Code('('),
new BinaryExpression._(
expression,
other,
'as',
).code,
const Code(')')
]));
}

/// Returns accessing the index operator (`[]`) on `this`.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: code_builder
version: 3.1.0
version: 3.1.1
description: A fluent API for generating Dart code
author: Dart Team <misc@dartlang.org>
homepage: https://github.com/dart-lang/code_builder
Expand Down
4 changes: 2 additions & 2 deletions test/specs/code/expression_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ void main() {

test('should emit an explicit cast', () {
expect(
refer('foo').asA(refer('String')),
equalsDart('foo as String'),
refer('foo').asA(refer('String')).property('length'),
equalsDart('( foo as String ).length'),
);
});

Expand Down

0 comments on commit 823aff9

Please sign in to comment.