Skip to content

Commit ef6ad7b

Browse files
authored
[go_router_builder] Adopt shared analyzer helpers from source_helper: ^1.4.0 (#13337)
Fixes flutter/flutter#194817 Adopts shared analyzer helpers published in [`package:source_helper` `1.4.0`](google/source_helper.dart#78) to replace local copies in `go_router_builder`: - Bumps `source_helper` constraint to `^1.4.0` and `go_router_builder` to `4.5.2`. - Replaces local `ExecutableElement.qualifiedName`, `withoutNullability` / `_isNullableType`, and `isEnumConstant` filtering across `type_helpers.dart`, `route_config.dart`, and `go_router_generator.dart` with `ExecutableElement.qualifiedName`, `DartType.toStringNonNullable()`, and `DartType.enumConstants` from `package:source_helper/source_helper.dart`. Pure internal deduplication; generated code is unchanged (`tool/run_tests.dart` and `example/test/ensure_build_test.dart` pass with zero diffs). ## 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 5d26c9b commit ef6ad7b

5 files changed

Lines changed: 20 additions & 55 deletions

File tree

‎packages/go_router_builder/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.5.2
2+
3+
- Adopts shared analyzer helpers from `package:source_helper` `^1.4.0`.
4+
15
## 4.5.1
26

37
- Migrates examples to `material_ui` package (1.4.0).

‎packages/go_router_builder/lib/src/go_router_generator.dart‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ 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';
11+
import 'package:source_helper/source_helper.dart';
1112

1213
import 'duplicate_path_severity.dart';
1314
import 'route_config.dart';
14-
import 'type_helpers.dart';
1515

1616
const String _routeDataUrl = 'package:go_router/src/route_data.dart';
1717

@@ -81,9 +81,7 @@ ${getters.map((String e) => "$e,").join('\n')}
8181
}
8282

8383
RouteBaseConfig _configForAnnotatedElement(Element element, ConstantReader annotation) {
84-
final String typedAnnotation = withoutNullability(
85-
annotation.objectValue.type!.getDisplayString(),
86-
);
84+
final String typedAnnotation = annotation.objectValue.type!.toStringNonNullable();
8785
final String type = typedAnnotation.substring(0, typedAnnotation.indexOf('<'));
8886
final String routeData = _annotations[type]!;
8987
if (element is! ClassElement) {

‎packages/go_router_builder/lib/src/route_config.dart‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ abstract class RouteBaseConfig {
803803
return false;
804804
}
805805
final DartType typeArgument = typeArguments.single;
806-
if (withoutNullability(typeArgument.getDisplayString()) != 'NavigatorState') {
806+
if (typeArgument.toStringNonNullable() != 'NavigatorState') {
807807
return false;
808808
}
809809
}
@@ -921,9 +921,7 @@ String _enumMapConst(InterfaceType type) {
921921

922922
final buffer = StringBuffer('const ${enumMapName(type)} = {');
923923

924-
for (final FieldElement enumField in type.element.fields.where(
925-
(FieldElement element) => element.isEnumConstant,
926-
)) {
924+
for (final FieldElement enumField in type.enumConstants) {
927925
buffer.writeln(
928926
'$enumName.${enumField.displayName}: ${escapeDartString(enumField.displayName.kebab)},',
929927
);

‎packages/go_router_builder/lib/src/type_helpers.dart‎

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ String decodeParameter(
129129
} else {
130130
throw InvalidGenerationSourceError(
131131
'The parameter type '
132-
'`${withoutNullability(paramType.getDisplayString())}` not have a well defined CustomParameterCodec decorator.',
132+
'`${paramType.toStringNonNullable()}` not have a well defined CustomParameterCodec decorator.',
133133
element: element,
134134
);
135135
}
@@ -150,7 +150,7 @@ String decodeParameter(
150150

151151
throw InvalidGenerationSourceError(
152152
'The parameter type '
153-
'`${withoutNullability(paramType.getDisplayString())}` is not supported. '
153+
'`${paramType.toStringNonNullable()}` is not supported. '
154154
'Consider using @TypedQueryParameter with a custom encoder and decoder.',
155155
element: element,
156156
);
@@ -189,7 +189,7 @@ String encodeField(PropertyAccessorElement element, List<ElementAnnotation>? met
189189
} else {
190190
throw InvalidGenerationSourceError(
191191
'The parameter type '
192-
'`${withoutNullability(element.type.getDisplayString())}` not have a well defined CustomParameterCodec decorator.',
192+
'`${element.type.toStringNonNullable()}` not have a well defined CustomParameterCodec decorator.',
193193
element: element,
194194
);
195195
}
@@ -271,16 +271,6 @@ String _stateValueAccess(FormalParameterElement element, Set<String> pathParamet
271271
return access;
272272
}
273273

274-
/// Returns `true` if the type string ends with a nullability marker (`?` or `*`)
275-
bool _isNullableType(String type) {
276-
return type.endsWith('?') || type.endsWith('*');
277-
}
278-
279-
/// Returns the type string without a trailing nullability marker
280-
String withoutNullability(String type) {
281-
return _isNullableType(type) ? type.substring(0, type.length - 1) : type;
282-
}
283-
284274
String _fieldWithEncoder(String field, String? customEncoder) {
285275
return customEncoder != null ? '$customEncoder($field)' : field;
286276
}
@@ -436,7 +426,7 @@ class _TypeHelperExtensionType extends _TypeHelper {
436426
'.$enumExtensionHelperName($stateValue) as $castType';
437427
}
438428

439-
final String representationTypeName = withoutNullability(representationType.getDisplayString());
429+
final String representationTypeName = representationType.toStringNonNullable();
440430
if (paramType.isNullableType || parameterElement.hasDefaultValue) {
441431
return "$representationTypeName.tryParse($stateValue ?? '') as $castType";
442432
} else {
@@ -453,7 +443,7 @@ class _TypeHelperExtensionType extends _TypeHelper {
453443

454444
if (representationType.isEnum) {
455445
return '${enumMapName(representationType as InterfaceType)}'
456-
'[$fieldName${type.ensureNotNull} as ${withoutNullability(representationType.getDisplayString())}]!';
446+
'[$fieldName${type.ensureNotNull} as ${representationType.toStringNonNullable()}]!';
457447
}
458448

459449
return '$fieldName${representationType.ensureNotNull}.toString()';
@@ -686,11 +676,11 @@ class _TypeHelperJson extends _TypeHelperWithHelper {
686676
if (fromJsonMethod == null ||
687677
!fromJsonMethod.isPublic ||
688678
fromJsonMethod.formalParameters.length != 1 ||
689-
withoutNullability(fromJsonMethod.formalParameters.first.type.getDisplayString()) !=
679+
fromJsonMethod.formalParameters.first.type.toStringNonNullable() !=
690680
'Map<String, dynamic>') {
691681
throw InvalidGenerationSourceError(
692682
'The parameter type '
693-
'`${withoutNullability(type.getDisplayString())}` not have a supported fromJson definition.',
683+
'`${type.toStringNonNullable()}` not have a supported fromJson definition.',
694684
element: type.element,
695685
);
696686
}
@@ -736,10 +726,10 @@ class _TypeHelperJson extends _TypeHelperWithHelper {
736726
}
737727

738728
final FormalParameterElement firstParam = parameters[0];
739-
if (withoutNullability(firstParam.type.getDisplayString()) != 'Map<String, dynamic>') {
729+
if (firstParam.type.toStringNonNullable() != 'Map<String, dynamic>') {
740730
throw InvalidGenerationSourceError(
741731
'The parameter type '
742-
'`${withoutNullability(type.getDisplayString())}` not have a supported fromJson definition.',
732+
'`${type.toStringNonNullable()}` not have a supported fromJson definition.',
743733
element: type.element,
744734
);
745735
}
@@ -757,7 +747,7 @@ class _TypeHelperJson extends _TypeHelperWithHelper {
757747
functionType.formalParameters[0].type.getDisplayString() != 'Object?') {
758748
throw InvalidGenerationSourceError(
759749
'The parameter type '
760-
'`${withoutNullability(type.getDisplayString())}` not have a supported fromJson definition.',
750+
'`${type.toStringNonNullable()}` not have a supported fromJson definition.',
761751
element: type.element,
762752
);
763753
}
@@ -878,28 +868,3 @@ class NullableDefaultValueError extends InvalidGenerationSourceError {
878868
const _typedQueryParameterChecker = TypeChecker.fromUrl(
879869
'package:go_router/src/route_data.dart#TypedQueryParameter',
880870
);
881-
882-
/// Extension helpers on [ExecutableElement].
883-
extension ExecutableElementExtension on ExecutableElement {
884-
/// Returns the name of `this` qualified with the class name if it's a
885-
/// [MethodElement].
886-
String get qualifiedName {
887-
if (this is TopLevelFunctionElement) {
888-
return name!;
889-
}
890-
891-
if (this is MethodElement) {
892-
return '${enclosingElement!.name}.$name';
893-
}
894-
895-
if (this is ConstructorElement) {
896-
// The default constructor.
897-
if ((name?.isEmpty ?? false) || name == 'new') {
898-
return enclosingElement!.name!;
899-
}
900-
return '${enclosingElement!.name}.$name';
901-
}
902-
903-
throw UnsupportedError('Not sure how to support typeof $runtimeType');
904-
}
905-
}

‎packages/go_router_builder/pubspec.yaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: go_router_builder
22
description: >-
33
A builder that supports generated strongly-typed route helpers for
44
package:go_router
5-
version: 4.5.1
5+
version: 4.5.2
66
repository: https://github.com/flutter/packages/tree/main/packages/go_router_builder
77
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router_builder%22
88

@@ -20,7 +20,7 @@ dependencies:
2020
meta: ^1.7.0
2121
path: ^1.8.0
2222
source_gen: ">=3.1.0 <5.0.0"
23-
source_helper: ^1.3.4
23+
source_helper: ^1.4.0
2424

2525
dev_dependencies:
2626
build_test: ^3.3.0

0 commit comments

Comments
 (0)