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

Commit 25571e3

Browse files
authored
Add index operator access to Expression. (#161)
* Add index operator access to Expression. * Address feedback.
1 parent 4403aaa commit 25571e3

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

CHANGELOG.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
1-
## 2.1.1-dev
1+
## 2.2.0-dev
22

33
* Imports are prefixed with `_i1` rather than `_1` which satisfies the lint
4-
`lowercase_with_underscores`.
4+
`lowercase_with_underscores`. While not a strictly breaking change you may
5+
have to fix/regenerate golden file-like tests. We added documentation that
6+
the specific prefix is not considered stable.
7+
8+
* Added `Expression.index` for accessing the `[]` operator:
9+
10+
```dart
11+
void main() {
12+
test('should emit an index operator', () {
13+
expect(
14+
refer('bar').index(literalTrue).assignVar('foo').statement,
15+
equalsDart('var foo = bar[true];'),
16+
);
17+
} );
18+
19+
test('should emit an index operator set', () {
20+
expect(
21+
refer('bar')
22+
.index(literalTrue)
23+
.assign(literalFalse)
24+
.assignVar('foo')
25+
.statement,
26+
equalsDart('var foo = bar[true] = false;'),
27+
);
28+
});
29+
}
30+
```
31+
532
* `literalList` accepts an `Iterable` argument.
633

734
## 2.1.0

lib/src/specs/expression.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ abstract class Expression implements Spec {
4545
return new BinaryExpression._(expression, other, '&&');
4646
}
4747

48+
/// Returns accessing the index operator (`[]`) on `this`.
49+
Expression index(Expression index) {
50+
return new BinaryExpression._(
51+
expression,
52+
new CodeExpression(new Block.of([
53+
const Code('['),
54+
index.code,
55+
const Code(']'),
56+
])),
57+
'',
58+
);
59+
}
60+
4861
/// This expression preceded by `await`.
4962
Expression get awaited {
5063
return new BinaryExpression._(

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: code_builder
2-
version: 2.1.1-dev
2+
version: 2.2.0-dev
33
description: A fluent API for generating Dart code
44
author: Dart Team <[email protected]>
55
homepage: https://github.com/dart-lang/code_builder

test/specs/code/expression_test.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,35 @@ void main() {
266266
);
267267
});
268268

269+
test('should emit an index operator', () {
270+
expect(
271+
refer('bar').index(literalString('key')).assignVar('foo').statement,
272+
equalsDart("var foo = bar['key'];"),
273+
);
274+
});
275+
276+
test('should emit an index operator set', () {
277+
expect(
278+
refer('bar')
279+
.index(literalString('key'))
280+
.assign(literalFalse)
281+
.assignVar('foo')
282+
.statement,
283+
equalsDart("var foo = bar['key'] = false;"),
284+
);
285+
});
286+
287+
test('should emit a null-aware index operator set', () {
288+
expect(
289+
refer('bar')
290+
.index(literalTrue)
291+
.assignNullAware(literalFalse)
292+
.assignVar('foo')
293+
.statement,
294+
equalsDart('var foo = bar[true] ??= false;'),
295+
);
296+
});
297+
269298
test('should emit assigning to a var', () {
270299
expect(
271300
literalTrue.assignVar('foo'),

0 commit comments

Comments
 (0)