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

Commit

Permalink
add Expression.asA (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakemac53 authored Jun 1, 2018
1 parent 0ad3ff1 commit 1dbe8d6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
21 changes: 18 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
## 3.0.4

* Added `Expression.asA` for creating explicit casts:

```dart
void main() {
test('should emit an explicit cast', () {
expect(
refer('foo').asA(refer('String')),
equalsDart('foo as String'),
);
});
}
```

## 3.0.3

* Fix a bug that caused all downstream users of `code_builder` to crash due to
Expand Down Expand Up @@ -314,7 +329,7 @@ are welcome!
* `const Code.scope((allocate) => '')`

* Removed `SimpleSpecVisitor` (it was unused).
* Removed `implements Reference` from `Method` and `Field`; not a lot of value.
* Removed `implements Reference` from `Method` and `Field`; not a lot of value.

* `SpecVisitor<T>`'s methods all have an optional `[T context]` parameter now.
* This makes it much easier to avoid allocating extra `StringBuffer`s.
Expand Down Expand Up @@ -353,7 +368,7 @@ final animal = new Class((b) => b

```dart
expect(
reference('foo').isInstanceOf(_barType),
reference('foo').isInstanceOf(_barType),
equalsSource('foo is Bar'),
);
```
Expand Down Expand Up @@ -403,7 +418,7 @@ that the entire Dart language is buildable with our API, though.
- Added `ConstructorBuilder.redirectTo` for a redirecting factory constructor.
- Added a `name` getter to `ReferenceBuilder`.
- Supplying an empty constructor name (`''`) is equivalent to `null` (default).
- Automatically encodes string literals with multiple lines as `'''`.
- Automatically encodes string literals with multiple lines as `'''`.
- Added `asThrow` to `ExpressionBuilder`.
- Fixed a bug that prevented `FieldBuilder` from being used at the top-level.

Expand Down
9 changes: 9 additions & 0 deletions lib/src/specs/expression.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ abstract class Expression implements Spec {
return new BinaryExpression._(expression, other, '&&');
}

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

/// Returns accessing the index operator (`[]`) on `this`.
Expression index(Expression index) {
return new BinaryExpression._(
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.0.4-dev
version: 3.0.4
description: A fluent API for generating Dart code
author: Dart Team <[email protected]>
homepage: https://github.com/dart-lang/code_builder
Expand Down
7 changes: 7 additions & 0 deletions test/specs/code/expression_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,13 @@ void main() {
);
});

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

test('should emit an is check', () {
expect(
refer('foo').isA(refer('String')),
Expand Down

0 comments on commit 1dbe8d6

Please sign in to comment.