From 48e0815842ac97a1dbfda58c276c6aa9e9713e22 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Sat, 4 Feb 2017 16:06:32 -0500 Subject: [PATCH 1/8] 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/8] 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/8] 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!'); + ''')); + }); } From d0b9d0fd6d6fbe52d58357d79206c9d98923ed41 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Wed, 22 Feb 2017 21:53:20 -0500 Subject: [PATCH 4/8] Removed asThrow --- lib/src/builders/expression.dart | 7 ------- lib/src/builders/expression/throw.dart | 22 ---------------------- test/builders/expression_test.dart | 11 ----------- 3 files changed, 40 deletions(-) delete mode 100644 lib/src/builders/expression/throw.dart diff --git a/lib/src/builders/expression.dart b/lib/src/builders/expression.dart index 4565cb4..213c744 100644 --- a/lib/src/builders/expression.dart +++ b/lib/src/builders/expression.dart @@ -29,7 +29,6 @@ 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 = @@ -206,9 +205,6 @@ 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); @@ -375,9 +371,6 @@ 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 deleted file mode 100644 index 5db07c6..0000000 --- a/lib/src/builders/expression/throw.dart +++ /dev/null @@ -1,22 +0,0 @@ -// 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/test/builders/expression_test.dart b/test/builders/expression_test.dart index 21a4876..571d0b9 100644 --- a/test/builders/expression_test.dart +++ b/test/builders/expression_test.dart @@ -404,15 +404,4 @@ void main() { '''), ); }); - - test('should throw an exception', () { - expect( - new TypeBuilder('StateError') - .newInstance([literal('Hey! No!')]) - .asThrow() - .asStatement(), - equalsSource(''' - throw new StateError('Hey! No!'); - ''')); - }); } From 2cd0cd49a8eae77eae9a39e88937ef27bf76f38f Mon Sep 17 00:00:00 2001 From: thosakwe Date: Wed, 14 Jun 2017 21:37:13 -0400 Subject: [PATCH 5/8] Added isInstanceOf, isNotInstanceOf, and corresponding tests. Also added a .gitignore rule to exclude the `.idea` directory, for convenience's sake. --- .gitignore | 3 ++ lib/src/builders/expression.dart | 15 +++++++ .../builders/expression/is_instance_of.dart | 39 +++++++++++++++++++ lib/src/tokens.dart | 3 ++ test/builders/is_instance_of_test.dart | 20 ++++++++++ 5 files changed, 80 insertions(+) create mode 100644 lib/src/builders/expression/is_instance_of.dart create mode 100644 test/builders/is_instance_of_test.dart diff --git a/.gitignore b/.gitignore index ae6cc48..148bb31 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,6 @@ doc/api/ # Don't commit pubspec lock file # (Library packages only! Remove pattern if developing an application package) pubspec.lock + +# Avoid IntelliJ adding junk files to the repo +.idea \ No newline at end of file diff --git a/lib/src/builders/expression.dart b/lib/src/builders/expression.dart index 213c744..4aead7e 100644 --- a/lib/src/builders/expression.dart +++ b/lib/src/builders/expression.dart @@ -26,6 +26,7 @@ part 'expression/cascade.dart'; part 'expression/cast.dart'; part 'expression/index.dart'; part 'expression/invocation.dart'; +part 'expression/is_instance_of.dart'; part 'expression/negate.dart'; part 'expression/operators.dart'; part 'expression/return.dart'; @@ -279,6 +280,14 @@ abstract class AbstractExpressionMixin implements ExpressionBuilder { return invocation; } + @override + ExpressionBuilder isInstanceOf(TypeBuilder type) => + new _IsInstanceOfExpression(this, type); + + @override + ExpressionBuilder isNotInstanceOf(TypeBuilder type) => + new _IsNotInstanceOfExpression(this, type); + @override ExpressionBuilder negate() => new _NegateExpression(this); @@ -422,6 +431,12 @@ abstract class ExpressionBuilder Map namedArguments, }); + /// Returns as an [ExpressionBuilder] indicating whether this expression is an instance of [type], using the `is` operator. + ExpressionBuilder isInstanceOf(TypeBuilder type); + + /// Returns as an [ExpressionBuilder] indicating whether this expression is not an instance of [type], using the `is!` operator. + ExpressionBuilder isNotInstanceOf(TypeBuilder type); + /// Returns as an [ExpressionBuilder] negating using the `!` operator. ExpressionBuilder negate(); diff --git a/lib/src/builders/expression/is_instance_of.dart b/lib/src/builders/expression/is_instance_of.dart new file mode 100644 index 0000000..42867ef --- /dev/null +++ b/lib/src/builders/expression/is_instance_of.dart @@ -0,0 +1,39 @@ +// 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 _IsInstanceOfExpression extends AbstractExpressionMixin + with TopLevelMixin { + final ExpressionBuilder _expression; + final TypeBuilder _type; + + _IsInstanceOfExpression(this._expression, this._type); + + @override + AstNode buildAst([Scope scope]) => buildExpression(scope); + + @override + Expression buildExpression([Scope scope]) { + return astFactory.isExpression( + _expression.buildExpression(scope), $is, null, _type.buildType(scope)); + } +} + +class _IsNotInstanceOfExpression extends AbstractExpressionMixin + with TopLevelMixin { + final ExpressionBuilder _expression; + final TypeBuilder _type; + + _IsNotInstanceOfExpression(this._expression, this._type); + + @override + AstNode buildAst([Scope scope]) => buildExpression(scope); + + @override + Expression buildExpression([Scope scope]) { + return astFactory.isExpression( + _expression.buildExpression(scope), $is, $not, _type.buildType(scope)); + } +} diff --git a/lib/src/tokens.dart b/lib/src/tokens.dart index 1ed0993..7bd9805 100644 --- a/lib/src/tokens.dart +++ b/lib/src/tokens.dart @@ -115,6 +115,9 @@ final Token $implements = new KeywordToken(Keyword.IMPLEMENTS, 0); /// The `in` token. final Token $in = new KeywordToken(Keyword.IN, 0); +/// The `is` token. +final Token $is = new KeywordToken(Keyword.IS, 0); + /// The `library` token. final Token $library = new KeywordToken(Keyword.LIBRARY, 0); diff --git a/test/builders/is_instance_of_test.dart b/test/builders/is_instance_of_test.dart new file mode 100644 index 0000000..3afbd57 --- /dev/null +++ b/test/builders/is_instance_of_test.dart @@ -0,0 +1,20 @@ +// 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. + +import 'package:code_builder/code_builder.dart'; +import 'package:code_builder/testing.dart'; +import 'package:test/test.dart'; + +final TypeBuilder _barType = new TypeBuilder('Bar'); + +void main() { + test('should emit an `is` expression', () { + expect(reference('foo').isInstanceOf(_barType), equalsSource('foo is Bar')); + }); + + test('should emit an `is!` expression', () { + expect(reference('foo').isNotInstanceOf(_barType), + equalsSource('foo is! Bar')); + }); +} From cc4164661268521c5928dc636682a280b24feea5 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Thu, 15 Jun 2017 11:03:51 -0400 Subject: [PATCH 6/8] Undo .gitignore change --- .gitignore | 3 - .idea/code_builder.iml | 20 ++ .idea/libraries/Dart_Packages.xml | 395 ++++++++++++++++++++++++++++++ .idea/libraries/Dart_SDK.xml | 25 ++ .idea/modules.xml | 8 + .idea/vcs.xml | 6 + 6 files changed, 454 insertions(+), 3 deletions(-) create mode 100644 .idea/code_builder.iml create mode 100644 .idea/libraries/Dart_Packages.xml create mode 100644 .idea/libraries/Dart_SDK.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore index 148bb31..ae6cc48 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,3 @@ doc/api/ # Don't commit pubspec lock file # (Library packages only! Remove pattern if developing an application package) pubspec.lock - -# Avoid IntelliJ adding junk files to the repo -.idea \ No newline at end of file diff --git a/.idea/code_builder.iml b/.idea/code_builder.iml new file mode 100644 index 0000000..7540a53 --- /dev/null +++ b/.idea/code_builder.iml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml new file mode 100644 index 0000000..fdfba4f --- /dev/null +++ b/.idea/libraries/Dart_Packages.xml @@ -0,0 +1,395 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Dart_SDK.xml b/.idea/libraries/Dart_SDK.xml new file mode 100644 index 0000000..5194270 --- /dev/null +++ b/.idea/libraries/Dart_SDK.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..a3fbde3 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file From 40c1f69dee16b9dde5d79fe7043502e58398ebd3 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Thu, 15 Jun 2017 11:04:35 -0400 Subject: [PATCH 7/8] Undo .gitignore change, remove IntelliJ metadata --- .idea/code_builder.iml | 20 -- .idea/libraries/Dart_Packages.xml | 395 ------------------------------ .idea/libraries/Dart_SDK.xml | 25 -- .idea/modules.xml | 8 - .idea/vcs.xml | 6 - 5 files changed, 454 deletions(-) delete mode 100644 .idea/code_builder.iml delete mode 100644 .idea/libraries/Dart_Packages.xml delete mode 100644 .idea/libraries/Dart_SDK.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.idea/code_builder.iml b/.idea/code_builder.iml deleted file mode 100644 index 7540a53..0000000 --- a/.idea/code_builder.iml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml deleted file mode 100644 index fdfba4f..0000000 --- a/.idea/libraries/Dart_Packages.xml +++ /dev/null @@ -1,395 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/libraries/Dart_SDK.xml b/.idea/libraries/Dart_SDK.xml deleted file mode 100644 index 5194270..0000000 --- a/.idea/libraries/Dart_SDK.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index a3fbde3..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 4ef710d3e9f7ba9eba1cdf7b9c52998fe47f0eb7 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Thu, 15 Jun 2017 19:57:50 -0400 Subject: [PATCH 8/8] Removed isNotInstanceOf --- lib/src/builders/expression.dart | 7 ------- lib/src/builders/expression/is_instance_of.dart | 17 ----------------- test/builders/is_instance_of_test.dart | 4 ++-- 3 files changed, 2 insertions(+), 26 deletions(-) diff --git a/lib/src/builders/expression.dart b/lib/src/builders/expression.dart index 325b1f5..faa80ef 100644 --- a/lib/src/builders/expression.dart +++ b/lib/src/builders/expression.dart @@ -299,10 +299,6 @@ abstract class AbstractExpressionMixin implements ExpressionBuilder { ExpressionBuilder isInstanceOf(TypeBuilder type) => new _IsInstanceOfExpression(this, type); - @override - ExpressionBuilder isNotInstanceOf(TypeBuilder type) => - new _IsNotInstanceOfExpression(this, type); - @override ExpressionBuilder negate() => new _NegateExpression(this); @@ -459,9 +455,6 @@ abstract class ExpressionBuilder /// Returns as an [ExpressionBuilder] indicating whether this expression is an instance of [type], using the `is` operator. ExpressionBuilder isInstanceOf(TypeBuilder type); - /// Returns as an [ExpressionBuilder] indicating whether this expression is not an instance of [type], using the `is!` operator. - ExpressionBuilder isNotInstanceOf(TypeBuilder type); - /// Returns as an [ExpressionBuilder] negating using the `!` operator. ExpressionBuilder negate(); diff --git a/lib/src/builders/expression/is_instance_of.dart b/lib/src/builders/expression/is_instance_of.dart index 42867ef..9fd38fb 100644 --- a/lib/src/builders/expression/is_instance_of.dart +++ b/lib/src/builders/expression/is_instance_of.dart @@ -20,20 +20,3 @@ class _IsInstanceOfExpression extends AbstractExpressionMixin _expression.buildExpression(scope), $is, null, _type.buildType(scope)); } } - -class _IsNotInstanceOfExpression extends AbstractExpressionMixin - with TopLevelMixin { - final ExpressionBuilder _expression; - final TypeBuilder _type; - - _IsNotInstanceOfExpression(this._expression, this._type); - - @override - AstNode buildAst([Scope scope]) => buildExpression(scope); - - @override - Expression buildExpression([Scope scope]) { - return astFactory.isExpression( - _expression.buildExpression(scope), $is, $not, _type.buildType(scope)); - } -} diff --git a/test/builders/is_instance_of_test.dart b/test/builders/is_instance_of_test.dart index 3afbd57..20bbf6c 100644 --- a/test/builders/is_instance_of_test.dart +++ b/test/builders/is_instance_of_test.dart @@ -14,7 +14,7 @@ void main() { }); test('should emit an `is!` expression', () { - expect(reference('foo').isNotInstanceOf(_barType), - equalsSource('foo is! Bar')); + expect(reference('foo').isInstanceOf(_barType).negate(), + equalsSource('!(foo is Bar)')); }); }