Skip to content

Commit 1af4d1a

Browse files
[go_router_builder] Removes path_to_regexp from the dependencies (#4524)
Fixes flutter/flutter#130817 Relates to flutter/flutter#122713 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].*
1 parent 203ad04 commit 1af4d1a

File tree

6 files changed

+104
-24
lines changed

6 files changed

+104
-24
lines changed

packages/go_router_builder/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.2.3
2+
3+
* Removes `path_to_regexp` from the dependencies.
4+
15
## 2.2.2
26

37
* Bumps example go_router version and migrate example code.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
final RegExp _parameterRegExp = RegExp(r':(\w+)(\((?:\\.|[^\\()])+\))?');
6+
7+
/// Extracts the path parameters from a [pattern] such as `/user/:id`.
8+
///
9+
/// The path parameters can be specified by prefixing them with `:`.
10+
///
11+
/// For example:
12+
///
13+
/// ```dart
14+
/// final pattern = '/user/:id/book/:bookId';
15+
/// final pathParameters = pathParametersFromPattern(pattern); // {'id', 'bookId'}
16+
/// ```
17+
Set<String> pathParametersFromPattern(String pattern) => <String>{
18+
for (final RegExpMatch match in _parameterRegExp.allMatches(pattern))
19+
match[1]!,
20+
};
21+
22+
/// Reconstructs the full path from a [pattern] and path parameters.
23+
///
24+
/// For example:
25+
///
26+
/// ```dart
27+
/// final pattern = '/family/:id';
28+
/// final path = patternToPath(pattern, {'id': 'family-id'}); // '/family/family-id'
29+
/// ```
30+
String patternToPath(String pattern, Map<String, String> pathParameters) {
31+
final StringBuffer buffer = StringBuffer();
32+
int start = 0;
33+
for (final RegExpMatch match in _parameterRegExp.allMatches(pattern)) {
34+
if (match.start > start) {
35+
buffer.write(pattern.substring(start, match.start));
36+
}
37+
final String name = match[1]!;
38+
buffer.write(pathParameters[name]);
39+
start = match.end;
40+
}
41+
42+
if (start < pattern.length) {
43+
buffer.write(pattern.substring(start));
44+
}
45+
return buffer.toString();
46+
}

packages/go_router_builder/lib/src/route_config.dart

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import 'package:analyzer/dart/element/type.dart';
1111
import 'package:collection/collection.dart';
1212
import 'package:meta/meta.dart';
1313
import 'package:path/path.dart' as p;
14-
import 'package:path_to_regexp/path_to_regexp.dart';
1514
import 'package:source_gen/source_gen.dart';
1615
import 'package:source_helper/source_helper.dart';
1716

17+
import 'path_utils.dart';
1818
import 'type_helpers.dart';
1919

2020
/// Custom [Iterable] implementation with extra info.
@@ -79,12 +79,8 @@ class GoRouteConfig extends RouteBaseConfig {
7979
/// The name of the GoRoute to be created by this configuration.
8080
final String? name;
8181

82-
late final Set<String> _pathParams = Set<String>.unmodifiable(_parsedPath
83-
.whereType<ParameterToken>()
84-
.map((ParameterToken e) => e.name));
85-
86-
late final List<Token> _parsedPath =
87-
List<Token>.unmodifiable(parse(_rawJoinedPath));
82+
late final Set<String> _pathParams =
83+
pathParametersFromPattern(_rawJoinedPath);
8884

8985
String get _rawJoinedPath {
9086
final List<String> pathSegments = <String>[];
@@ -103,22 +99,18 @@ class GoRouteConfig extends RouteBaseConfig {
10399
// construct path bits using parent bits
104100
// if there are any queryParam objects, add in the `queryParam` bits
105101
String get _locationArgs {
106-
final Iterable<String> pathItems = _parsedPath.map((Token e) {
107-
if (e is ParameterToken) {
102+
final Map<String, String> pathParameters = Map<String, String>.fromEntries(
103+
_pathParams.map((String pathParameter) {
108104
// Enum types are encoded using a map, so we need a nullability check
109105
// here to ensure it matches Uri.encodeComponent nullability
110-
final DartType? type = _field(e.name)?.returnType;
111-
return '\${Uri.encodeComponent(${_encodeFor(e.name)}${type?.isEnum ?? false ? '!' : ''})}';
112-
}
113-
if (e is PathToken) {
114-
return e.value;
115-
}
116-
throw UnsupportedError(
117-
'$likelyIssueMessage '
118-
'Token ($e) of type ${e.runtimeType} is not supported.',
119-
);
120-
});
121-
return "'${pathItems.join()}'";
106+
final DartType? type = _field(pathParameter)?.returnType;
107+
final String value =
108+
'\${Uri.encodeComponent(${_encodeFor(pathParameter)}${type?.isEnum ?? false ? '!' : ''})}';
109+
return MapEntry<String, String>(pathParameter, value);
110+
}),
111+
);
112+
final String location = patternToPath(_rawJoinedPath, pathParameters);
113+
return "'$location'";
122114
}
123115

124116
ParameterElement? get _extraParam => _ctor.parameters

packages/go_router_builder/pubspec.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: go_router_builder
22
description: >-
33
A builder that supports generated strongly-typed route helpers for
44
package:go_router
5-
version: 2.2.2
5+
version: 2.2.3
66
repository: https://github.com/flutter/packages/tree/main/packages/go_router_builder
77
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router_builder%22
88

@@ -17,7 +17,6 @@ dependencies:
1717
collection: ^1.14.0
1818
meta: ^1.7.0
1919
path: ^1.8.0
20-
path_to_regexp: ^0.4.0
2120
source_gen: ^1.0.0
2221
source_helper: ^1.3.0
2322

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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_builder/src/path_utils.dart';
6+
import 'package:test/test.dart';
7+
8+
void main() {
9+
group('pathParametersFromPattern', () {
10+
test('It should return the parameters of the path', () {
11+
expect(pathParametersFromPattern('/'), const <String>{});
12+
expect(pathParametersFromPattern('/user'), const <String>{});
13+
expect(pathParametersFromPattern('/user/:id'), const <String>{'id'});
14+
expect(pathParametersFromPattern('/user/:id/book'), const <String>{'id'});
15+
expect(
16+
pathParametersFromPattern('/user/:id/book/:bookId'),
17+
const <String>{'id', 'bookId'},
18+
);
19+
});
20+
});
21+
22+
group('patternToPath', () {
23+
test('It should replace the path parameters with their values', () {
24+
expect(patternToPath('/', const <String, String>{}), '/');
25+
expect(patternToPath('/user', const <String, String>{}), '/user');
26+
expect(
27+
patternToPath('/user/:id', const <String, String>{'id': 'user-id'}),
28+
'/user/user-id');
29+
expect(
30+
patternToPath(
31+
'/user/:id/book', const <String, String>{'id': 'user-id'}),
32+
'/user/user-id/book');
33+
expect(
34+
patternToPath('/user/:id/book/:bookId',
35+
const <String, String>{'id': 'user-id', 'bookId': 'book-id'}),
36+
'/user/user-id/book/book-id',
37+
);
38+
});
39+
});
40+
}

script/configs/allowed_unpinned_deps.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# cautious about adding to this list.
1414
- build_verify
1515
- google_maps
16-
- path_to_regexp
1716
- win32
1817

1918
## Allowed by default

0 commit comments

Comments
 (0)