Skip to content

Commit 2162d41

Browse files
committed
Validate entire subtree
1 parent 1e551d2 commit 2162d41

6 files changed

+54
-6
lines changed

packages/go_router_builder/lib/src/route_config.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -599,19 +599,23 @@ abstract class RouteBaseConfig {
599599
factory RouteBaseConfig._fromAnnotation(
600600
ConstantReader reader,
601601
InterfaceElement element,
602-
RouteBaseConfig? parent,
603-
) {
602+
RouteBaseConfig? parent, {
603+
bool isAncestorRelative = false,
604+
}) {
604605
assert(!reader.isNull, 'reader should not be null');
605606
final InterfaceType type = reader.objectValue.type! as InterfaceType;
606607
final String typeName = type.element.name;
607608

608-
if (parent is RelativeGoRouteConfig && typeName == 'TypedGoRoute') {
609+
if (isAncestorRelative && typeName == 'TypedGoRoute') {
609610
throw InvalidGenerationSourceError(
610-
'TypedRelativeGoRoute cannot have a TypedGoRoute as a child.',
611+
'TypedRelativeGoRoute cannot have a TypedGoRoute descendant.',
611612
element: element,
612613
);
613614
}
614615

616+
final bool isRelative =
617+
isAncestorRelative || typeName == 'TypedRelativeGoRoute';
618+
615619
final DartType typeParamType = type.typeArguments.single;
616620
if (typeParamType is! InterfaceType) {
617621
throw InvalidGenerationSourceError(
@@ -744,7 +748,8 @@ abstract class RouteBaseConfig {
744748
.read(_generateChildrenGetterName(typeName))
745749
.listValue
746750
.map<RouteBaseConfig>((DartObject e) => RouteBaseConfig._fromAnnotation(
747-
ConstantReader(e), element, value)));
751+
ConstantReader(e), element, value,
752+
isAncestorRelative: isRelative)));
748753

749754
return value;
750755
}

packages/go_router_builder/test_inputs/relative_route_with_absolute_sub_route.dart.expect

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TypedRelativeGoRoute cannot have a TypedGoRoute descendant.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:go_router/go_router.dart';
6+
7+
mixin _$HomeRoute {}
8+
mixin _$ShellRoute {}
9+
mixin _$RelativeRoute {}
10+
mixin _$AbsoluteRoute {}
11+
12+
@TypedGoRoute<HomeRoute>(
13+
path: '/',
14+
routes: <TypedRoute<RouteData>>[relativeRoute],
15+
)
16+
class HomeRoute extends GoRouteData with _$HomeRoute {
17+
const HomeRoute();
18+
}
19+
20+
const TypedRelativeGoRoute<RelativeRoute> relativeRoute =
21+
TypedRelativeGoRoute<RelativeRoute>(
22+
path: 'relative-route',
23+
routes: <TypedRoute<RouteData>>[shellRoute],
24+
);
25+
26+
const TypedShellRoute<ShellRoute> shellRoute =
27+
TypedShellRoute<ShellRoute>(routes: <TypedRoute<RouteData>>[absoluteRoute]);
28+
29+
const TypedGoRoute<AbsoluteRoute> absoluteRoute =
30+
TypedGoRoute<AbsoluteRoute>(path: 'absolute-route');
31+
32+
class RelativeRoute extends RelativeGoRouteData with _$RelativeRoute {
33+
const RelativeRoute();
34+
}
35+
36+
class ShellRoute extends ShellRouteData with _$ShellRoute {
37+
const ShellRoute();
38+
}
39+
40+
class AbsoluteRoute extends GoRouteData with _$AbsoluteRoute {
41+
const AbsoluteRoute();
42+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TypedRelativeGoRoute cannot have a TypedGoRoute descendant.

0 commit comments

Comments
 (0)