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
4 changes: 4 additions & 0 deletions pkgs/shelf_router_generator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.4-wip

* Require `analyzer: '>=8.1.1 <10.0.0'

## 1.1.3

* Require `analyzer: ^8.1.1`
Expand Down
43 changes: 21 additions & 22 deletions pkgs/shelf_router_generator/lib/src/shelf_router_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import 'dart:async' show Future;

import 'package:analyzer/dart/element/element2.dart'
show ClassElement2, ElementKind, ExecutableElement2;
import 'package:analyzer/dart/element/element.dart'
show ClassElement, ElementKind, ExecutableElement;
import 'package:analyzer/dart/element/type.dart' show ParameterizedType;
import 'package:build/build.dart' show BuildStep, log;
import 'package:code_builder/code_builder.dart' as code;
Expand Down Expand Up @@ -48,37 +48,36 @@ const _stringType = g.TypeChecker.typeNamed(String, inSdk: true);
/// A representation of a handler that was annotated with [shelf_router.Route].
class _Handler {
final String verb, route;
final ExecutableElement2 element;
final ExecutableElement element;

_Handler(this.verb, this.route, this.element);
}

/// Find members of a class annotated with [shelf_router.Route].
List<ExecutableElement2> getAnnotatedElementsOrderBySourceOffset(
ClassElement2 cls,
List<ExecutableElement> getAnnotatedElementsOrderBySourceOffset(
ClassElement cls,
) =>
<ExecutableElement2>[
...cls.methods2.where(_routeType.hasAnnotationOfExact),
...cls.getters2.where(_routeType.hasAnnotationOfExact),
<ExecutableElement>[
...cls.methods.where(_routeType.hasAnnotationOfExact),
...cls.getters.where(_routeType.hasAnnotationOfExact),
]..sort(
(a, b) => (a.firstFragment.nameOffset2!).compareTo(
b.firstFragment.nameOffset2!,
),
(a, b) =>
(a.firstFragment.nameOffset!).compareTo(b.firstFragment.nameOffset!),
);

/// Generate a `_$<className>Router(<className> service)` method that returns a
/// [shelf_router.Router] configured based on annotated handlers.
code.Method _buildRouterMethod({
required ClassElement2 classElement,
required ClassElement classElement,
required List<_Handler> handlers,
}) => code.Method(
(b) => b
..name = '_\$${classElement.name3}Router'
..name = '_\$${classElement.name}Router'
..requiredParameters.add(
code.Parameter(
(b) => b
..name = 'service'
..type = code.refer(classElement.name3!),
..type = code.refer(classElement.name!),
),
)
..returns = code.refer('Router')
Expand Down Expand Up @@ -110,16 +109,16 @@ code.Code _buildAddHandlerCode({
}) => switch (handler.verb) {
r'$mount' => router.property('mount').call([
code.literalString(handler.route, raw: true),
service.property(handler.element.name3!).property('call'),
service.property(handler.element.name!).property('call'),
]).statement,
r'$all' => router.property('all').call([
code.literalString(handler.route, raw: true),
service.property(handler.element.name3!),
service.property(handler.element.name!),
]).statement,
_ => router.property('add').call([
code.literalString(handler.verb.toUpperCase()),
code.literalString(handler.route, raw: true),
service.property(handler.element.name3!),
service.property(handler.element.name!),
]).statement,
};

Expand All @@ -128,13 +127,13 @@ class ShelfRouterGenerator extends g.Generator {
Future<String?> generate(g.LibraryReader library, BuildStep buildStep) async {
// Create a map from ClassElement to list of annotated elements sorted by
// offset in source code, this is not type checked yet.
final classes = <ClassElement2, List<_Handler>>{};
final classes = <ClassElement, List<_Handler>>{};
for (final cls in library.classes) {
final elements = getAnnotatedElementsOrderBySourceOffset(cls);
if (elements.isEmpty) {
continue;
}
log.info('found shelf_router.Route annotations in ${cls.name3}');
log.info('found shelf_router.Route annotations in ${cls.name}');

classes[cls] = elements
.map(
Expand Down Expand Up @@ -258,12 +257,12 @@ void _typeCheckHandler(_Handler h) {
}
for (var i = 0; i < params.length; i++) {
final p = h.element.formalParameters[i + 1];
if (p.name3 != params[i]) {
if (p.name != params[i]) {
throw g.InvalidGenerationSourceError(
'The shelf_router.Route annotation can only be used on shelf '
'request handlers accept a shelf.Request parameter and/or a '
'shelf.Request parameter and all string parameters in the route, '
'the "${p.name3}" parameter should be named "${params[i]}"',
'the "${p.name}" parameter should be named "${params[i]}"',
element: p,
);
}
Expand All @@ -272,7 +271,7 @@ void _typeCheckHandler(_Handler h) {
'The shelf_router.Route annotation can only be used on shelf '
'request handlers accept a shelf.Request parameter and/or a '
'shelf.Request parameter and all string parameters in the route, '
'the "${p.name3}" parameter is not of type string',
'the "${p.name}" parameter is not of type string',
element: p,
);
}
Expand Down
6 changes: 3 additions & 3 deletions pkgs/shelf_router_generator/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: shelf_router_generator
version: 1.1.3
version: 1.1.4-wip
description: >
A package:build-compatible builder for generating request routers for the
shelf web-framework based on source annotations.
Expand All @@ -14,7 +14,7 @@ environment:
sdk: ^3.9.0

dependencies:
analyzer: ^8.1.1
analyzer: '>=8.1.1 <10.0.0'
build: ^4.0.0
build_config: ^1.2.0
code_builder: ^4.2.0
Expand All @@ -24,7 +24,7 @@ dependencies:
source_gen: ^4.0.1

dev_dependencies:
build_runner: ^2.7.2
build_runner: ^2.8.0
build_verify: ^3.0.0
dart_flutter_team_lints: ^3.0.0
http: ^1.0.0
Expand Down