-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[go_router_builder] Migrate to Element2 API and update dependencies #9649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+283
−225
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4a699ef
feat: Update dependencies and migrate to element2 API
koji-1009 d47937e
fix: set readAllSourcesFromFilesystem
koji-1009 614c2b2
test: improve code formatting consistency in test
koji-1009 5a0cb6f
doc: CHANGLOG
koji-1009 4f83b21
refactor: dart format
koji-1009 a4584b4
Merge branch 'main' into feat/element2
koji-1009 4003e03
Merge branch 'main' into feat/element2
koji-1009 96d68f9
test: Normalize path separators for cross-platform compatibility
koji-1009 b6f37a8
doc: Update README
koji-1009 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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 { | ||
|
@@ -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, | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chunhtai
It will be difficult as it was not announced in advance. Specifying
^8.0.0
will break compatibility with other packages.https://github.com/dart-lang/mockito/blob/v5.4.5/pubspec.yaml
https://github.com/google/json_serializable.dart/blob/json_serializable-v6.10.0/json_serializable/pubspec.yaml
https://github.com/rrousselGit/freezed/blob/freezed-v3.2.0/packages/freezed/pubspec.yaml