diff --git a/lib/src/builders/expression.dart b/lib/src/builders/expression.dart index dbb32cb..faa80ef 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'; @@ -294,6 +295,10 @@ abstract class AbstractExpressionMixin implements ExpressionBuilder { return invocation; } + @override + ExpressionBuilder isInstanceOf(TypeBuilder type) => + new _IsInstanceOfExpression(this, type); + @override ExpressionBuilder negate() => new _NegateExpression(this); @@ -447,6 +452,9 @@ 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] 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..9fd38fb --- /dev/null +++ b/lib/src/builders/expression/is_instance_of.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 _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)); + } +} diff --git a/lib/src/tokens.dart b/lib/src/tokens.dart index d47f5b1..65a6207 100644 --- a/lib/src/tokens.dart +++ b/lib/src/tokens.dart @@ -118,6 +118,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..20bbf6c --- /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').isInstanceOf(_barType).negate(), + equalsSource('!(foo is Bar)')); + }); +}