From 19ba1bec146fa2529e436e974f0aac3b83b451dc Mon Sep 17 00:00:00 2001 From: ValentinVignal Date: Wed, 2 Aug 2023 19:00:16 +0800 Subject: [PATCH 1/3] fix: Supports shell route data without const constructor --- .../lib/src/route_config.dart | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/go_router_builder/lib/src/route_config.dart b/packages/go_router_builder/lib/src/route_config.dart index ef3b82edfb61..1d607761a98e 100644 --- a/packages/go_router_builder/lib/src/route_config.dart +++ b/packages/go_router_builder/lib/src/route_config.dart @@ -47,13 +47,24 @@ class ShellRouteConfig extends RouteBaseConfig { final String? navigatorKey; @override - Iterable classDeclarations() => [ - ''' -extension $_extensionName on $_className { - static $_className _fromState(GoRouterState state) => const $_className(); -} -''' - ]; + Iterable classDeclarations() { + if (routeDataClass.unnamedConstructor == null) { + throw InvalidGenerationSourceError( + 'The ShellRouteData "$_className" class must have an unnamed constructor.', + element: routeDataClass, + ); + } + + final bool isConst = routeDataClass.unnamedConstructor!.isConst; + + return [ + ''' + extension $_extensionName on $_className { + static $_className _fromState(GoRouterState state) =>${isConst ? ' const' : ''} $_className(); + } + ''' + ]; + } @override String get routeConstructorParameters => From 11203973e25757cd50e2d241ac5844f7f7e2e7b9 Mon Sep 17 00:00:00 2001 From: ValentinVignal Date: Wed, 2 Aug 2023 19:00:27 +0800 Subject: [PATCH 2/3] test: Test code generation for shell routes --- .../test_inputs/shell_route_data.dart | 17 +++++++++++++++++ .../test_inputs/shell_route_data.dart.expect | 19 +++++++++++++++++++ ...oute_data_without_unnamed_constructor.dart | 12 ++++++++++++ ...ta_without_unnamed_constructor.dart.expect | 1 + 4 files changed, 49 insertions(+) create mode 100644 packages/go_router_builder/test_inputs/shell_route_data.dart create mode 100644 packages/go_router_builder/test_inputs/shell_route_data.dart.expect create mode 100644 packages/go_router_builder/test_inputs/shell_route_data_without_unnamed_constructor.dart create mode 100644 packages/go_router_builder/test_inputs/shell_route_data_without_unnamed_constructor.dart.expect diff --git a/packages/go_router_builder/test_inputs/shell_route_data.dart b/packages/go_router_builder/test_inputs/shell_route_data.dart new file mode 100644 index 000000000000..43f2b28498bd --- /dev/null +++ b/packages/go_router_builder/test_inputs/shell_route_data.dart @@ -0,0 +1,17 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:go_router/go_router.dart'; + +@TypedShellRoute( + routes: >[], +) +class ShellRouteNoConstConstructor extends ShellRouteData {} + +@TypedShellRoute( + routes: >[], +) +class ShellRouteWithConstConstructor extends ShellRouteData { + const ShellRouteWithConstConstructor(); +} diff --git a/packages/go_router_builder/test_inputs/shell_route_data.dart.expect b/packages/go_router_builder/test_inputs/shell_route_data.dart.expect new file mode 100644 index 000000000000..b99dd5176beb --- /dev/null +++ b/packages/go_router_builder/test_inputs/shell_route_data.dart.expect @@ -0,0 +1,19 @@ +RouteBase get $shellRouteNoConstConstructor => ShellRouteData.$route( + factory: $ShellRouteNoConstConstructorExtension._fromState, + ); + +extension $ShellRouteNoConstConstructorExtension + on ShellRouteNoConstConstructor { + static ShellRouteNoConstConstructor _fromState(GoRouterState state) => + ShellRouteNoConstConstructor(); +} + +RouteBase get $shellRouteWithConstConstructor => ShellRouteData.$route( + factory: $ShellRouteWithConstConstructorExtension._fromState, + ); + +extension $ShellRouteWithConstConstructorExtension + on ShellRouteWithConstConstructor { + static ShellRouteWithConstConstructor _fromState(GoRouterState state) => + const ShellRouteWithConstConstructor(); +} diff --git a/packages/go_router_builder/test_inputs/shell_route_data_without_unnamed_constructor.dart b/packages/go_router_builder/test_inputs/shell_route_data_without_unnamed_constructor.dart new file mode 100644 index 000000000000..24e97bfd9c22 --- /dev/null +++ b/packages/go_router_builder/test_inputs/shell_route_data_without_unnamed_constructor.dart @@ -0,0 +1,12 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:go_router/go_router.dart'; + +@TypedShellRoute( + routes: >[], +) +class ShellRouteWithoutUnnamedConstructor extends ShellRouteData { + const ShellRouteWithoutUnnamedConstructor.namedConstructor(); +} diff --git a/packages/go_router_builder/test_inputs/shell_route_data_without_unnamed_constructor.dart.expect b/packages/go_router_builder/test_inputs/shell_route_data_without_unnamed_constructor.dart.expect new file mode 100644 index 000000000000..c3d6e57e861a --- /dev/null +++ b/packages/go_router_builder/test_inputs/shell_route_data_without_unnamed_constructor.dart.expect @@ -0,0 +1 @@ +The ShellRouteData "ShellRouteWithoutUnnamedConstructor" class must have an unnamed constructor. From 5b99f515c4f357143458a726ac85dbe2d74cf4c1 Mon Sep 17 00:00:00 2001 From: ValentinVignal Date: Wed, 2 Aug 2023 19:01:22 +0800 Subject: [PATCH 3/3] core: Update version --- packages/go_router_builder/CHANGELOG.md | 4 ++++ packages/go_router_builder/pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/go_router_builder/CHANGELOG.md b/packages/go_router_builder/CHANGELOG.md index cb3433fb376c..11aeb62a35d9 100644 --- a/packages/go_router_builder/CHANGELOG.md +++ b/packages/go_router_builder/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.2.5 + +* Fixes a bug where shell routes without const constructor were not generated correctly. + ## 2.2.4 * Bumps example go_router version to v10.0.0 and migrate example code. diff --git a/packages/go_router_builder/pubspec.yaml b/packages/go_router_builder/pubspec.yaml index 1bd454a71be6..dac268f794b7 100644 --- a/packages/go_router_builder/pubspec.yaml +++ b/packages/go_router_builder/pubspec.yaml @@ -2,7 +2,7 @@ name: go_router_builder description: >- A builder that supports generated strongly-typed route helpers for package:go_router -version: 2.2.4 +version: 2.2.5 repository: https://github.com/flutter/packages/tree/main/packages/go_router_builder issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router_builder%22