Repository navigation
Expand file tree
/
Copy pathroute_config.dart
More file actions
1076 lines (905 loc) · 35.2 KB
/
Copy pathroute_config.dart
File metadata and controls
1076 lines (905 loc) · 35.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:collection';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:build/build.dart';
import 'package:collection/collection.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as p;
import 'package:source_gen/source_gen.dart';
import 'package:source_helper/source_helper.dart';
import 'duplicate_path_severity.dart';
import 'path_utils.dart';
import 'type_helpers.dart';
/// Custom [Iterable] implementation with extra info.
class InfoIterable extends IterableBase<String> {
InfoIterable._({required this.members, required this.routeGetterName});
/// Name of the getter associated with `this`.
final String routeGetterName;
/// The generated elements associated with `this`.
final List<String> members;
@override
Iterator<String> get iterator => members.iterator;
}
/// The configuration to generate class declarations for a ShellRouteData.
class ShellRouteConfig extends RouteBaseConfig {
ShellRouteConfig._({
required this.navigatorKey,
required this.parentNavigatorKey,
required super.routeDataClass,
required this.observers,
required super.parent,
required this.restorationScopeId,
}) : super._();
/// The command for calling the navigator key getter from the ShellRouteData.
final String? navigatorKey;
/// The parent navigator key.
final String? parentNavigatorKey;
/// The navigator observers.
final String? observers;
/// The restoration scope id.
final String? restorationScopeId;
@override
Iterable<String> 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 <String>[
'''
extension $_extensionName on $_className {
static $_className _fromState(GoRouterState state) =>${isConst ? ' const' : ''} $_className();
}
''',
];
}
@override
String get routeConstructorParameters =>
'${navigatorKey == null ? '' : 'navigatorKey: $navigatorKey,'}'
'${parentNavigatorKey == null ? '' : 'parentNavigatorKey: $parentNavigatorKey,'}'
'${observers == null ? '' : 'observers: $observers,'}'
'${restorationScopeId == null ? '' : 'restorationScopeId: $restorationScopeId,'}';
@override
String get factorConstructorParameters => 'factory: $_extensionName._fromState,';
@override
String get routeDataClassName => 'ShellRouteData';
@override
String get dataConvertionFunctionName => r'$route';
}
/// The configuration to generate class declarations for a StatefulShellRouteData.
class StatefulShellRouteConfig extends RouteBaseConfig {
StatefulShellRouteConfig._({
required this.parentNavigatorKey,
required super.routeDataClass,
required super.parent,
required this.navigatorContainerBuilder,
required this.restorationScopeId,
}) : super._();
/// The parent navigator key.
final String? parentNavigatorKey;
/// The navigator container builder.
final String? navigatorContainerBuilder;
/// The restoration scope id.
final String? restorationScopeId;
@override
Iterable<String> classDeclarations() => <String>[
'''
extension $_extensionName on $_className {
static $_className _fromState(GoRouterState state) =>${routeDataClass.unnamedConstructor!.isConst ? ' const' : ''} $_className();
}
''',
];
@override
String get routeConstructorParameters =>
'${parentNavigatorKey == null ? '' : 'parentNavigatorKey: $parentNavigatorKey,'}'
'${restorationScopeId == null ? '' : 'restorationScopeId: $restorationScopeId,'}'
'${navigatorContainerBuilder == null ? '' : 'navigatorContainerBuilder: $navigatorContainerBuilder,'}';
@override
String get factorConstructorParameters => 'factory: $_extensionName._fromState,';
@override
String get routeDataClassName => 'StatefulShellRouteData';
@override
String get dataConvertionFunctionName => r'$route';
}
/// The configuration to generate class declarations for a StatefulShellBranchData.
class StatefulShellBranchConfig extends RouteBaseConfig {
StatefulShellBranchConfig._({
required this.navigatorKey,
required super.routeDataClass,
required super.parent,
required this.observers,
this.restorationScopeId,
this.initialLocation,
this.preload,
}) : super._();
/// The command for calling the navigator key getter from the ShellRouteData.
final String? navigatorKey;
/// The restoration scope id.
final String? restorationScopeId;
/// The initial route.
final String? initialLocation;
/// The navigator observers.
final String? observers;
/// The preload parameter.
final String? preload;
@override
Iterable<String> classDeclarations() => <String>[];
@override
String get factorConstructorParameters => '';
@override
String get routeConstructorParameters =>
'${navigatorKey == null ? '' : 'navigatorKey: $navigatorKey,'}'
'${restorationScopeId == null ? '' : 'restorationScopeId: $restorationScopeId,'}'
'${initialLocation == null ? '' : 'initialLocation: $initialLocation,'}'
'${observers == null ? '' : 'observers: $observers,'}'
'${preload == null ? '' : 'preload: $preload,'}';
@override
String get routeDataClassName => 'StatefulShellBranchData';
@override
String get dataConvertionFunctionName => r'$branch';
}
/// A mixin that provides common functionality for GoRoute-based configurations.
mixin _GoRouteMixin on RouteBaseConfig {
String get _basePathForLocation;
/// The path this route contributes to the URL, without any parent path.
///
/// This is the path exactly as written in the annotation.
String get path;
/// Whether this route only matches a URL that matches its path's casing.
bool get caseSensitive;
/// The path this route matches, joined with the paths of its ancestors.
///
/// Shell routes and branches contribute nothing, since they own no path, so
/// this is the URL pattern the route resolves to at runtime. Two routes with
/// the same pattern compete for the same URL no matter where they sit in the
/// tree, which is what [reportDuplicateRoutePaths] compares.
///
/// [_basePathForLocation] cannot serve here, because a relative route
/// deliberately reports only its own path.
String get _joinedPath {
final pathSegments = <String>[];
RouteBaseConfig? config = this;
while (config != null) {
if (config case _GoRouteMixin(:final String path)) {
pathSegments.add(path);
}
config = config.parent;
}
return p.url.joinAll(pathSegments.reversed);
}
late final Set<String> _pathParams = pathParametersFromPattern(_basePathForLocation);
// construct path bits using parent bits
// if there are any queryParam objects, add in the `queryParam` bits
String get _locationArgs {
final pathParameters = Map<String, String>.fromEntries(
_pathParams.map((String pathParameter) {
// Enum types are encoded using a map, so we need a nullability check
// here to ensure it matches Uri.encodeComponent nullability
final DartType? type = _field(pathParameter)?.returnType;
final valueBuffer = StringBuffer();
valueBuffer.write(r'${Uri.encodeComponent(');
valueBuffer.write(_encodeFor(pathParameter));
if (type?.isEnum ?? false) {
valueBuffer.write('!');
} else if (type?.isNullableType ?? false) {
valueBuffer.write("?? ''");
}
valueBuffer.write(')}');
return MapEntry<String, String>(pathParameter, valueBuffer.toString());
}),
);
final String location = patternToPath(_basePathForLocation, pathParameters);
return "'$location'";
}
/// The definition of the mixin to be generated.
String get _mixinDefinition;
FormalParameterElement? get _extraParam => _ctor.formalParameters.singleWhereOrNull(
(FormalParameterElement element) => element.isExtraField,
);
String get _fromStateConstructor {
final buffer = StringBuffer('=>');
if (_ctor.isConst && _ctorParams.isEmpty && _ctorQueryParams.isEmpty && _extraParam == null) {
buffer.writeln('const ');
}
buffer.writeln('$_className(');
for (final param in <FormalParameterElement>[
..._ctorParams,
..._ctorQueryParams,
if (_extraParam != null) _extraParam!,
]) {
buffer.write(_decodeFor(param));
}
buffer.writeln(');');
return buffer.toString();
}
String get _castedSelf {
if (_pathParams.isEmpty && _ctorQueryParams.isEmpty && _extraParam == null) {
return '';
}
return '\n$_className get $selfFieldName => this as $_className;\n';
}
String _decodeFor(FormalParameterElement element) {
if (element.isRequired) {
if (element.type.nullabilitySuffix == NullabilitySuffix.question &&
_pathParams.contains(element.displayName)) {
throw InvalidGenerationSourceError(
'Required parameters in the path cannot be nullable.',
element: element,
);
}
}
final List<ElementAnnotation>? metadata = _fieldMetadata(element.displayName);
final String fromStateExpression = decodeParameter(element, _pathParams, metadata);
if (element.isPositional) {
return '$fromStateExpression,';
}
if (element.isNamed) {
return '${element.displayName}: $fromStateExpression,';
}
throw InvalidGenerationSourceError(
'$likelyIssueMessage (param not named or positional)',
element: element,
);
}
String _encodeFor(String fieldName) {
final PropertyAccessorElement? field = _field(fieldName);
if (field == null) {
throw InvalidGenerationSourceError(
'Could not find a field for the path parameter "$fieldName".',
element: routeDataClass,
);
}
final List<ElementAnnotation>? metadata = _fieldMetadata(fieldName);
return encodeField(field, metadata);
}
String get _locationQueryParams {
if (_ctorQueryParams.isEmpty) {
return '';
}
final buffer = StringBuffer('queryParams: {\n');
for (final FormalParameterElement param in _ctorQueryParams) {
final String parameterName = param.displayName;
final conditions = <String>[];
if (param.hasDefaultValue) {
if (param.type.isNullableType) {
throw NullableDefaultValueError(param);
}
conditions.add(compareField(param));
} else if (param.type.isNullableType) {
conditions.add('$selfFieldName.$parameterName != null');
}
var line = '';
if (conditions.isNotEmpty) {
line = 'if (${conditions.join(' && ')}) ';
}
line +=
'${param.uriName}: '
'${_encodeFor(parameterName)},';
buffer.writeln(line);
}
buffer.writeln('},');
return buffer.toString();
}
late final List<FormalParameterElement> _ctorParams = _ctor.formalParameters.where((
FormalParameterElement element,
) {
if (_pathParams.contains(element.displayName)) {
return true;
}
return false;
}).toList();
late final List<FormalParameterElement> _ctorQueryParams = _ctor.formalParameters
.where(
(FormalParameterElement element) =>
!_pathParams.contains(element.displayName) && !element.isExtraField,
)
.toList();
ConstructorElement get _ctor {
final ConstructorElement? ctor = routeDataClass.unnamedConstructor;
if (ctor == null) {
throw InvalidGenerationSourceError('Missing default constructor', element: routeDataClass);
}
return ctor;
}
@override
Iterable<String> classDeclarations() => <String>[_mixinDefinition, ..._enumDeclarations()];
/// Returns code representing the constant maps that contain the `enum` to
/// [String] mapping for each referenced enum.
Iterable<String> _enumDeclarations() {
final enumParamTypes = <InterfaceType>{};
for (final ctorParam in <FormalParameterElement>[..._ctorParams, ..._ctorQueryParams]) {
DartType potentialEnumType = ctorParam.type;
if (potentialEnumType is ParameterizedType &&
(ctorParam.type as ParameterizedType).typeArguments.isNotEmpty) {
potentialEnumType = (ctorParam.type as ParameterizedType).typeArguments.first;
}
if (potentialEnumType.isEnum) {
enumParamTypes.add(potentialEnumType as InterfaceType);
}
// Support for enum extension types
final DartType representedType = potentialEnumType.extensionTypeErasure;
if (potentialEnumType != representedType && representedType.isEnum) {
enumParamTypes.add(representedType as InterfaceType);
}
}
return enumParamTypes.map<String>(_enumMapConst);
}
@override
String get factorConstructorParameters => 'factory: $_mixinName._fromState,';
@override
String get dataConvertionFunctionName => r'$route';
}
/// The configuration to generate class declarations for a GoRouteData.
class GoRouteConfig extends RouteBaseConfig with _GoRouteMixin {
GoRouteConfig._({
required this.path,
required this.name,
required this.caseSensitive,
required this.hasOverriddenOnExit,
required this.parentNavigatorKey,
required super.routeDataClass,
required super.parent,
}) : super._();
/// The path of the GoRoute to be created by this configuration.
@override
final String path;
/// The name of the GoRoute to be created by this configuration.
final String? name;
/// The case sensitivity of the GoRoute to be created by this configuration.
@override
final bool caseSensitive;
/// Whether to enable the onExit callback for this route.
///
/// When set to true, the route will include an onExit parameter in the
/// generated GoRoute constructor, allowing you to implement custom logic
/// when navigating away from this route.
final bool hasOverriddenOnExit;
/// The parent navigator key.
final String? parentNavigatorKey;
@override
String get _basePathForLocation => _joinedPath;
@override
String get _mixinDefinition {
final bool hasMixin =
getNodeDeclaration<ClassDeclaration>(routeDataClass)?.withClause?.mixinTypes
.any((NamedType e) => e.name.toString() == _mixinName) ??
false;
if (!hasMixin) {
throw InvalidGenerationSourceError(
'Missing mixin clause `with $_mixinName`',
element: routeDataClass,
);
}
return '''
mixin $_mixinName on $routeDataClassName {
static $_className _fromState(GoRouterState state) $_fromStateConstructor
$_castedSelf
@override
String get location => GoRouteData.\$location($_locationArgs,$_locationQueryParams);
@override
void go(BuildContext context) =>
context.go(location${_extraParam != null ? ', extra: $selfFieldName.$extraFieldName' : ''});
@override
Future<T?> push<T>(BuildContext context) =>
context.push<T>(location${_extraParam != null ? ', extra: $selfFieldName.$extraFieldName' : ''});
@override
void pushReplacement(BuildContext context) =>
context.pushReplacement(location${_extraParam != null ? ', extra: $selfFieldName.$extraFieldName' : ''});
@override
void replace(BuildContext context) =>
context.replace(location${_extraParam != null ? ', extra: $selfFieldName.$extraFieldName' : ''});
}
''';
}
@override
String get routeConstructorParameters =>
'path: ${escapeDartString(path)},'
'${name != null ? 'name: ${escapeDartString(name!)},' : ''}'
'${caseSensitive ? '' : 'caseSensitive: $caseSensitive,'}'
'${'hasOverriddenOnExit: $hasOverriddenOnExit,'}'
'${parentNavigatorKey == null ? '' : 'parentNavigatorKey: $parentNavigatorKey,'}';
@override
String get routeDataClassName => 'GoRouteData';
}
/// The configuration to generate class declarations for a RelativeGoRouteData.
class RelativeGoRouteConfig extends RouteBaseConfig with _GoRouteMixin {
RelativeGoRouteConfig._({
required this.path,
required this.caseSensitive,
required this.hasOverriddenOnExit,
required this.parentNavigatorKey,
required super.routeDataClass,
required super.parent,
}) : super._();
/// The path of the GoRoute to be created by this configuration.
@override
final String path;
/// The case sensitivity of the GoRoute to be created by this configuration.
@override
final bool caseSensitive;
/// Whether to enable the onExit callback for this route.
///
/// When set to true, the route will include an onExit parameter in the
/// generated GoRoute constructor, allowing you to implement custom logic
/// when navigating away from this route.
final bool hasOverriddenOnExit;
/// The parent navigator key.
final String? parentNavigatorKey;
@override
String get _basePathForLocation => path;
@override
String get _mixinDefinition {
final bool hasMixin =
getNodeDeclaration<ClassDeclaration>(routeDataClass)?.withClause?.mixinTypes
.any((NamedType e) => e.name.toString() == _mixinName) ??
false;
if (!hasMixin) {
throw InvalidGenerationSourceError(
'Missing mixin clause `with $_mixinName`',
element: routeDataClass,
);
}
return '''
mixin $_mixinName on $routeDataClassName {
static $_className _fromState(GoRouterState state) $_fromStateConstructor
$_castedSelf
@override
String get subLocation => RelativeGoRouteData.\$location($_locationArgs,$_locationQueryParams);
@override
String get relativeLocation => './\$subLocation';
@override
void goRelative(BuildContext context) =>
context.go(relativeLocation${_extraParam != null ? ', extra: $selfFieldName.$extraFieldName' : ''});
@override
Future<T?> pushRelative<T>(BuildContext context) =>
context.push<T>(relativeLocation${_extraParam != null ? ', extra: $selfFieldName.$extraFieldName' : ''});
@override
void pushReplacementRelative(BuildContext context) =>
context.pushReplacement(relativeLocation${_extraParam != null ? ', extra: $selfFieldName.$extraFieldName' : ''});
@override
void replaceRelative(BuildContext context) =>
context.replace(relativeLocation${_extraParam != null ? ', extra: $selfFieldName.$extraFieldName' : ''});
}
''';
}
@override
String get routeConstructorParameters =>
'path: ${escapeDartString(path)},'
'${caseSensitive ? '' : 'caseSensitive: $caseSensitive,'}'
'${'hasOverriddenOnExit: $hasOverriddenOnExit,'}'
'${parentNavigatorKey == null ? '' : 'parentNavigatorKey: $parentNavigatorKey,'}';
@override
String get routeDataClassName => 'RelativeGoRouteData';
}
/// Represents a `TypedGoRoute` annotation to the builder.
abstract class RouteBaseConfig {
RouteBaseConfig._({required this.routeDataClass, required this.parent});
/// Creates a new [RouteBaseConfig] represented the annotation data in [reader].
factory RouteBaseConfig.fromAnnotation(ConstantReader reader, InterfaceElement element) {
final definition = RouteBaseConfig._fromAnnotation(reader, element, null);
if (element != definition.routeDataClass) {
throw InvalidGenerationSourceError(
'The @TypedGoRoute annotation must have a type parameter that matches '
'the annotated element.',
element: element,
);
}
return definition;
}
factory RouteBaseConfig._fromAnnotation(
ConstantReader reader,
InterfaceElement element,
RouteBaseConfig? parent, {
bool isAncestorRelative = false,
}) {
assert(!reader.isNull, 'reader should not be null');
final type = reader.objectValue.type! as InterfaceType;
final String typeName = type.element.displayName;
if (isAncestorRelative && typeName == 'TypedGoRoute') {
throw InvalidGenerationSourceError(
'TypedRelativeGoRoute cannot have a TypedGoRoute descendant.',
element: element,
);
}
final bool isRelative = isAncestorRelative || typeName == 'TypedRelativeGoRoute';
final DartType typeParamType = type.typeArguments.single;
if (typeParamType is! InterfaceType) {
throw InvalidGenerationSourceError(
'The type parameter on one of the @TypedGoRoute declarations could not '
'be parsed.',
element: element,
);
}
// TODO(kevmoo): validate that this MUST be a subtype of `GoRouteData`
final InterfaceElement classElement = typeParamType.element;
final RouteBaseConfig value;
switch (typeName) {
case 'TypedShellRoute':
value = ShellRouteConfig._(
routeDataClass: classElement,
parent: parent,
navigatorKey: _generateParameterGetterCode(classElement, parameterName: r'$navigatorKey'),
parentNavigatorKey: _generateParameterGetterCode(
classElement,
parameterName: r'$parentNavigatorKey',
),
observers: _generateParameterGetterCode(classElement, parameterName: r'$observers'),
restorationScopeId: _generateParameterGetterCode(
classElement,
parameterName: r'$restorationScopeId',
),
);
case 'TypedStatefulShellRoute':
value = StatefulShellRouteConfig._(
routeDataClass: classElement,
parent: parent,
parentNavigatorKey: _generateParameterGetterCode(
classElement,
parameterName: r'$parentNavigatorKey',
),
restorationScopeId: _generateParameterGetterCode(
classElement,
parameterName: r'$restorationScopeId',
),
navigatorContainerBuilder: _generateParameterGetterCode(
classElement,
parameterName: r'$navigatorContainerBuilder',
),
);
case 'TypedStatefulShellBranch':
value = StatefulShellBranchConfig._(
routeDataClass: classElement,
parent: parent,
navigatorKey: _generateParameterGetterCode(classElement, parameterName: r'$navigatorKey'),
restorationScopeId: _generateParameterGetterCode(
classElement,
parameterName: r'$restorationScopeId',
),
initialLocation: _generateParameterGetterCode(
classElement,
parameterName: r'$initialLocation',
),
observers: _generateParameterGetterCode(classElement, parameterName: r'$observers'),
preload: _generateParameterGetterCode(classElement, parameterName: r'$preload'),
);
case 'TypedGoRoute':
final ConstantReader pathValue = reader.read('path');
if (pathValue.isNull) {
throw InvalidGenerationSourceError(
'Missing `path` value on annotation.',
element: element,
);
}
final ConstantReader nameValue = reader.read('name');
final ConstantReader caseSensitiveValue = reader.read('caseSensitive');
final bool hasOverriddenOnExit = classElement.methods.any(
(method) => method.name == 'onExit',
);
value = GoRouteConfig._(
path: pathValue.stringValue,
name: nameValue.isNull ? null : nameValue.stringValue,
caseSensitive: caseSensitiveValue.boolValue,
hasOverriddenOnExit: hasOverriddenOnExit,
routeDataClass: classElement,
parent: parent,
parentNavigatorKey: _generateParameterGetterCode(
classElement,
parameterName: r'$parentNavigatorKey',
),
);
case 'TypedRelativeGoRoute':
final ConstantReader pathValue = reader.read('path');
if (pathValue.isNull) {
throw InvalidGenerationSourceError(
'Missing `path` value on annotation.',
element: element,
);
}
final String pathString = pathValue.stringValue;
if (pathString.startsWith('/')) {
throw InvalidGenerationSourceError(
'The path for a TypedRelativeGoRoute cannot start with "/".',
element: element,
);
}
final ConstantReader caseSensitiveValue = reader.read('caseSensitive');
final bool hasOverriddenOnExit = classElement.methods.any(
(method) => method.name == 'onExit',
);
value = RelativeGoRouteConfig._(
path: pathValue.stringValue,
caseSensitive: caseSensitiveValue.boolValue,
hasOverriddenOnExit: hasOverriddenOnExit,
routeDataClass: classElement,
parent: parent,
parentNavigatorKey: _generateParameterGetterCode(
classElement,
parameterName: r'$parentNavigatorKey',
),
);
default:
throw UnsupportedError('Unrecognized type $typeName');
}
value._children.addAll(
reader
.read(_generateChildrenGetterName(typeName))
.listValue
.map<RouteBaseConfig>(
(DartObject e) => RouteBaseConfig._fromAnnotation(
ConstantReader(e),
element,
value,
isAncestorRelative: isRelative,
),
),
);
return value;
}
final List<RouteBaseConfig> _children = <RouteBaseConfig>[];
/// The `RouteData` class this class represents.
final InterfaceElement routeDataClass;
/// The parent of this route config.
final RouteBaseConfig? parent;
static String _generateChildrenGetterName(String name) {
return (name == 'TypedStatefulShellRoute' || name == 'StatefulShellRouteData')
? 'branches'
: 'routes';
}
static String? _generateParameterGetterCode(
InterfaceElement classElement, {
required String parameterName,
}) {
final String? fieldDisplayName = classElement.fields
.where((FieldElement element) {
if (!element.isStatic || element.displayName != parameterName) {
return false;
}
if (parameterName.toLowerCase().contains(RegExp('navigatorKey | observers'))) {
final DartType type = element.type;
if (type is! ParameterizedType) {
return false;
}
final List<DartType> typeArguments = type.typeArguments;
if (typeArguments.length != 1) {
return false;
}
final DartType typeArgument = typeArguments.single;
if (typeArgument.toStringNonNullable() != 'NavigatorState') {
return false;
}
}
return true;
})
.map<String>((FieldElement e) => e.displayName)
.firstOrNull;
if (fieldDisplayName != null) {
return '${classElement.displayName}.$fieldDisplayName';
}
final String? methodDisplayName = classElement.methods
.where((MethodElement element) {
return element.isStatic && element.displayName == parameterName;
})
.map<String>((MethodElement e) => e.displayName)
.firstOrNull;
if (methodDisplayName != null) {
return '${classElement.displayName}.$methodDisplayName';
}
return null;
}
/// Generates all of the members that correspond to `this`.
InfoIterable generateMembers() =>
InfoIterable._(members: _generateMembers().toList(), routeGetterName: _routeGetterName);
Iterable<String> _generateMembers() sync* {
final items = <String>[_rootDefinition()];
for (final RouteBaseConfig def in _flatten()) {
items.addAll(def.classDeclarations());
}
yield* items;
yield* items
.expand(
(String e) => helperNames.entries
.where((MapEntry<String, String> element) => e.contains(element.key))
.map((MapEntry<String, String> e) => e.value),
)
.toSet();
}
/// Returns this [GoRouteConfig] and all child [GoRouteConfig] instances.
Iterable<RouteBaseConfig> _flatten() sync* {
yield this;
for (final RouteBaseConfig child in _children) {
yield* child._flatten();
}
}
late final String _routeGetterName =
r'$' + _className.substring(0, 1).toLowerCase() + _className.substring(1);
/// Returns the `GoRoute` code for the annotated class.
String _rootDefinition() =>
'''
RouteBase get $_routeGetterName => ${_invokesRouteConstructor()};
''';
String get _className => routeDataClass.displayName;
String get _mixinName => '\$$_className';
String get _extensionName => '\$${_className}Extension';
String _invokesRouteConstructor() {
final routesBit = _children.isEmpty
? ''
: '''
${_generateChildrenGetterName(routeDataClassName)}: [${_children.map((RouteBaseConfig e) => '${e._invokesRouteConstructor()},').join()}],
''';
return '''
$routeDataClassName.$dataConvertionFunctionName(
$routeConstructorParameters $factorConstructorParameters $routesBit)
''';
}
PropertyAccessorElement? _field(String name) => routeDataClass.getGetter(name);
List<ElementAnnotation>? _fieldMetadata(String name) => routeDataClass.fields
.firstWhereOrNull((FieldElement element) => element.displayName == name)
?.metadata
.annotations;
/// The name of `RouteData` subclass this configuration represents.
@protected
String get routeDataClassName;
/// The function name of `RouteData` to get Routes or branches.
@protected
String get dataConvertionFunctionName;
/// Additional factory constructor.
@protected
String get factorConstructorParameters;
/// Additional constructor parameter for invoking route constructor.
@protected
String get routeConstructorParameters;
/// Returns all class declarations code.
@protected
Iterable<String> classDeclarations();
}
String _enumMapConst(InterfaceType type) {
assert(type.isEnum);
final String enumName = type.element.displayName;
final buffer = StringBuffer('const ${enumMapName(type)} = {');
for (final FieldElement enumField in type.enumConstants) {
buffer.writeln(
'$enumName.${enumField.displayName}: ${escapeDartString(enumField.displayName.kebab)},',
);
}
buffer.writeln('};');
return buffer.toString();
}
/// [Map] from the name of a generated helper to its definition.
const Map<String, String> helperNames = <String, String>{
convertMapValueHelperName: _convertMapValueHelper,
boolConverterHelperName: _boolConverterHelper,
enumExtensionHelperName: _enumConverterHelper,
iterablesEqualHelperName: _iterableEqualsHelper,
};
const String _convertMapValueHelper =
'''
T? $convertMapValueHelperName<T>(
String key,
Map<String, String> map,
T? Function(String) converter,
) {
final value = map[key];
return value == null ? null : converter(value);
}
''';
const String _boolConverterHelper =
'''
bool $boolConverterHelperName(String value) {
switch (value) {
case 'true':
return true;
case 'false':
return false;
default:
throw UnsupportedError('Cannot convert "\$value" into a bool.');
}
}
''';
const String _enumConverterHelper =
'''
extension<T extends Enum> on Map<T, String> {
T? $enumExtensionHelperName(String? value) =>
entries.where((element) => element.value == value).firstOrNull?.key;
}''';
const String _iterableEqualsHelper =
'''
bool $iterablesEqualHelperName<T>(Iterable<T>? iterable1, Iterable<T>? iterable2) {
if (identical(iterable1, iterable2)) return true;
if (iterable1 == null || iterable2 == null) return false;
final iterator1 = iterable1.iterator;
final iterator2 = iterable2.iterator;
while (true) {
final hasNext1 = iterator1.moveNext();
final hasNext2 = iterator2.moveNext();
if (hasNext1 != hasNext2) return false;
if (!hasNext1) return true;
if (iterator1.current != iterator2.current) return false;
}
}''';
/// Reports routes that resolve to the same URL pattern, with the severity
/// selected by the `duplicate_route_paths` builder option.
///
/// [roots] holds every top-level route config in a library, which is the widest
/// scope the builder can see, since it generates one library at a time.
///
/// Routes are compared by the whole URL pattern they resolve to, their
/// [_GoRouteMixin._joinedPath], rather than by the path each one declares. Two
/// routes at different depths can still land on one URL: a route nested as `a`