From 48e0815842ac97a1dbfda58c276c6aa9e9713e22 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Sat, 4 Feb 2017 16:06:32 -0500 Subject: [PATCH 1/3] FieldBuilders now works to-level --- lib/src/builders/field.dart | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/src/builders/field.dart b/lib/src/builders/field.dart index 5964e4a..971de13 100644 --- a/lib/src/builders/field.dart +++ b/lib/src/builders/field.dart @@ -106,8 +106,7 @@ abstract class FieldBuilder TopLevelVariableDeclaration buildTopLevel([Scope scope]); } -class _FieldBuilderImpl extends TopLevelMixin - with HasAnnotationsMixin +class _FieldBuilderImpl extends HasAnnotationsMixin implements FieldBuilder { final Keyword _keyword; final String _name; @@ -157,6 +156,11 @@ class _FieldBuilderImpl extends TopLevelMixin ); } + @override + CompilationUnitMember buildTopLevelAst([Scope scope]) { + return buildTopLevel(scope); + } + VariableDeclarationList _buildVariableList([Scope scope]) { return astFactory.variableDeclarationList( null, From 209182fff1114d11c9acc8832346da42d405fa00 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Sat, 4 Feb 2017 16:10:55 -0500 Subject: [PATCH 2/3] Ran dartfmt --- lib/src/builders/field.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/src/builders/field.dart b/lib/src/builders/field.dart index 971de13..1424463 100644 --- a/lib/src/builders/field.dart +++ b/lib/src/builders/field.dart @@ -106,8 +106,7 @@ abstract class FieldBuilder TopLevelVariableDeclaration buildTopLevel([Scope scope]); } -class _FieldBuilderImpl extends HasAnnotationsMixin - implements FieldBuilder { +class _FieldBuilderImpl extends HasAnnotationsMixin implements FieldBuilder { final Keyword _keyword; final String _name; final TypeBuilder _type; From d78246c310737fd9c6f42513ca2258a4fb46cdc4 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Sat, 4 Feb 2017 16:57:11 -0500 Subject: [PATCH 3/3] asThrow on ExpressionBuilder --- lib/src/builders/expression.dart | 7 +++++++ lib/src/builders/expression/throw.dart | 22 ++++++++++++++++++++++ lib/src/tokens.dart | 3 +++ test/builders/expression_test.dart | 11 +++++++++++ 4 files changed, 43 insertions(+) create mode 100644 lib/src/builders/expression/throw.dart diff --git a/lib/src/builders/expression.dart b/lib/src/builders/expression.dart index 213c744..4565cb4 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/throw.dart'; part 'expression/yield.dart'; final _false = @@ -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); @@ -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} =`. diff --git a/lib/src/builders/expression/throw.dart b/lib/src/builders/expression/throw.dart new file mode 100644 index 0000000..5db07c6 --- /dev/null +++ b/lib/src/builders/expression/throw.dart @@ -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)); + } +} diff --git a/lib/src/tokens.dart b/lib/src/tokens.dart index 88687a6..1ed0993 100644 --- a/lib/src/tokens.dart +++ b/lib/src/tokens.dart @@ -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); diff --git a/test/builders/expression_test.dart b/test/builders/expression_test.dart index 571d0b9..21a4876 100644 --- a/test/builders/expression_test.dart +++ b/test/builders/expression_test.dart @@ -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!'); + ''')); + }); }