From d51ae5dcf805e12f45128efb4670e7f2e09f47c0 Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Thu, 17 Apr 2025 11:19:22 -0700 Subject: [PATCH] Drop pkg:collection usage in lib --- json_serializable/CHANGELOG.md | 4 ++++ .../lib/src/type_helpers/json_converter_helper.dart | 10 +++++----- .../lib/src/type_helpers/json_helper.dart | 7 ++++--- json_serializable/lib/src/type_helpers/map_helper.dart | 3 +-- json_serializable/pubspec.yaml | 4 ++-- .../test/generic_files/generic_class.dart | 3 +-- json_serializable/test/test_utils.dart | 2 ++ 7 files changed, 19 insertions(+), 14 deletions(-) diff --git a/json_serializable/CHANGELOG.md b/json_serializable/CHANGELOG.md index 332e9a493..e49cc7918 100644 --- a/json_serializable/CHANGELOG.md +++ b/json_serializable/CHANGELOG.md @@ -1,3 +1,7 @@ +## 6.9.6-wip + +- Move `package:collection` to a dev dependency. + ## 6.9.5 - Support the `analyzer: '>=6.9.0 <8.0.0'`. diff --git a/json_serializable/lib/src/type_helpers/json_converter_helper.dart b/json_serializable/lib/src/type_helpers/json_converter_helper.dart index 9958368af..20ec0fc25 100644 --- a/json_serializable/lib/src/type_helpers/json_converter_helper.dart +++ b/json_serializable/lib/src/type_helpers/json_converter_helper.dart @@ -5,7 +5,6 @@ import 'package:analyzer/dart/constant/value.dart'; import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/type.dart'; -import 'package:collection/collection.dart'; import 'package:json_annotation/json_annotation.dart'; import 'package:source_gen/source_gen.dart'; import 'package:source_helper/source_helper.dart'; @@ -280,10 +279,11 @@ _ConverterMatch? _compatibleMatch( ) { final converterClassElement = constantValue.type!.element as ClassElement; - final jsonConverterSuper = - converterClassElement.allSupertypes.singleWhereOrNull( - (e) => _jsonConverterChecker.isExactly(e.element), - ); + final jsonConverterSuper = converterClassElement.allSupertypes + .where( + (e) => _jsonConverterChecker.isExactly(e.element), + ) + .singleOrNull; if (jsonConverterSuper == null) { return null; diff --git a/json_serializable/lib/src/type_helpers/json_helper.dart b/json_serializable/lib/src/type_helpers/json_helper.dart index fe10a3fea..d66ab772e 100644 --- a/json_serializable/lib/src/type_helpers/json_helper.dart +++ b/json_serializable/lib/src/type_helpers/json_helper.dart @@ -4,7 +4,6 @@ import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/type.dart'; -import 'package:collection/collection.dart'; import 'package:json_annotation/json_annotation.dart'; import 'package:source_gen/source_gen.dart'; import 'package:source_helper/source_helper.dart'; @@ -79,7 +78,8 @@ class JsonHelper extends TypeHelper { final classElement = targetType.element; final fromJsonCtor = classElement.constructors - .singleWhereOrNull((ce) => ce.name == 'fromJson'); + .where((ce) => ce.name == 'fromJson') + .singleOrNull; var output = expression; if (fromJsonCtor != null) { @@ -291,4 +291,5 @@ ClassConfig? _annotation(ClassConfig config, InterfaceType source) { MethodElement? _toJsonMethod(DartType type) => type.typeImplementations .map((dt) => dt is InterfaceType ? dt.getMethod('toJson') : null) - .firstWhereOrNull((me) => me != null); + .where((me) => me != null) + .firstOrNull; diff --git a/json_serializable/lib/src/type_helpers/map_helper.dart b/json_serializable/lib/src/type_helpers/map_helper.dart index 51cb3a0a5..e0c6ef560 100644 --- a/json_serializable/lib/src/type_helpers/map_helper.dart +++ b/json_serializable/lib/src/type_helpers/map_helper.dart @@ -3,7 +3,6 @@ // BSD-style license that can be found in the LICENSE file. import 'package:analyzer/dart/element/type.dart'; -import 'package:collection/collection.dart'; import 'package:source_helper/source_helper.dart'; import '../constants.dart'; @@ -147,7 +146,7 @@ final _instances = [ ]; ToFromStringHelper? _forType(DartType type) => - _instances.singleWhereOrNull((i) => i.matches(type)); + _instances.where((i) => i.matches(type)).singleOrNull; /// Returns `true` if [keyType] can be automatically converted to/from String – /// and is therefor usable as a key in a [Map]. diff --git a/json_serializable/pubspec.yaml b/json_serializable/pubspec.yaml index 94d1cdfa6..10e7ec8aa 100644 --- a/json_serializable/pubspec.yaml +++ b/json_serializable/pubspec.yaml @@ -1,5 +1,5 @@ name: json_serializable -version: 6.9.5 +version: 6.9.6-wip description: >- Automatically generate code for converting to and from JSON by annotating Dart classes. @@ -19,7 +19,6 @@ dependencies: async: ^2.10.0 build: ^2.4.1 build_config: ^1.1.0 - collection: ^1.17.0 dart_style: '>=2.3.7 <4.0.0' # Use a tight version constraint to ensure that a constraint on @@ -37,6 +36,7 @@ dev_dependencies: path: ../shared_test build_runner: ^2.4.6 build_verify: ^3.0.0 + collection: ^1.17.0 logging: ^1.0.0 source_gen_test: ^1.0.6 test: ^1.24.4 diff --git a/json_serializable/test/generic_files/generic_class.dart b/json_serializable/test/generic_files/generic_class.dart index 0ebfe7d1e..8c8b8d715 100644 --- a/json_serializable/test/generic_files/generic_class.dart +++ b/json_serializable/test/generic_files/generic_class.dart @@ -4,7 +4,6 @@ // ignore_for_file: inference_failure_on_instance_creation -import 'package:collection/collection.dart'; import 'package:json_annotation/json_annotation.dart'; import '../test_utils.dart'; @@ -146,7 +145,7 @@ class Issue980ParentClass { other is Issue980ParentClass && deepEquals(list, other.list); @override - int get hashCode => const DeepCollectionEquality().hash(list); + int get hashCode => deepHash(list); } @JsonSerializable(genericArgumentFactories: true) diff --git a/json_serializable/test/test_utils.dart b/json_serializable/test/test_utils.dart index 7d09c427c..fce40a9d1 100644 --- a/json_serializable/test/test_utils.dart +++ b/json_serializable/test/test_utils.dart @@ -6,5 +6,7 @@ import 'package:collection/collection.dart'; export 'package:_json_serial_shared_test/shared_test.dart'; +int deepHash(List value) => const DeepCollectionEquality().hash(value); + bool deepEquals(dynamic a, dynamic b) => const DeepCollectionEquality().equals(a, b);