From 0f19ef946d4365c8fc4964dc1bdac70a7922f8df Mon Sep 17 00:00:00 2001 From: Matan Lurey Date: Tue, 14 Nov 2017 19:05:29 -0800 Subject: [PATCH 1/2] Add index operator access to Expression. --- CHANGELOG.md | 29 ++++++++++++++++++++++++++-- lib/src/specs/expression.dart | 13 +++++++++++++ pubspec.yaml | 2 +- test/specs/code/expression_test.dart | 29 ++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e94ccd7..94019aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,32 @@ -## 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, + 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;'), + ); + }); +} +``` ## 2.1.0 diff --git a/lib/src/specs/expression.dart b/lib/src/specs/expression.dart index c1ae450..50d3ec6 100644 --- a/lib/src/specs/expression.dart +++ b/lib/src/specs/expression.dart @@ -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._( diff --git a/pubspec.yaml b/pubspec.yaml index 2407864..0e58701 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 homepage: https://github.com/dart-lang/code_builder diff --git a/test/specs/code/expression_test.dart b/test/specs/code/expression_test.dart index 3aa724c..72ced05 100644 --- a/test/specs/code/expression_test.dart +++ b/test/specs/code/expression_test.dart @@ -266,6 +266,35 @@ void main() { ); }); + test('should emit an index operator', () { + expect( + refer('bar').index(literalTrue).assignVar('foo').statement, + 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;'), + ); + }); + + 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'), From 60491982246aba42f488337b4d8eea5f7417cb54 Mon Sep 17 00:00:00 2001 From: Matan Lurey Date: Mon, 20 Nov 2017 20:36:43 -0800 Subject: [PATCH 2/2] Address feedback. --- test/specs/code/expression_test.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/specs/code/expression_test.dart b/test/specs/code/expression_test.dart index 72ced05..7cf5845 100644 --- a/test/specs/code/expression_test.dart +++ b/test/specs/code/expression_test.dart @@ -268,19 +268,19 @@ void main() { test('should emit an index operator', () { expect( - refer('bar').index(literalTrue).assignVar('foo').statement, - equalsDart('var foo = bar[true];'), + refer('bar').index(literalString('key')).assignVar('foo').statement, + equalsDart("var foo = bar['key'];"), ); }); test('should emit an index operator set', () { expect( refer('bar') - .index(literalTrue) + .index(literalString('key')) .assign(literalFalse) .assignVar('foo') .statement, - equalsDart('var foo = bar[true] = false;'), + equalsDart("var foo = bar['key'] = false;"), ); });