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

As throw #85

Merged
merged 3 commits into from
Feb 24, 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
7 changes: 7 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/throw.dart';
part 'expression/yield.dart';

final _false =
Expand Down Expand Up @@ -205,6 +206,9 @@ abstract class AbstractExpressionMixin implements ExpressionBuilder {
@override
StatementBuilder asYieldStar() => new _AsYield(this, true);

@override
ExpressionBuilder asThrow() => new _ThrowExpression(this);

@override
WhileStatementBuilder asWhile({bool asDo: false}) {
return new WhileStatementBuilder(asDo, this);
Expand Down Expand Up @@ -371,6 +375,9 @@ abstract class ExpressionBuilder
/// [Statement] AST instead of an expression.
StatementBuilder asStatement();

/// Returns as an [ExpressionBuilder] that throws this expression as an error.
ExpressionBuilder asThrow();

/// Returns as a [StatementBuilder] that assigns to a new `var` [variable].
///
/// If [type] is supplied, the resulting statement is `{type} {variable} =`.
Expand Down
22 changes: 22 additions & 0 deletions lib/src/builders/expression/throw.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2016, 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 _ThrowExpression extends AbstractExpressionMixin with TopLevelMixin {
final ExpressionBuilder _value;

_ThrowExpression(this._value);

@override
ExpressionBuilder asThrow() => this;

@override
AstNode buildAst([Scope scope]) => buildExpression(scope);

@override
Expression buildExpression([Scope scope]) {
return astFactory.throwExpression($throw, _value.buildExpression(scope));
}
}
9 changes: 6 additions & 3 deletions lib/src/builders/field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ abstract class FieldBuilder
TopLevelVariableDeclaration buildTopLevel([Scope scope]);
}

class _FieldBuilderImpl extends TopLevelMixin
with HasAnnotationsMixin
implements FieldBuilder {
class _FieldBuilderImpl extends HasAnnotationsMixin implements FieldBuilder {
final Keyword _keyword;
final String _name;
final TypeBuilder _type;
Expand Down Expand Up @@ -157,6 +155,11 @@ class _FieldBuilderImpl extends TopLevelMixin
);
}

@override
CompilationUnitMember buildTopLevelAst([Scope scope]) {
return buildTopLevel(scope);
}

VariableDeclarationList _buildVariableList([Scope scope]) {
return astFactory.variableDeclarationList(
null,
Expand Down
3 changes: 3 additions & 0 deletions lib/src/tokens.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ final Token $static = new KeywordToken(Keyword.STATIC, 0);
/// The `this` token.
final Token $this = new KeywordToken(Keyword.THIS, 0);

/// The `throw` token.
final Token $throw = new KeywordToken(Keyword.THROW, 0);

/// The `true` token.
final Token $true = new KeywordToken(Keyword.TRUE, 0);

Expand Down
11 changes: 11 additions & 0 deletions test/builders/expression_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,15 @@ void main() {
'''),
);
});

test('should throw an exception', () {
expect(
new TypeBuilder('StateError')
.newInstance([literal('Hey! No!')])
.asThrow()
.asStatement(),
equalsSource('''
throw new StateError('Hey! No!');
'''));
});
}