Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit 4f32297

Browse files
thosakwematanlurey
authored andcommitted
Add support for is, is! expressions (#111)
* FieldBuilders now works to-level * Ran dartfmt * asThrow on ExpressionBuilder * Removed asThrow * Added isInstanceOf, isNotInstanceOf, and corresponding tests. Also added a .gitignore rule to exclude the `.idea` directory, for convenience's sake. * Undo .gitignore change * Undo .gitignore change, remove IntelliJ metadata * Removed isNotInstanceOf
1 parent 731c18b commit 4f32297

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

lib/src/builders/expression.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ part 'expression/cascade.dart';
2626
part 'expression/cast.dart';
2727
part 'expression/index.dart';
2828
part 'expression/invocation.dart';
29+
part 'expression/is_instance_of.dart';
2930
part 'expression/negate.dart';
3031
part 'expression/operators.dart';
3132
part 'expression/return.dart';
@@ -294,6 +295,10 @@ abstract class AbstractExpressionMixin implements ExpressionBuilder {
294295
return invocation;
295296
}
296297

298+
@override
299+
ExpressionBuilder isInstanceOf(TypeBuilder type) =>
300+
new _IsInstanceOfExpression(this, type);
301+
297302
@override
298303
ExpressionBuilder negate() => new _NegateExpression(this);
299304

@@ -447,6 +452,9 @@ abstract class ExpressionBuilder
447452
Map<String, ExpressionBuilder> namedArguments,
448453
});
449454

455+
/// Returns as an [ExpressionBuilder] indicating whether this expression is an instance of [type], using the `is` operator.
456+
ExpressionBuilder isInstanceOf(TypeBuilder type);
457+
450458
/// Returns as an [ExpressionBuilder] negating using the `!` operator.
451459
ExpressionBuilder negate();
452460

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
part of code_builder.src.builders.expression;
6+
7+
class _IsInstanceOfExpression extends AbstractExpressionMixin
8+
with TopLevelMixin {
9+
final ExpressionBuilder _expression;
10+
final TypeBuilder _type;
11+
12+
_IsInstanceOfExpression(this._expression, this._type);
13+
14+
@override
15+
AstNode buildAst([Scope scope]) => buildExpression(scope);
16+
17+
@override
18+
Expression buildExpression([Scope scope]) {
19+
return astFactory.isExpression(
20+
_expression.buildExpression(scope), $is, null, _type.buildType(scope));
21+
}
22+
}

lib/src/tokens.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ final Token $implements = new KeywordToken(Keyword.IMPLEMENTS, 0);
118118
/// The `in` token.
119119
final Token $in = new KeywordToken(Keyword.IN, 0);
120120

121+
/// The `is` token.
122+
final Token $is = new KeywordToken(Keyword.IS, 0);
123+
121124
/// The `library` token.
122125
final Token $library = new KeywordToken(Keyword.LIBRARY, 0);
123126

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:code_builder/code_builder.dart';
6+
import 'package:code_builder/testing.dart';
7+
import 'package:test/test.dart';
8+
9+
final TypeBuilder _barType = new TypeBuilder('Bar');
10+
11+
void main() {
12+
test('should emit an `is` expression', () {
13+
expect(reference('foo').isInstanceOf(_barType), equalsSource('foo is Bar'));
14+
});
15+
16+
test('should emit an `is!` expression', () {
17+
expect(reference('foo').isInstanceOf(_barType).negate(),
18+
equalsSource('!(foo is Bar)'));
19+
});
20+
}

0 commit comments

Comments
 (0)