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 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..dbb32cb 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,13 @@ 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 +464,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..5a93a3c --- /dev/null +++ b/lib/src/builders/expression/ternary.dart @@ -0,0 +1,31 @@ +// 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 + '''), + ); + }); } 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( 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"