From d3247a9edd795a3ec125bf703f379ba55669442a Mon Sep 17 00:00:00 2001 From: Matan Lurey Date: Thu, 16 Mar 2017 23:28:54 -0700 Subject: [PATCH 1/4] Add ExpressionBuilder#ternary. --- CHANGELOG.md | 4 ++++ lib/src/builders/expression.dart | 13 ++++++++++++ lib/src/builders/expression/ternary.dart | 27 ++++++++++++++++++++++++ lib/src/tokens.dart | 3 +++ pubspec.yaml | 2 +- test/builders/expression_test.dart | 9 ++++++++ 6 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 lib/src/builders/expression/ternary.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 69cdf0b..5c1bcf9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.0.0-beta+7 + +- Added `ExpressionBuilder#ternary`. + ## 1.0.0-beta+6 - Added `TypeDefBuilder`. diff --git a/lib/src/builders/expression.dart b/lib/src/builders/expression.dart index 24d0f62..5f81c70 100644 --- a/lib/src/builders/expression.dart +++ b/lib/src/builders/expression.dart @@ -29,6 +29,7 @@ part 'expression/invocation.dart'; part 'expression/negate.dart'; part 'expression/operators.dart'; part 'expression/return.dart'; +part 'expression/ternary.dart'; part 'expression/throw.dart'; part 'expression/yield.dart'; @@ -322,6 +323,12 @@ abstract class AbstractExpressionMixin implements ExpressionBuilder { @override ExpressionBuilder property(String name) => new _MemberExpression(this, name); + + @override + ExpressionBuilder ternary( + ExpressionBuilder ifTrue, + ExpressionBuilder ifFalse, + ) => new _TernaryExpression(this, ifTrue, ifFalse); } /// Builds an [Expression] AST when [buildExpression] is invoked. @@ -456,6 +463,12 @@ abstract class ExpressionBuilder /// Returns {{this}}.{{name}}. ExpressionBuilder property(String name); + + /// Returns {{this}} ? {{ifTrue}} : {{ifFalse}}. + ExpressionBuilder ternary( + ExpressionBuilder ifTrue, + ExpressionBuilder ifFalse, + ); } /// An [AstBuilder] that can add [ExpressionBuilder]. diff --git a/lib/src/builders/expression/ternary.dart b/lib/src/builders/expression/ternary.dart new file mode 100644 index 0000000..25a208c --- /dev/null +++ b/lib/src/builders/expression/ternary.dart @@ -0,0 +1,27 @@ +// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +part of code_builder.src.builders.expression; + +class _TernaryExpression extends TopLevelMixin with AbstractExpressionMixin { + final ExpressionBuilder _target; + final ExpressionBuilder _ifTrue; + final ExpressionBuilder _ifFalse; + + _TernaryExpression(this._target, this._ifTrue, this._ifFalse,); + + @override + AstNode buildAst([Scope scope]) => buildExpression(scope); + + @override + Expression buildExpression([Scope scope]) { + return astFactory.conditionalExpression( + _target.buildExpression(scope), + $question, + _ifTrue.buildExpression(scope), + $colon, + _ifFalse.buildExpression(scope), + ); + } +} diff --git a/lib/src/tokens.dart b/lib/src/tokens.dart index 4b7b15e..b0464dc 100644 --- a/lib/src/tokens.dart +++ b/lib/src/tokens.dart @@ -202,6 +202,9 @@ final Token $var = new KeywordToken(Keyword.VAR, 0); /// The `with` token. final Token $with = new KeywordToken(Keyword.WITH, 0); +/// The `?` token. +final Token $question = new Token(TokenType.QUESTION, 0); + /// Returns an int token for the given int [value]. StringToken intToken(int value) => new StringToken(TokenType.INT, '$value', 0); diff --git a/pubspec.yaml b/pubspec.yaml index 0ec2d77..ceae7f9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: code_builder -version: 1.0.0-beta+6 +version: 1.0.0-beta+7 description: A fluent API for generating Dart code author: Dart Team homepage: https://github.com/dart-lang/code_builder diff --git a/test/builders/expression_test.dart b/test/builders/expression_test.dart index 6467d4b..39d8354 100644 --- a/test/builders/expression_test.dart +++ b/test/builders/expression_test.dart @@ -437,4 +437,13 @@ void main() { throw new StateError('Hey! No!'); ''')); }); + + test('should create a ternary condition ? ifTrue : ifFalse', () { + expect( + reference('someValue').ternary(literal(true), literal(false)), + equalsSource(r''' + someValue ? true : false + '''), + ); + }); } From 6b7add6f94f59016f59918fd1f9b03dddba1c8f5 Mon Sep 17 00:00:00 2001 From: Matan Lurey Date: Wed, 29 Mar 2017 12:50:04 -0700 Subject: [PATCH 2/4] Run dartfmt. --- lib/src/builders/expression.dart | 3 ++- lib/src/builders/expression/ternary.dart | 6 +++++- test/e2e_test.dart | 5 ++--- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/src/builders/expression.dart b/lib/src/builders/expression.dart index 5f81c70..dbb32cb 100644 --- a/lib/src/builders/expression.dart +++ b/lib/src/builders/expression.dart @@ -328,7 +328,8 @@ abstract class AbstractExpressionMixin implements ExpressionBuilder { ExpressionBuilder ternary( ExpressionBuilder ifTrue, ExpressionBuilder ifFalse, - ) => new _TernaryExpression(this, ifTrue, ifFalse); + ) => + new _TernaryExpression(this, ifTrue, ifFalse); } /// Builds an [Expression] AST when [buildExpression] is invoked. diff --git a/lib/src/builders/expression/ternary.dart b/lib/src/builders/expression/ternary.dart index 25a208c..5a93a3c 100644 --- a/lib/src/builders/expression/ternary.dart +++ b/lib/src/builders/expression/ternary.dart @@ -9,7 +9,11 @@ class _TernaryExpression extends TopLevelMixin with AbstractExpressionMixin { final ExpressionBuilder _ifTrue; final ExpressionBuilder _ifFalse; - _TernaryExpression(this._target, this._ifTrue, this._ifFalse,); + _TernaryExpression( + this._target, + this._ifTrue, + this._ifFalse, + ); @override AstNode buildAst([Scope scope]) => buildExpression(scope); diff --git a/test/e2e_test.dart b/test/e2e_test.dart index 482eccf..356e25e 100644 --- a/test/e2e_test.dart +++ b/test/e2e_test.dart @@ -88,9 +88,8 @@ void main() { ..addMethod(new MethodBuilder( 'instantiateAndReturnNamedThing', returnType: thingRef, - ) - ..addStatement( - thingRef.newInstance([], constructor: 'named').asReturn()))); + )..addStatement( + thingRef.newInstance([], constructor: 'named').asReturn()))); expect( lib, equalsSource( From 0ca597b5788c1de8ea36f57edb5615a22f480bff Mon Sep 17 00:00:00 2001 From: Matan Lurey Date: Wed, 29 Mar 2017 13:17:05 -0700 Subject: [PATCH 3/4] Dartfmt and use the SDK version. --- tool/presubmit.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tool/presubmit.sh b/tool/presubmit.sh index 2c7a343..8606c34 100755 --- a/tool/presubmit.sh +++ b/tool/presubmit.sh @@ -3,7 +3,7 @@ # Make sure dartfmt is run on everything # This assumes you have dart_style as a dev_dependency echo "Checking dartfmt..." -NEEDS_DARTFMT="$(find lib test -name "*.dart" | xargs pub run dart_style:format -n)" +NEEDS_DARTFMT="$(find lib test -name "*.dart" | xargs dartfmt -n)" if [[ ${NEEDS_DARTFMT} != "" ]] then echo "FAILED" From 72460ec7777b57825b9cce5a5eb07d820fe57dfc Mon Sep 17 00:00:00 2001 From: Matan Lurey Date: Wed, 29 Mar 2017 13:18:35 -0700 Subject: [PATCH 4/4] Run travis on more SDKs. --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index fcb7af4..1470e94 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: dart dart: - # pkg/test is currently failing @ dev - # - dev + - 1.22.0 + - dev - stable script: ./tool/presubmit.sh