mirror of
https://github.com/dart-lang/sdk
synced 2024-11-05 18:22:09 +00:00
Extract NullableObjectExtension.
Change-Id: I2fe2704d875082d20e1586f924aeebd6653703ee Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/250262 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Reviewed-by: Samuel Rawlins <srawlins@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
parent
1c461e06c1
commit
a8541852d5
6 changed files with 44 additions and 24 deletions
|
@ -23,6 +23,7 @@ import 'package:analyzer/dart/element/nullability_suffix.dart';
|
|||
import 'package:analyzer/dart/element/type.dart';
|
||||
import 'package:analyzer/src/dart/element/element.dart';
|
||||
import 'package:analyzer/src/dartdoc/dartdoc_directive_info.dart';
|
||||
import 'package:analyzer/src/utilities/extensions/object.dart';
|
||||
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
|
||||
import 'package:analyzer_plugin/utilities/range_factory.dart';
|
||||
|
||||
|
@ -1645,11 +1646,3 @@ class _ElementDocumentation {
|
|||
required this.summary,
|
||||
});
|
||||
}
|
||||
|
||||
extension on Object? {
|
||||
/// If the target is [T], return it, otherwise `null`.
|
||||
T? ifTypeOrNull<T>() {
|
||||
final self = this;
|
||||
return self is T ? self : null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,6 +46,7 @@ import 'package:analyzer/src/generated/error_detection_helpers.dart';
|
|||
import 'package:analyzer/src/generated/parser.dart' show ParserErrorCode;
|
||||
import 'package:analyzer/src/generated/this_access_tracker.dart';
|
||||
import 'package:analyzer/src/summary2/macro_application_error.dart';
|
||||
import 'package:analyzer/src/utilities/extensions/object.dart';
|
||||
import 'package:analyzer/src/utilities/extensions/string.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
|
||||
|
@ -5339,11 +5340,3 @@ class _UninstantiatedBoundChecker extends RecursiveAstVisitor<void> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension on Object? {
|
||||
/// If the target is [T], return it, otherwise `null`.
|
||||
T? ifTypeOrNull<T>() {
|
||||
final self = this;
|
||||
return self is T ? self : null;
|
||||
}
|
||||
}
|
||||
|
|
11
pkg/analyzer/lib/src/utilities/extensions/object.dart
Normal file
11
pkg/analyzer/lib/src/utilities/extensions/object.dart
Normal file
|
@ -0,0 +1,11 @@
|
|||
// Copyright (c) 2022, 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.
|
||||
|
||||
extension NullableObjectExtension on Object? {
|
||||
/// If the target is [T], return it, otherwise `null`.
|
||||
T? ifTypeOrNull<T>() {
|
||||
final self = this;
|
||||
return self is T ? self : null;
|
||||
}
|
||||
}
|
28
pkg/analyzer/test/src/utilities/extensions/object_test.dart
Normal file
28
pkg/analyzer/test/src/utilities/extensions/object_test.dart
Normal file
|
@ -0,0 +1,28 @@
|
|||
// Copyright (c) 2022, 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:analyzer/src/utilities/extensions/object.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:test_reflective_loader/test_reflective_loader.dart';
|
||||
|
||||
main() {
|
||||
defineReflectiveSuite(() {
|
||||
defineReflectiveTests(NullableObjectExtensionTest);
|
||||
});
|
||||
}
|
||||
|
||||
@reflectiveTest
|
||||
class NullableObjectExtensionTest {
|
||||
test_ifTypeOrNull_int() {
|
||||
expect(0.ifTypeOrNull<int>(), 0);
|
||||
expect(0.ifTypeOrNull<num>(), 0);
|
||||
expect(0.ifTypeOrNull<Object>(), 0);
|
||||
expect(0.ifTypeOrNull<String>(), isNull);
|
||||
}
|
||||
|
||||
test_ifTypeOrNull_null() {
|
||||
expect(null.ifTypeOrNull<Object>(), isNull);
|
||||
expect(null.ifTypeOrNull<int>(), isNull);
|
||||
}
|
||||
}
|
|
@ -5,12 +5,14 @@
|
|||
import 'package:test_reflective_loader/test_reflective_loader.dart';
|
||||
|
||||
import 'collection_test.dart' as collection;
|
||||
import 'object_test.dart' as object;
|
||||
import 'stream_test.dart' as stream;
|
||||
import 'string_test.dart' as string;
|
||||
|
||||
main() {
|
||||
defineReflectiveSuite(() {
|
||||
collection.main();
|
||||
object.main();
|
||||
stream.main();
|
||||
string.main();
|
||||
}, name: 'extensions');
|
||||
|
|
|
@ -9,6 +9,7 @@ import 'package:analyzer/dart/element/element.dart';
|
|||
import 'package:analyzer/dart/element/type.dart';
|
||||
import 'package:analyzer/src/dart/ast/token.dart';
|
||||
import 'package:analyzer/src/generated/source.dart';
|
||||
import 'package:analyzer/src/utilities/extensions/object.dart';
|
||||
|
||||
/// A CompletionTarget represents an edge in the parse tree which connects an
|
||||
/// AST node (the [containingNode] of the completion) to one of its children
|
||||
|
@ -697,11 +698,3 @@ class CompletionTarget {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension on Object? {
|
||||
/// If the target is [T], return it, otherwise `null`.
|
||||
T? ifTypeOrNull<T>() {
|
||||
final self = this;
|
||||
return self is T ? self : null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue