File tree Expand file tree Collapse file tree 7 files changed +45
-25
lines changed
analysis_server/lib/src/services/completion/dart
analyzer_plugin/lib/src/utilities/completion
test/src/utilities/extensions Expand file tree Collapse file tree 7 files changed +45
-25
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ import 'package:analyzer/dart/element/nullability_suffix.dart';
2323import 'package:analyzer/dart/element/type.dart' ;
2424import 'package:analyzer/src/dart/element/element.dart' ;
2525import 'package:analyzer/src/dartdoc/dartdoc_directive_info.dart' ;
26+ import 'package:analyzer/src/utilities/extensions/object.dart' ;
2627import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart' ;
2728import 'package:analyzer_plugin/utilities/range_factory.dart' ;
2829
@@ -1645,11 +1646,3 @@ class _ElementDocumentation {
16451646 required this .summary,
16461647 });
16471648}
1648-
1649- extension on Object ? {
1650- /// If the target is [T] , return it, otherwise `null` .
1651- T ? ifTypeOrNull <T >() {
1652- final self = this ;
1653- return self is T ? self : null ;
1654- }
1655- }
Original file line number Diff line number Diff line change @@ -46,6 +46,7 @@ import 'package:analyzer/src/generated/error_detection_helpers.dart';
4646import 'package:analyzer/src/generated/parser.dart' show ParserErrorCode;
4747import 'package:analyzer/src/generated/this_access_tracker.dart' ;
4848import 'package:analyzer/src/summary2/macro_application_error.dart' ;
49+ import 'package:analyzer/src/utilities/extensions/object.dart' ;
4950import 'package:analyzer/src/utilities/extensions/string.dart' ;
5051import 'package:collection/collection.dart' ;
5152
@@ -5339,11 +5340,3 @@ class _UninstantiatedBoundChecker extends RecursiveAstVisitor<void> {
53395340 }
53405341 }
53415342}
5342-
5343- extension on Object ? {
5344- /// If the target is [T] , return it, otherwise `null` .
5345- T ? ifTypeOrNull <T >() {
5346- final self = this ;
5347- return self is T ? self : null ;
5348- }
5349- }
Original file line number Diff line number Diff line change 1+ // Copyright (c) 2022, 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+ extension NullableObjectExtension on Object ? {
6+ /// If the target is [T] , return it, otherwise `null` .
7+ T ? ifTypeOrNull <T >() {
8+ final self = this ;
9+ return self is T ? self : null ;
10+ }
11+ }
Original file line number Diff line number Diff line change 1+ // Copyright (c) 2022, 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:analyzer/src/utilities/extensions/object.dart' ;
6+ import 'package:test/test.dart' ;
7+ import 'package:test_reflective_loader/test_reflective_loader.dart' ;
8+
9+ main () {
10+ defineReflectiveSuite (() {
11+ defineReflectiveTests (NullableObjectExtensionTest );
12+ });
13+ }
14+
15+ @reflectiveTest
16+ class NullableObjectExtensionTest {
17+ test_ifTypeOrNull_int () {
18+ expect (0. ifTypeOrNull <int >(), 0 );
19+ expect (0. ifTypeOrNull <num >(), 0 );
20+ expect (0. ifTypeOrNull <Object >(), 0 );
21+ expect (0. ifTypeOrNull <String >(), isNull);
22+ }
23+
24+ test_ifTypeOrNull_null () {
25+ expect (null .ifTypeOrNull <Object >(), isNull);
26+ expect (null .ifTypeOrNull <int >(), isNull);
27+ }
28+ }
Original file line number Diff line number Diff line change 55import 'package:test_reflective_loader/test_reflective_loader.dart' ;
66
77import 'collection_test.dart' as collection;
8+ import 'object_test.dart' as object;
89import 'stream_test.dart' as stream;
910import 'string_test.dart' as string;
1011
1112main () {
1213 defineReflectiveSuite (() {
1314 collection.main ();
15+ object.main ();
1416 stream.main ();
1517 string.main ();
1618 }, name: 'extensions' );
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ import 'package:analyzer/dart/element/element.dart';
99import 'package:analyzer/dart/element/type.dart' ;
1010import 'package:analyzer/src/dart/ast/token.dart' ;
1111import 'package:analyzer/src/generated/source.dart' ;
12+ import 'package:analyzer/src/utilities/extensions/object.dart' ;
1213
1314/// A CompletionTarget represents an edge in the parse tree which connects an
1415/// AST node (the [containingNode] of the completion) to one of its children
@@ -697,11 +698,3 @@ class CompletionTarget {
697698 }
698699 }
699700}
700-
701- extension on Object ? {
702- /// If the target is [T] , return it, otherwise `null` .
703- T ? ifTypeOrNull <T >() {
704- final self = this ;
705- return self is T ? self : null ;
706- }
707- }
Original file line number Diff line number Diff line change @@ -27,5 +27,5 @@ CHANNEL dev
2727MAJOR 2
2828MINOR 18
2929PATCH 0
30- PRERELEASE 240
30+ PRERELEASE 241
3131PRERELEASE_PATCH 0
You can’t perform that action at this time.
0 commit comments