Skip to content

Commit 268f662

Browse files
kevmoovkorencik
authored andcommitted
Drop pkg:collection usage in lib (google#1488)
1 parent 907998e commit 268f662

File tree

7 files changed

+31
-33
lines changed

7 files changed

+31
-33
lines changed

json_serializable/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 6.9.6-wip
2+
3+
- Move `package:collection` to a dev dependency.
4+
15
## 6.9.5
26

37
- Support the `analyzer: '>=6.9.0 <8.0.0'`.

json_serializable/lib/src/type_helpers/json_converter_helper.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import 'package:analyzer/dart/constant/value.dart';
66
import 'package:analyzer/dart/element/element.dart';
77
import 'package:analyzer/dart/element/type.dart';
8-
import 'package:collection/collection.dart';
98
import 'package:json_annotation/json_annotation.dart';
109
import 'package:source_gen/source_gen.dart';
1110
import 'package:source_helper/source_helper.dart';
@@ -280,10 +279,11 @@ _ConverterMatch? _compatibleMatch(
280279
) {
281280
final converterClassElement = constantValue.type!.element as ClassElement;
282281

283-
final jsonConverterSuper =
284-
converterClassElement.allSupertypes.singleWhereOrNull(
285-
(e) => _jsonConverterChecker.isExactly(e.element),
286-
);
282+
final jsonConverterSuper = converterClassElement.allSupertypes
283+
.where(
284+
(e) => _jsonConverterChecker.isExactly(e.element),
285+
)
286+
.singleOrNull;
287287

288288
if (jsonConverterSuper == null) {
289289
return null;

json_serializable/lib/src/type_helpers/json_helper.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import 'package:analyzer/dart/element/element.dart';
66
import 'package:analyzer/dart/element/type.dart';
7-
import 'package:collection/collection.dart';
87
import 'package:json_annotation/json_annotation.dart';
98
import 'package:source_gen/source_gen.dart';
109
import 'package:source_helper/source_helper.dart';
@@ -79,7 +78,8 @@ class JsonHelper extends TypeHelper<TypeHelperContextWithConfig> {
7978
final classElement = targetType.element;
8079

8180
final fromJsonCtor = classElement.constructors
82-
.singleWhereOrNull((ce) => ce.name == 'fromJson');
81+
.where((ce) => ce.name == 'fromJson')
82+
.singleOrNull;
8383

8484
var output = expression;
8585
if (fromJsonCtor != null) {
@@ -291,4 +291,5 @@ ClassConfig? _annotation(ClassConfig config, InterfaceType source) {
291291

292292
MethodElement? _toJsonMethod(DartType type) => type.typeImplementations
293293
.map((dt) => dt is InterfaceType ? dt.getMethod('toJson') : null)
294-
.firstWhereOrNull((me) => me != null);
294+
.where((me) => me != null)
295+
.firstOrNull;

json_serializable/lib/src/type_helpers/map_helper.dart

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/element/type.dart';
6-
import 'package:collection/collection.dart';
76
import 'package:source_helper/source_helper.dart';
87

98
import '../constants.dart';
@@ -18,11 +17,7 @@ class MapHelper extends TypeHelper<TypeHelperContextWithConfig> {
1817
const MapHelper();
1918

2019
@override
21-
String? serialize(
22-
DartType targetType,
23-
String expression,
24-
TypeHelperContextWithConfig context,
25-
) {
20+
String? serialize(DartType targetType, String expression, TypeHelperContextWithConfig context) {
2621
if (!coreMapTypeChecker.isAssignableFromType(targetType)) {
2722
return null;
2823
}
@@ -92,7 +87,7 @@ class MapHelper extends TypeHelper<TypeHelperContextWithConfig> {
9287
// `toDouble` on input values
9388
valueArg.isSimpleJsonTypeNotDouble)) {
9489
// No mapping of the values or null check required!
95-
final valueString = valueArg.getDisplayString(withNullability: true);
90+
final valueString = valueArg.getDisplayString();
9691
return 'Map<String, $valueString>.from($expression as Map)';
9792
}
9893
}
@@ -133,14 +128,9 @@ final _intString = ToFromStringHelper('int.parse', 'toString()', 'int');
133128

134129
/// [ToFromStringHelper] instances representing non-String types that can
135130
/// be used as [Map] keys.
136-
final _instances = [
137-
bigIntString,
138-
dateTimeString,
139-
_intString,
140-
uriString,
141-
];
131+
final _instances = [bigIntString, dateTimeString, _intString, uriString];
142132

143-
ToFromStringHelper? _forType(DartType type) => _instances.singleWhereOrNull((i) => i.matches(type));
133+
ToFromStringHelper? _forType(DartType type) => _instances.where((i) => i.matches(type)).singleOrNull;
144134

145135
/// Returns `true` if [keyType] can be automatically converted to/from String –
146136
/// and is therefor usable as a key in a [Map].
@@ -155,19 +145,20 @@ void _checkSafeKeyType(String expression, DartType keyArg) {
155145
return;
156146
}
157147

158-
throw UnsupportedTypeError(
159-
keyArg,
160-
expression,
161-
'Map keys must be one of: ${allowedMapKeyTypes.join(', ')}.',
162-
);
148+
throw UnsupportedTypeError(keyArg, expression, 'Map keys must be one of: ${allowedMapKeyTypes.join(', ')}.');
163149
}
164150

165151
/// The names of types that can be used as [Map] keys.
166152
///
167153
/// Used in [_checkSafeKeyType] to provide a helpful error with unsupported
168154
/// types.
169-
List<String> get allowedMapKeyTypes =>
170-
['Object', 'dynamic', 'enum', 'String', ..._instances.map((i) => i.coreTypeName)];
155+
List<String> get allowedMapKeyTypes => [
156+
'Object',
157+
'dynamic',
158+
'enum',
159+
'String',
160+
..._instances.map((i) => i.coreTypeName),
161+
];
171162

172163
extension on DartType {
173164
bool get isSimpleJsonTypeNotDouble => !isDartCoreDouble && simpleJsonTypeChecker.isAssignableFromType(this);

json_serializable/pubspec.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: json_serializable
2-
version: 6.9.5
2+
version: 6.9.6-wip
33
description: >-
44
Automatically generate code for converting to and from JSON by annotating
55
Dart classes.
@@ -15,7 +15,7 @@ topics:
1515
resolution: workspace
1616

1717
dependencies:
18-
analyzer: '>=6.9.0 <8.0.0'
18+
analyzer: ">=6.9.0 <8.0.0"
1919
async: ^2.10.0
2020
build: ^3.0.0
2121
build_config: ^1.1.0
@@ -42,6 +42,7 @@ dev_dependencies:
4242
build_runner: ^2.4.6
4343
build_verify: ^3.0.0
4444
dart_style: ^3.0.0
45+
collection: ^1.17.0
4546
logging: ^1.0.0
4647
source_gen_test: ^1.0.6
4748
test: ^1.24.4

json_serializable/test/generic_files/generic_class.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
// ignore_for_file: inference_failure_on_instance_creation
66

7-
import 'package:collection/collection.dart';
87
import 'package:json_annotation/json_annotation.dart';
98

109
import '../test_utils.dart';
@@ -146,7 +145,7 @@ class Issue980ParentClass {
146145
other is Issue980ParentClass && deepEquals(list, other.list);
147146

148147
@override
149-
int get hashCode => const DeepCollectionEquality().hash(list);
148+
int get hashCode => deepHash(list);
150149
}
151150

152151
@JsonSerializable(genericArgumentFactories: true)

json_serializable/test/test_utils.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ import 'package:collection/collection.dart';
66

77
export 'package:_json_serial_shared_test/shared_test.dart';
88

9+
int deepHash(List value) => const DeepCollectionEquality().hash(value);
10+
911
bool deepEquals(dynamic a, dynamic b) =>
1012
const DeepCollectionEquality().equals(a, b);

0 commit comments

Comments
 (0)