Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Add ExpressionBuilder#ternary. #97

Merged
merged 4 commits into from
Mar 29, 2017
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
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: dart

dart:
# pkg/test is currently failing @ dev
# - dev
- 1.22.0
- dev
- stable

script: ./tool/presubmit.sh
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.0-beta+7

- Added `ExpressionBuilder#ternary`.

## 1.0.0-beta+6

- Added `TypeDefBuilder`.
Expand Down
14 changes: 14 additions & 0 deletions lib/src/builders/expression.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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].
Expand Down
31 changes: 31 additions & 0 deletions lib/src/builders/expression/ternary.dart
Original file line number Diff line number Diff line change
@@ -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),
);
}
}
3 changes: 3 additions & 0 deletions lib/src/tokens.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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: 1.0.0-beta+6
version: 1.0.0-beta+7
description: A fluent API for generating Dart code
author: Dart Team <[email protected]>
homepage: https://github.com/dart-lang/code_builder
Expand Down
9 changes: 9 additions & 0 deletions test/builders/expression_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
'''),
);
});
}
5 changes: 2 additions & 3 deletions test/e2e_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion tool/presubmit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down