Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
## 2.1.1-dev
## 2.2.0-dev

* Imports are prefixed with `_i1` rather than `_1` which satisfies the lint
`lowercase_with_underscores`.
`lowercase_with_underscores`. While not a strictly breaking change you may
have to fix/regenerate golden file-like tests. We added documentation that
the specific prefix is not considered stable.

* Added `Expression.index` for accessing the `[]` operator:

```dart
void main() {
test('should emit an index operator', () {
expect(
refer('bar').index(literalTrue).assignVar('foo').statement,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] passing a boolean to the index operator isn't something I've ever seen - do we want a more common example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used literalString. Thanks!

equalsDart('var foo = bar[true];'),
);
} );

test('should emit an index operator set', () {
expect(
refer('bar')
.index(literalTrue)
.assign(literalFalse)
.assignVar('foo')
.statement,
equalsDart('var foo = bar[true] = false;'),
);
});
}
```

* `literalList` accepts an `Iterable` argument.

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

/// Returns accessing the index operator (`[]`) on `this`.
Expression index(Expression index) {
return new BinaryExpression._(
expression,
new CodeExpression(new Block.of([
const Code('['),
index.code,
const Code(']'),
])),
'',
);
}

/// This expression preceded by `await`.
Expression get awaited {
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: 2.1.1-dev
version: 2.2.0-dev
description: A fluent API for generating Dart code
author: Dart Team <misc@dartlang.org>
homepage: https://github.com/dart-lang/code_builder
Expand Down
29 changes: 29 additions & 0 deletions test/specs/code/expression_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,35 @@ void main() {
);
});

test('should emit an index operator', () {
expect(
refer('bar').index(literalString('key')).assignVar('foo').statement,
equalsDart("var foo = bar['key'];"),
);
});

test('should emit an index operator set', () {
expect(
refer('bar')
.index(literalString('key'))
.assign(literalFalse)
.assignVar('foo')
.statement,
equalsDart("var foo = bar['key'] = false;"),
);
});

test('should emit a null-aware index operator set', () {
expect(
refer('bar')
.index(literalTrue)
.assignNullAware(literalFalse)
.assignVar('foo')
.statement,
equalsDart('var foo = bar[true] ??= false;'),
);
});

test('should emit assigning to a var', () {
expect(
literalTrue.assignVar('foo'),
Expand Down