Skip to content

Commit 0e20a04

Browse files
[go_router_builder] Add analyzer 9 compatibility (#10526)
Updates the compatible `analyzer` range from 7.x-8.x to 8.x-9.x (9.0 being the current version). The range has to be moved, rather than expanded, because part of the breaking change in 9.0 was removing methods that were replaced and deprecated in 8.x. Fixes flutter/flutter#179073 Fixes flutter/flutter#166603 ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent b6e7d59 commit 0e20a04

File tree

6 files changed

+65
-74
lines changed

6 files changed

+65
-74
lines changed

packages/go_router_builder/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 4.1.2
22

3+
* Updates supported analyzer versions to 8.x or 9.x.
34
* Updates minimum supported SDK version to Flutter 3.32/Dart 3.8.
45

56
## 4.1.1

packages/go_router_builder/lib/src/go_router_generator.dart

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

55
import 'dart:async';
66

7-
import 'package:analyzer/dart/element/element2.dart';
7+
import 'package:analyzer/dart/element/element.dart';
88
import 'package:analyzer/dart/element/type.dart';
99
import 'package:build/build.dart';
1010
import 'package:source_gen/source_gen.dart';
@@ -76,7 +76,7 @@ ${getters.map((String e) => "$e,").join('\n')}
7676
}
7777

7878
InfoIterable _generateForAnnotatedElement(
79-
Element2 element,
79+
Element element,
8080
ConstantReader annotation,
8181
) {
8282
final String typedAnnotation = withoutNullability(
@@ -87,7 +87,7 @@ ${getters.map((String e) => "$e,").join('\n')}
8787
typedAnnotation.indexOf('<'),
8888
);
8989
final String routeData = _annotations[type]!;
90-
if (element is! ClassElement2) {
90+
if (element is! ClassElement) {
9191
throw InvalidGenerationSourceError(
9292
'The @$type annotation can only be applied to classes.',
9393
element: element,

packages/go_router_builder/lib/src/route_config.dart

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'dart:collection';
66

77
import 'package:analyzer/dart/ast/ast.dart';
88
import 'package:analyzer/dart/constant/value.dart';
9-
import 'package:analyzer/dart/element/element2.dart';
9+
import 'package:analyzer/dart/element/element.dart';
1010
import 'package:analyzer/dart/element/nullability_suffix.dart';
1111
import 'package:analyzer/dart/element/type.dart';
1212
import 'package:collection/collection.dart';
@@ -57,14 +57,14 @@ class ShellRouteConfig extends RouteBaseConfig {
5757

5858
@override
5959
Iterable<String> classDeclarations() {
60-
if (routeDataClass.unnamedConstructor2 == null) {
60+
if (routeDataClass.unnamedConstructor == null) {
6161
throw InvalidGenerationSourceError(
6262
'The ShellRouteData "$_className" class must have an unnamed constructor.',
6363
element: routeDataClass,
6464
);
6565
}
6666

67-
final bool isConst = routeDataClass.unnamedConstructor2!.isConst;
67+
final bool isConst = routeDataClass.unnamedConstructor!.isConst;
6868

6969
return <String>[
7070
'''
@@ -116,7 +116,7 @@ class StatefulShellRouteConfig extends RouteBaseConfig {
116116
Iterable<String> classDeclarations() => <String>[
117117
'''
118118
extension $_extensionName on $_className {
119-
static $_className _fromState(GoRouterState state) =>${routeDataClass.unnamedConstructor2!.isConst ? ' const' : ''} $_className();
119+
static $_className _fromState(GoRouterState state) =>${routeDataClass.unnamedConstructor!.isConst ? ' const' : ''} $_className();
120120
}
121121
''',
122122
];
@@ -297,7 +297,7 @@ mixin _GoRouteMixin on RouteBaseConfig {
297297
}
298298

299299
String _encodeFor(String fieldName) {
300-
final PropertyAccessorElement2? field = _field(fieldName);
300+
final PropertyAccessorElement? field = _field(fieldName);
301301
if (field == null) {
302302
throw InvalidGenerationSourceError(
303303
'Could not find a field for the path parameter "$fieldName".',
@@ -363,8 +363,8 @@ mixin _GoRouteMixin on RouteBaseConfig {
363363
)
364364
.toList();
365365

366-
ConstructorElement2 get _ctor {
367-
final ConstructorElement2? ctor = routeDataClass.unnamedConstructor2;
366+
ConstructorElement get _ctor {
367+
final ConstructorElement? ctor = routeDataClass.unnamedConstructor;
368368

369369
if (ctor == null) {
370370
throw InvalidGenerationSourceError(
@@ -465,7 +465,7 @@ class GoRouteConfig extends RouteBaseConfig with _GoRouteMixin {
465465
getNodeDeclaration<ClassDeclaration>(routeDataClass)
466466
?.withClause
467467
?.mixinTypes
468-
.any((NamedType e) => e.name2.toString() == _mixinName) ??
468+
.any((NamedType e) => e.name.toString() == _mixinName) ??
469469
false;
470470

471471
if (!hasMixin) {
@@ -540,7 +540,7 @@ class RelativeGoRouteConfig extends RouteBaseConfig with _GoRouteMixin {
540540
getNodeDeclaration<ClassDeclaration>(routeDataClass)
541541
?.withClause
542542
?.mixinTypes
543-
.any((NamedType e) => e.name2.toString() == _mixinName) ??
543+
.any((NamedType e) => e.name.toString() == _mixinName) ??
544544
false;
545545

546546
if (!hasMixin) {
@@ -596,7 +596,7 @@ abstract class RouteBaseConfig {
596596
/// Creates a new [RouteBaseConfig] represented the annotation data in [reader].
597597
factory RouteBaseConfig.fromAnnotation(
598598
ConstantReader reader,
599-
InterfaceElement2 element,
599+
InterfaceElement element,
600600
) {
601601
final definition = RouteBaseConfig._fromAnnotation(reader, element, null);
602602

@@ -613,7 +613,7 @@ abstract class RouteBaseConfig {
613613

614614
factory RouteBaseConfig._fromAnnotation(
615615
ConstantReader reader,
616-
InterfaceElement2 element,
616+
InterfaceElement element,
617617
RouteBaseConfig? parent, {
618618
bool isAncestorRelative = false,
619619
}) {
@@ -641,8 +641,7 @@ abstract class RouteBaseConfig {
641641
}
642642

643643
// TODO(kevmoo): validate that this MUST be a subtype of `GoRouteData`
644-
// ignore: experimental_member_use
645-
final InterfaceElement2 classElement = typeParamType.element3;
644+
final InterfaceElement classElement = typeParamType.element;
646645

647646
final RouteBaseConfig value;
648647
switch (typeName) {
@@ -780,7 +779,7 @@ abstract class RouteBaseConfig {
780779
final List<RouteBaseConfig> _children = <RouteBaseConfig>[];
781780

782781
/// The `RouteData` class this class represents.
783-
final InterfaceElement2 routeDataClass;
782+
final InterfaceElement routeDataClass;
784783

785784
/// The parent of this route config.
786785
final RouteBaseConfig? parent;
@@ -793,11 +792,11 @@ abstract class RouteBaseConfig {
793792
}
794793

795794
static String? _generateParameterGetterCode(
796-
InterfaceElement2 classElement, {
795+
InterfaceElement classElement, {
797796
required String parameterName,
798797
}) {
799-
final String? fieldDisplayName = classElement.fields2
800-
.where((FieldElement2 element) {
798+
final String? fieldDisplayName = classElement.fields
799+
.where((FieldElement element) {
801800
if (!element.isStatic || element.displayName != parameterName) {
802801
return false;
803802
}
@@ -820,17 +819,17 @@ abstract class RouteBaseConfig {
820819
}
821820
return true;
822821
})
823-
.map<String>((FieldElement2 e) => e.displayName)
822+
.map<String>((FieldElement e) => e.displayName)
824823
.firstOrNull;
825824

826825
if (fieldDisplayName != null) {
827826
return '${classElement.displayName}.$fieldDisplayName';
828827
}
829-
final String? methodDisplayName = classElement.methods2
830-
.where((MethodElement2 element) {
828+
final String? methodDisplayName = classElement.methods
829+
.where((MethodElement element) {
831830
return element.isStatic && element.displayName == parameterName;
832831
})
833-
.map<String>((MethodElement2 e) => e.displayName)
832+
.map<String>((MethodElement e) => e.displayName)
834833
.firstOrNull;
835834

836835
if (methodDisplayName != null) {
@@ -901,12 +900,12 @@ $routeDataClassName.$dataConvertionFunctionName(
901900
''';
902901
}
903902

904-
PropertyAccessorElement2? _field(String name) =>
905-
routeDataClass.getGetter2(name);
903+
PropertyAccessorElement? _field(String name) =>
904+
routeDataClass.getGetter(name);
906905

907-
List<ElementAnnotation>? _fieldMetadata(String name) => routeDataClass.fields2
908-
.firstWhereOrNull((FieldElement2 element) => element.displayName == name)
909-
?.metadata2
906+
List<ElementAnnotation>? _fieldMetadata(String name) => routeDataClass.fields
907+
.firstWhereOrNull((FieldElement element) => element.displayName == name)
908+
?.metadata
910909
.annotations;
911910

912911
/// The name of `RouteData` subclass this configuration represents.
@@ -937,9 +936,8 @@ String _enumMapConst(InterfaceType type) {
937936

938937
final buffer = StringBuffer('const ${enumMapName(type)} = {');
939938

940-
// ignore: experimental_member_use
941-
for (final FieldElement2 enumField in type.element3.fields2.where(
942-
(FieldElement2 element) => element.isEnumConstant,
939+
for (final FieldElement enumField in type.element.fields.where(
940+
(FieldElement element) => element.isEnumConstant,
943941
)) {
944942
buffer.writeln(
945943
'$enumName.${enumField.displayName}: ${escapeDartString(enumField.displayName.kebab)},',

packages/go_router_builder/lib/src/type_helpers.dart

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'package:analyzer/dart/analysis/results.dart';
66
import 'package:analyzer/dart/analysis/session.dart';
77
import 'package:analyzer/dart/ast/ast.dart';
88
import 'package:analyzer/dart/constant/value.dart';
9-
import 'package:analyzer/dart/element/element2.dart';
9+
import 'package:analyzer/dart/element/element.dart';
1010
import 'package:analyzer/dart/element/type.dart';
1111
import 'package:collection/collection.dart';
1212
import 'package:source_gen/source_gen.dart';
@@ -56,7 +56,7 @@ const List<_TypeHelper> _helpers = <_TypeHelper>[
5656

5757
/// Checks if has a function that converts string to string, such as encode and decode.
5858
bool _isStringToStringFunction(
59-
ExecutableElement2? executableElement,
59+
ExecutableElement? executableElement,
6060
String name,
6161
) {
6262
if (executableElement == null) {
@@ -71,9 +71,10 @@ bool _isStringToStringFunction(
7171

7272
/// Returns the custom codec for the annotation.
7373
String? _getCustomCodec(ElementAnnotation annotation, String name) {
74-
final ExecutableElement2? executableElement =
75-
// ignore: experimental_member_use
76-
annotation.computeConstantValue()?.getField(name)?.toFunctionValue2();
74+
final ExecutableElement? executableElement = annotation
75+
.computeConstantValue()
76+
?.getField(name)
77+
?.toFunctionValue();
7778
if (_isStringToStringFunction(executableElement, name)) {
7879
return executableElement!.displayName;
7980
}
@@ -111,7 +112,7 @@ String decodeParameter(
111112
} else {
112113
throw InvalidGenerationSourceError(
113114
'The parameter type '
114-
'`${paramType.getDisplayString(withNullability: false)}` not have a well defined CustomParameterCodec decorator.',
115+
'`${withoutNullability(paramType.getDisplayString())}` not have a well defined CustomParameterCodec decorator.',
115116
element: element,
116117
);
117118
}
@@ -141,7 +142,7 @@ String decodeParameter(
141142
///
142143
/// Otherwise, throws an [InvalidGenerationSourceError].
143144
String encodeField(
144-
PropertyAccessorElement2 element,
145+
PropertyAccessorElement element,
145146
List<ElementAnnotation>? metadata,
146147
) {
147148
for (final _TypeHelper helper in _helpers) {
@@ -161,7 +162,7 @@ String encodeField(
161162
} else {
162163
throw InvalidGenerationSourceError(
163164
'The parameter type '
164-
'`${element.type.getDisplayString(withNullability: false)}` not have a well defined CustomParameterCodec decorator.',
165+
'`${withoutNullability(element.type.getDisplayString())}` not have a well defined CustomParameterCodec decorator.',
165166
element: element,
166167
);
167168
}
@@ -182,18 +183,15 @@ String encodeField(
182183
}
183184

184185
/// Returns an AstNode type from a InterfaceElement2.
185-
T? getNodeDeclaration<T extends AstNode>(InterfaceElement2 element) {
186+
T? getNodeDeclaration<T extends AstNode>(InterfaceElement element) {
186187
final AnalysisSession? session = element.session;
187188
if (session == null) {
188189
return null;
189190
}
190191

191192
final parsedLibrary =
192-
// ignore: experimental_member_use
193-
session.getParsedLibraryByElement2(element.library2)
194-
as ParsedLibraryResult;
193+
session.getParsedLibraryByElement(element.library) as ParsedLibraryResult;
195194
final FragmentDeclarationResult? declaration = parsedLibrary
196-
// ignore: experimental_member_use
197195
.getFragmentDeclaration(element.firstFragment);
198196
final AstNode? node = declaration?.node;
199197

@@ -698,10 +696,9 @@ class _TypeHelperJson extends _TypeHelperWithHelper {
698696
return false;
699697
}
700698

701-
final MethodElement2? toJsonMethod = type.lookUpMethod3(
699+
final MethodElement? toJsonMethod = type.lookUpMethod(
702700
'toJson',
703-
// ignore: experimental_member_use
704-
type.element3.library2,
701+
type.element.library,
705702
);
706703
if (toJsonMethod == null ||
707704
!toJsonMethod.isPublic ||
@@ -715,22 +712,21 @@ class _TypeHelperJson extends _TypeHelperWithHelper {
715712
return _matchesType(type.typeArguments.first);
716713
}
717714

718-
// ignore: experimental_member_use
719-
final ConstructorElement2? fromJsonMethod = type.element3
720-
.getNamedConstructor2('fromJson');
715+
final ConstructorElement? fromJsonMethod = type.element.getNamedConstructor(
716+
'fromJson',
717+
);
721718

722719
if (fromJsonMethod == null ||
723720
!fromJsonMethod.isPublic ||
724721
fromJsonMethod.formalParameters.length != 1 ||
725-
fromJsonMethod.formalParameters.first.type.getDisplayString(
726-
withNullability: false,
722+
withoutNullability(
723+
fromJsonMethod.formalParameters.first.type.getDisplayString(),
727724
) !=
728725
'Map<String, dynamic>') {
729726
throw InvalidGenerationSourceError(
730727
'The parameter type '
731-
'`${type.getDisplayString(withNullability: false)}` not have a supported fromJson definition.',
732-
// ignore: experimental_member_use
733-
element: type.element3,
728+
'`${withoutNullability(type.getDisplayString())}` not have a supported fromJson definition.',
729+
element: type.element,
734730
);
735731
}
736732

@@ -759,9 +755,9 @@ class _TypeHelperJson extends _TypeHelperWithHelper {
759755

760756
bool _isNestedTemplate(InterfaceType type) {
761757
// check if has fromJson constructor
762-
// ignore: experimental_member_use
763-
final ConstructorElement2? fromJsonMethod = type.element3
764-
.getNamedConstructor2('fromJson');
758+
final ConstructorElement? fromJsonMethod = type.element.getNamedConstructor(
759+
'fromJson',
760+
);
765761
if (fromJsonMethod == null || !fromJsonMethod.isPublic) {
766762
return false;
767763
}
@@ -778,13 +774,12 @@ class _TypeHelperJson extends _TypeHelperWithHelper {
778774
}
779775

780776
final FormalParameterElement firstParam = parameters[0];
781-
if (firstParam.type.getDisplayString(withNullability: false) !=
777+
if (withoutNullability(firstParam.type.getDisplayString()) !=
782778
'Map<String, dynamic>') {
783779
throw InvalidGenerationSourceError(
784780
'The parameter type '
785-
'`${type.getDisplayString(withNullability: false)}` not have a supported fromJson definition.',
786-
// ignore: experimental_member_use
787-
element: type.element3,
781+
'`${withoutNullability(type.getDisplayString())}` not have a supported fromJson definition.',
782+
element: type.element,
788783
);
789784
}
790785

@@ -795,17 +790,14 @@ class _TypeHelperJson extends _TypeHelperWithHelper {
795790
}
796791

797792
final functionType = secondParam.type as FunctionType;
798-
// ignore: experimental_member_use
799793
if (functionType.formalParameters.length != 1 ||
800794
functionType.returnType.getDisplayString() !=
801795
type.element.typeParameters.first.displayName ||
802-
// ignore: experimental_member_use
803796
functionType.formalParameters[0].type.getDisplayString() != 'Object?') {
804797
throw InvalidGenerationSourceError(
805798
'The parameter type '
806-
'`${type.getDisplayString(withNullability: false)}` not have a supported fromJson definition.',
807-
// ignore: experimental_member_use
808-
element: type.element3,
799+
'`${withoutNullability(type.getDisplayString())}` not have a supported fromJson definition.',
800+
element: type.element,
809801
);
810802
}
811803

@@ -866,7 +858,7 @@ extension FormalParameterElementExtension on FormalParameterElement {
866858
/// An error thrown when a default value is used with a nullable type.
867859
class NullableDefaultValueError extends InvalidGenerationSourceError {
868860
/// An error thrown when a default value is used with a nullable type.
869-
NullableDefaultValueError(Element2 element)
861+
NullableDefaultValueError(Element element)
870862
: super(
871863
'Default value used with a nullable type. Only non-nullable type can have a default value.',
872864
todo: 'Remove the default value or make the type non-nullable.',

0 commit comments

Comments
 (0)