Skip to content

Commit d65e4d7

Browse files
kevmoovkorencik
authored andcommitted
Update to latest analyzer release and fix deprecations (google#1510)
1 parent c864e17 commit d65e4d7

File tree

9 files changed

+38
-52
lines changed

9 files changed

+38
-52
lines changed

analysis_options.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ include: package:dart_flutter_team_lints/analysis_options.yaml
44
analyzer:
55
language:
66
strict-casts: true
7-
errors:
8-
# Analyzer v7.4.0 crazy
9-
deprecated_member_use: ignore
107

118
linter:
129
rules:

json_serializable/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 6.10.0-wip
2+
3+
- Required `analyzer: ^7.4.0`.
4+
15
## 6.10.0-dev
26

37
- Switch to analyzer element2 model and `build: ^3.0.0-dev`.

json_serializable/lib/src/enum_utils.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import 'json_literal_generator.dart';
1313
import 'utils.dart';
1414

1515
String constMapName(DartType targetType) =>
16-
'_\$${targetType.element!.name}EnumMap';
16+
'_\$${targetType.element3!.name3}EnumMap';
1717

1818
/// If [targetType] is not an enum, return `null`.
1919
///
@@ -45,7 +45,7 @@ String? enumValueMapFromType(
4545
final items = enumMap.entries
4646
.map(
4747
(e) =>
48-
' ${targetType.element!.name}.${e.key.name3}: '
48+
' ${targetType.element3!.name3}.${e.key.name3}: '
4949
'${jsonLiteralAsDart(e.value)},',
5050
)
5151
.join();
@@ -57,7 +57,7 @@ Map<FieldElement2, Object?>? _enumMap(
5757
DartType targetType, {
5858
bool nullWithNoAnnotation = false,
5959
}) {
60-
final targetTypeElement = targetType.element;
60+
final targetTypeElement = targetType.element3;
6161
if (targetTypeElement == null) return null;
6262
final annotation = _jsonEnumChecker.firstAnnotationOf(targetTypeElement);
6363
final jsonEnum = _fromAnnotation(annotation);

json_serializable/lib/src/helper_core.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ $converterOrKeyInstructions
7979
} else if (field.type != error.type) {
8080
message = '$message because of type `${typeToCode(error.type)}`';
8181
} else {
82-
final element = error.type.element?.name;
82+
final element = error.type.element3?.name3;
8383
todo =
8484
'''
8585
To support the type `${element ?? error.type}` you can:

json_serializable/lib/src/json_key_utils.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ KeyConfig _from(FieldElement2 element, ClassConfig classAnnotation) {
6666
// literal, which is NOT supported!
6767
badType = 'Function';
6868
} else if (!reader.isLiteral) {
69-
badType = dartObject.type!.element?.name;
69+
badType = dartObject.type!.element3?.name3;
7070
}
7171

7272
if (badType != null) {
@@ -196,7 +196,7 @@ KeyConfig _from(FieldElement2 element, ClassConfig classAnnotation) {
196196
(n) => n,
197197
);
198198

199-
return '${annotationType.element!.name}.$enumValueName';
199+
return '${annotationType.element3!.name3}.$enumValueName';
200200
} else {
201201
final defaultValueLiteral = literalForObject(fieldName, objectValue, []);
202202
if (defaultValueLiteral == null) {
@@ -333,7 +333,7 @@ bool _includeIfNull(
333333
bool _interfaceTypesEqual(DartType a, DartType b) {
334334
if (a is InterfaceType && b is InterfaceType) {
335335
// Handle nullability case. Pretty sure this is fine for enums.
336-
return a.element == b.element;
336+
return a.element3 == b.element3;
337337
}
338338
return a == b;
339339
}

json_serializable/lib/src/type_helpers/json_converter_helper.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ _JsonConvertData? _typeConverterFrom(
244244

245245
if (match.genericTypeArg != null) {
246246
return _JsonConvertData.genericClass(
247-
match.annotation.type!.element!.name!,
247+
match.annotation.type!.element3!.name3!,
248248
match.genericTypeArg!,
249249
reviver.accessor,
250250
match.jsonType,
@@ -253,7 +253,7 @@ _JsonConvertData? _typeConverterFrom(
253253
}
254254

255255
return _JsonConvertData.className(
256-
match.annotation.type!.element!.name!,
256+
match.annotation.type!.element3!.name3!,
257257
reviver.accessor,
258258
match.jsonType,
259259
match.fieldType,
@@ -291,7 +291,7 @@ _ConverterMatch? _compatibleMatch(
291291
return null;
292292
}
293293

294-
assert(jsonConverterSuper.element.typeParameters.length == 2);
294+
assert(jsonConverterSuper.element3.typeParameters2.length == 2);
295295
assert(jsonConverterSuper.typeArguments.length == 2);
296296

297297
final fieldType = jsonConverterSuper.typeArguments[0];
@@ -308,7 +308,7 @@ _ConverterMatch? _compatibleMatch(
308308
}
309309

310310
if (fieldType is TypeParameterType && targetType is TypeParameterType) {
311-
assert(annotation?.element is! PropertyAccessorElement2);
311+
assert(annotation?.element2 is! PropertyAccessorElement2);
312312
assert(converterClassElement.typeParameters2.isNotEmpty);
313313
if (converterClassElement.typeParameters2.length > 1) {
314314
throw InvalidGenerationSourceError(
@@ -323,7 +323,7 @@ _ConverterMatch? _compatibleMatch(
323323
annotation,
324324
constantValue,
325325
jsonConverterSuper.typeArguments[1],
326-
'${targetType.element.name}${targetType.isNullableType ? '?' : ''}',
326+
'${targetType.element3.name3}${targetType.isNullableType ? '?' : ''}',
327327
fieldType,
328328
);
329329
}

json_serializable/lib/src/type_helpers/json_helper.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ List<String> _helperParams(
157157
final args = <String>[];
158158

159159
for (var helperArg in rest) {
160-
final typeParamIndex = type.element.typeParameters.indexOf(
161-
helperArg.element,
160+
final typeParamIndex = type.element3.typeParameters2.indexOf(
161+
helperArg.element3,
162162
);
163163

164164
// TODO: throw here if `typeParamIndex` is -1 ?
@@ -181,7 +181,7 @@ TypeParameterType _decodeHelper(
181181
type.normalParameterTypes.length == 1) {
182182
final funcReturnType = type.returnType;
183183

184-
if (param.name3 == fromJsonForName(funcReturnType.element!.name!)) {
184+
if (param.name3 == fromJsonForName(funcReturnType.element3!.name3!)) {
185185
final funcParamType = type.normalParameterTypes.single;
186186

187187
if ((funcParamType.isDartCoreObject && funcParamType.isNullableType) ||
@@ -212,7 +212,7 @@ TypeParameterType _encodeHelper(
212212
type.normalParameterTypes.length == 1) {
213213
final funcParamType = type.normalParameterTypes.single;
214214

215-
if (param.name3 == toJsonForName(funcParamType.element!.name!)) {
215+
if (param.name3 == toJsonForName(funcParamType.element3!.name3!)) {
216216
if (funcParamType is TypeParameterType) {
217217
return funcParamType;
218218
}
@@ -252,7 +252,7 @@ InterfaceType? _instantiate(
252252
InterfaceType classType,
253253
) {
254254
final argTypes = ctorParamType.typeArguments.map((arg) {
255-
final typeParamIndex = classType.element.typeParameters.indexWhere(
255+
final typeParamIndex = classType.element3.typeParameters2.indexWhere(
256256
// TODO: not 100% sure `nullabilitySuffix` is right
257257
(e) => e.instantiate(nullabilitySuffix: arg.nullabilitySuffix) == arg,
258258
);
@@ -269,7 +269,7 @@ InterfaceType? _instantiate(
269269
return null;
270270
}
271271

272-
return ctorParamType.element.instantiate(
272+
return ctorParamType.element3.instantiate(
273273
typeArguments: argTypes.cast<DartType>(),
274274
nullabilitySuffix: ctorParamType.nullabilitySuffix,
275275
);

json_serializable/lib/src/utils.dart

Lines changed: 15 additions & 30 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/constant/value.dart';
6-
import 'package:analyzer/dart/element/element.dart';
76
import 'package:analyzer/dart/element/element2.dart';
87
import 'package:analyzer/dart/element/type.dart';
98
import 'package:json_annotation/json_annotation.dart';
@@ -17,22 +16,17 @@ const _jsonKeyChecker = TypeChecker.fromRuntime(JsonKey);
1716

1817
DartObject? _jsonKeyAnnotation(FieldElement2 element) =>
1918
_jsonKeyChecker.firstAnnotationOf(element) ??
20-
(element.getter2 == null
21-
? null
22-
: _jsonKeyChecker.firstAnnotationOf(element.getter2!));
19+
(element.getter2 == null ? null : _jsonKeyChecker.firstAnnotationOf(element.getter2!));
2320

24-
ConstantReader jsonKeyAnnotation(FieldElement2 element) =>
25-
ConstantReader(_jsonKeyAnnotation(element));
21+
ConstantReader jsonKeyAnnotation(FieldElement2 element) => ConstantReader(_jsonKeyAnnotation(element));
2622

2723
/// Returns `true` if [element] is annotated with [JsonKey].
28-
bool hasJsonKeyAnnotation(FieldElement2 element) =>
29-
_jsonKeyAnnotation(element) != null;
24+
bool hasJsonKeyAnnotation(FieldElement2 element) => _jsonKeyAnnotation(element) != null;
3025

31-
Never throwUnsupported(FieldElement2 element, String message) =>
32-
throw InvalidGenerationSourceError(
33-
'Error with `@JsonKey` on the `${element.name3}` field. $message',
34-
element: element,
35-
);
26+
Never throwUnsupported(FieldElement2 element, String message) => throw InvalidGenerationSourceError(
27+
'Error with `@JsonKey` on the `${element.name3}` field. $message',
28+
element: element,
29+
);
3630

3731
T? readEnum<T extends Enum>(ConstantReader reader, List<T> values) =>
3832
reader.isNull ? null : enumValueForDartObject<T>(reader.objectValue, values, (f) => f.name);
@@ -69,11 +63,7 @@ JsonSerializable _valueForAnnotation(ConstantReader reader) => JsonSerializable(
6963
/// Note: if [JsonSerializable.genericArgumentFactories] is `false` for [reader]
7064
/// and `true` for [config], the corresponding field in the return value will
7165
/// only be `true` if [classElement] has type parameters.
72-
ClassConfig mergeConfig(
73-
ClassConfig config,
74-
ConstantReader reader, {
75-
required ClassElement2 classElement,
76-
}) {
66+
ClassConfig mergeConfig(ClassConfig config, ConstantReader reader, {required ClassElement2 classElement}) {
7767
final annotation = _valueForAnnotation(reader);
7868
assert(config.ctorParamDefaults.isEmpty);
7969

@@ -104,19 +94,15 @@ ClassConfig mergeConfig(
10494
fieldRename: annotation.fieldRename ?? config.fieldRename,
10595
genericArgumentFactories:
10696
annotation.genericArgumentFactories ??
107-
(classElement.typeParameters2.isNotEmpty &&
108-
config.genericArgumentFactories),
97+
(classElement.typeParameters2.isNotEmpty && config.genericArgumentFactories),
10998
ignoreUnannotated: annotation.ignoreUnannotated ?? config.ignoreUnannotated,
11099
includeIfNull: annotation.includeIfNull ?? config.includeIfNull,
111100
ctorParamDefaults: paramDefaultValueMap,
112101
converters: converters.isNull ? const [] : converters.listValue,
113102
);
114103
}
115104

116-
ConstructorElement2? _constructorByNameOrNull(
117-
ClassElement2 classElement,
118-
String name,
119-
) {
105+
ConstructorElement2? _constructorByNameOrNull(ClassElement2 classElement, String name) {
120106
try {
121107
return constructorByName(classElement, name);
122108
} on InvalidGenerationSourceError {
@@ -147,20 +133,19 @@ ConstructorElement2 constructorByName(ClassElement2 classElement, String name) {
147133
return ctor;
148134
}
149135

150-
/// If [targetType] is an enum, returns the [FieldElement] instances associated
136+
/// If [targetType] is an enum, returns the [FieldElement2] instances associated
151137
/// with its values.
152138
///
153139
/// Otherwise, `null`.
154140
Iterable<FieldElement2>? iterateEnumFields(DartType targetType) {
155-
if ( /*targetType is InterfaceType && */ targetType.element3
156-
is EnumElement2) {
141+
if ( /*targetType is InterfaceType && */ targetType.element3 is EnumElement2) {
157142
return (targetType.element3 as EnumElement2).constants2;
158143
}
159144
return null;
160145
}
161146

162147
extension DartTypeExtension on DartType {
163-
DartType promoteNonNullable() => element?.library?.typeSystem.promoteToNonNull(this) ?? this;
148+
DartType promoteNonNullable() => element3?.library2?.typeSystem.promoteToNonNull(this) ?? this;
164149

165150
String toStringNonNullable() {
166151
final val = getDisplayString(withNullability: false);
@@ -190,7 +175,7 @@ String typeToCode(DartType type, {bool forceNullable = false}) {
190175
return 'dynamic';
191176
} else if (type is InterfaceType) {
192177
return [
193-
type.element.name,
178+
type.element3.name3,
194179
if (type.typeArguments.isNotEmpty) '<${type.typeArguments.map(typeToCode).join(', ')}>',
195180
(type.isNullableType || forceNullable) ? '?' : '',
196181
].join();
@@ -227,7 +212,7 @@ String? defaultDecodeLogic(DartType targetType, String expression, {bool default
227212

228213
extension ExecutableElementExtension on ExecutableElement2 {
229214
/// Returns the name of `this` qualified with the class name if it's a
230-
/// [MethodElement].
215+
/// [MethodElement2].
231216
String get qualifiedName {
232217
if (this is TopLevelFunctionElement) {
233218
return name3!;

json_serializable/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: json_serializable
2-
version: 6.10.0-dev
2+
version: 6.10.0-wip
33
description: >-
44
Automatically generate code for converting to and from JSON by annotating
55
Dart classes.

0 commit comments

Comments
 (0)