Skip to content

Commit 6cb9113

Browse files
authored
[go_router_builder] Migrate to Element2 API and update dependencies (#9649)
close flutter/flutter#170832 close flutter/flutter#171003 * Update [build](https://pub.dev/packages/build), [build_test](https://pub.dev/packages/build_test), and [source_gen](https://pub.dev/packages/source_gen) versions. * Update [analyzer](https://pub.dev/packages/analyzer) version to support Element2 API. * Fix unit test code formatting and comparison logic * Flutter 3.29 and Dart 3.7. ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^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 08a9b2c commit 6cb9113

12 files changed

+283
-225
lines changed

packages/go_router_builder/CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
## NEXT
1+
## 3.1.0
22

3-
- Restricts `build` to versions less than 2.5.0.
3+
- Updates dependencies to use the latest `analyzer`, `build`, and `source_gen`.
4+
- Updates dev dependencies to use the latest `build_test`.
5+
- Migrates to the `element2` API.
6+
- Improves test code formatting consistency.
7+
- Updates minimum supported SDK version to Flutter 3.29/Dart 3.7.
48

59
## 3.0.1
610

packages/go_router_builder/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ To use `go_router_builder`, you need to have the following dependencies in
88
```yaml
99
dependencies:
1010
# ...along with your other dependencies
11-
go_router: ^9.0.3
11+
go_router: ^16.0.0
1212

1313
dev_dependencies:
1414
# ...along with your other dev-dependencies
15-
build_runner: ^2.0.0
16-
go_router_builder: ^2.3.0
15+
build_runner: ^2.6.0
16+
go_router_builder: ^3.1.0
1717
```
1818
1919
### Source code

packages/go_router_builder/lib/go_router_builder.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,5 @@ import 'src/go_router_generator.dart';
2121
/// `go_router`.
2222
///
2323
/// Not meant to be invoked by hand-authored code.
24-
Builder goRouterBuilder(BuilderOptions options) => SharedPartBuilder(
25-
const <Generator>[GoRouterGenerator()],
26-
'go_router',
27-
);
24+
Builder goRouterBuilder(BuilderOptions options) =>
25+
SharedPartBuilder(const <Generator>[GoRouterGenerator()], 'go_router');

packages/go_router_builder/lib/src/go_router_generator.dart

Lines changed: 28 additions & 22 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/element.dart';
7+
import 'package:analyzer/dart/element/element2.dart';
88
import 'package:analyzer/dart/element/type.dart';
99
import 'package:build/build.dart';
1010
import 'package:source_gen/source_gen.dart';
@@ -27,9 +27,10 @@ class GoRouterGenerator extends Generator {
2727
const GoRouterGenerator();
2828

2929
TypeChecker get _typeChecker => TypeChecker.any(
30-
_annotations.keys.map((String annotation) =>
31-
TypeChecker.fromUrl('$_routeDataUrl#$annotation')),
32-
);
30+
_annotations.keys.map(
31+
(String annotation) => TypeChecker.fromUrl('$_routeDataUrl#$annotation'),
32+
),
33+
);
3334

3435
@override
3536
FutureOr<String> generate(LibraryReader library, BuildStep buildStep) async {
@@ -61,48 +62,53 @@ ${getters.map((String e) => "$e,").join('\n')}
6162
Set<String> values,
6263
Set<String> getters,
6364
) {
64-
for (final AnnotatedElement annotatedElement
65-
in library.annotatedWith(_typeChecker)) {
65+
for (final AnnotatedElement annotatedElement in library.annotatedWith(
66+
_typeChecker,
67+
)) {
6668
final InfoIterable generatedValue = _generateForAnnotatedElement(
6769
annotatedElement.element,
6870
annotatedElement.annotation,
6971
);
7072
getters.add(generatedValue.routeGetterName);
71-
for (final String value in generatedValue) {
72-
assert(value.length == value.trim().length);
73-
values.add(value);
74-
}
73+
values.addAll(generatedValue.members);
7574
}
7675
}
7776

7877
InfoIterable _generateForAnnotatedElement(
79-
Element element,
78+
Element2 element,
8079
ConstantReader annotation,
8180
) {
82-
final String typedAnnotation =
83-
withoutNullability(annotation.objectValue.type!.getDisplayString());
84-
final String type =
85-
typedAnnotation.substring(0, typedAnnotation.indexOf('<'));
81+
final String typedAnnotation = withoutNullability(
82+
annotation.objectValue.type!.getDisplayString(),
83+
);
84+
final String type = typedAnnotation.substring(
85+
0,
86+
typedAnnotation.indexOf('<'),
87+
);
8688
final String routeData = _annotations[type]!;
87-
if (element is! ClassElement) {
89+
if (element is! ClassElement2) {
8890
throw InvalidGenerationSourceError(
8991
'The @$type annotation can only be applied to classes.',
9092
element: element,
9193
);
9294
}
9395

94-
final TypeChecker dataChecker =
95-
TypeChecker.fromUrl('$_routeDataUrl#$routeData');
96-
if (!element.allSupertypes
97-
.any((InterfaceType element) => dataChecker.isExactlyType(element))) {
96+
final TypeChecker dataChecker = TypeChecker.fromUrl(
97+
'$_routeDataUrl#$routeData',
98+
);
99+
if (!element.allSupertypes.any(
100+
(InterfaceType element) => dataChecker.isExactlyType(element),
101+
)) {
98102
throw InvalidGenerationSourceError(
99103
'The @$type annotation can only be applied to classes that '
100104
'extend or implement `$routeData`.',
101105
element: element,
102106
);
103107
}
104108

105-
return RouteBaseConfig.fromAnnotation(annotation, element)
106-
.generateMembers();
109+
return RouteBaseConfig.fromAnnotation(
110+
annotation,
111+
element,
112+
).generateMembers();
107113
}
108114
}

packages/go_router_builder/lib/src/path_utils.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ final RegExp _parameterRegExp = RegExp(r':(\w+)(\((?:\\.|[^\\()])+\))?');
1515
/// final pathParameters = pathParametersFromPattern(pattern); // {'id', 'bookId'}
1616
/// ```
1717
Set<String> pathParametersFromPattern(String pattern) => <String>{
18-
for (final RegExpMatch match in _parameterRegExp.allMatches(pattern))
19-
match[1]!,
20-
};
18+
for (final RegExpMatch match in _parameterRegExp.allMatches(pattern))
19+
match[1]!,
20+
};
2121

2222
/// Reconstructs the full path from a [pattern] and path parameters.
2323
///

0 commit comments

Comments
 (0)