Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/go_router_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## NEXT
## 3.1.0

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

## 3.0.1

Expand Down
6 changes: 3 additions & 3 deletions packages/go_router_builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ To use `go_router_builder`, you need to have the following dependencies in
```yaml
dependencies:
# ...along with your other dependencies
go_router: ^9.0.3
go_router: ^16.0.0

dev_dependencies:
# ...along with your other dev-dependencies
build_runner: ^2.0.0
go_router_builder: ^2.3.0
build_runner: ^2.6.0
go_router_builder: ^3.1.0
```

### Source code
Expand Down
6 changes: 2 additions & 4 deletions packages/go_router_builder/lib/go_router_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,5 @@ import 'src/go_router_generator.dart';
/// `go_router`.
///
/// Not meant to be invoked by hand-authored code.
Builder goRouterBuilder(BuilderOptions options) => SharedPartBuilder(
const <Generator>[GoRouterGenerator()],
'go_router',
);
Builder goRouterBuilder(BuilderOptions options) =>
SharedPartBuilder(const <Generator>[GoRouterGenerator()], 'go_router');
50 changes: 28 additions & 22 deletions packages/go_router_builder/lib/src/go_router_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import 'dart:async';

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:build/build.dart';
import 'package:source_gen/source_gen.dart';
Expand All @@ -27,9 +27,10 @@ class GoRouterGenerator extends Generator {
const GoRouterGenerator();

TypeChecker get _typeChecker => TypeChecker.any(
_annotations.keys.map((String annotation) =>
TypeChecker.fromUrl('$_routeDataUrl#$annotation')),
);
_annotations.keys.map(
(String annotation) => TypeChecker.fromUrl('$_routeDataUrl#$annotation'),
),
);

@override
FutureOr<String> generate(LibraryReader library, BuildStep buildStep) async {
Expand Down Expand Up @@ -61,48 +62,53 @@ ${getters.map((String e) => "$e,").join('\n')}
Set<String> values,
Set<String> getters,
) {
for (final AnnotatedElement annotatedElement
in library.annotatedWith(_typeChecker)) {
for (final AnnotatedElement annotatedElement in library.annotatedWith(
_typeChecker,
)) {
final InfoIterable generatedValue = _generateForAnnotatedElement(
annotatedElement.element,
annotatedElement.annotation,
);
getters.add(generatedValue.routeGetterName);
for (final String value in generatedValue) {
assert(value.length == value.trim().length);
values.add(value);
}
values.addAll(generatedValue.members);
}
}

InfoIterable _generateForAnnotatedElement(
Element element,
Element2 element,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking at the doc https://pub.dev/documentation/analyzer/latest/dart_element_element/Element2.html

should we keep the original class name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chunhtai
Element2 will be deprecated starting with analyzer v8.0.0. Since analyzer: “>=7.4.0 <8.0.0” is set, Element2 is not deprecated.

https://pub.dev/documentation/analyzer/7.7.1/dart_element_element2/Element2-class.html
https://pub.dev/documentation/analyzer/8.0.0/dart_element_element/Element2.html

Other libraries are also replacing Element2 in the same way.

google/json_serializable.dart#1504
rrousselGit/freezed#1276

Copy link
Contributor

@chunhtai chunhtai Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we pin analyzer 8.0.0 and above directly?

Copy link
Contributor Author

@koji-1009 koji-1009 Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConstantReader annotation,
) {
final String typedAnnotation =
withoutNullability(annotation.objectValue.type!.getDisplayString());
final String type =
typedAnnotation.substring(0, typedAnnotation.indexOf('<'));
final String typedAnnotation = withoutNullability(
annotation.objectValue.type!.getDisplayString(),
);
final String type = typedAnnotation.substring(
0,
typedAnnotation.indexOf('<'),
);
final String routeData = _annotations[type]!;
if (element is! ClassElement) {
if (element is! ClassElement2) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw InvalidGenerationSourceError(
'The @$type annotation can only be applied to classes.',
element: element,
);
}

final TypeChecker dataChecker =
TypeChecker.fromUrl('$_routeDataUrl#$routeData');
if (!element.allSupertypes
.any((InterfaceType element) => dataChecker.isExactlyType(element))) {
final TypeChecker dataChecker = TypeChecker.fromUrl(
'$_routeDataUrl#$routeData',
);
if (!element.allSupertypes.any(
(InterfaceType element) => dataChecker.isExactlyType(element),
)) {
throw InvalidGenerationSourceError(
'The @$type annotation can only be applied to classes that '
'extend or implement `$routeData`.',
element: element,
);
}

return RouteBaseConfig.fromAnnotation(annotation, element)
.generateMembers();
return RouteBaseConfig.fromAnnotation(
annotation,
element,
).generateMembers();
}
}
6 changes: 3 additions & 3 deletions packages/go_router_builder/lib/src/path_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ final RegExp _parameterRegExp = RegExp(r':(\w+)(\((?:\\.|[^\\()])+\))?');
/// final pathParameters = pathParametersFromPattern(pattern); // {'id', 'bookId'}
/// ```
Set<String> pathParametersFromPattern(String pattern) => <String>{
for (final RegExpMatch match in _parameterRegExp.allMatches(pattern))
match[1]!,
};
for (final RegExpMatch match in _parameterRegExp.allMatches(pattern))
match[1]!,
};

/// Reconstructs the full path from a [pattern] and path parameters.
///
Expand Down
Loading