[go_router_builder] Removes path_to_regexp from the dependencies (#4524)

Fixes https://github.com/flutter/flutter/issues/130817
Relates to https://github.com/flutter/flutter/issues/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].*
This commit is contained in:
Valentin Vignal
2023-07-22 00:07:51 +08:00
committed by GitHub
parent 203ad04a71
commit 1af4d1a40a
6 changed files with 104 additions and 24 deletions

View File

@ -1,3 +1,7 @@
## 2.2.3
* Removes `path_to_regexp` from the dependencies.
## 2.2.2
* Bumps example go_router version and migrate example code.

View File

@ -0,0 +1,46 @@
// 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.
final RegExp _parameterRegExp = RegExp(r':(\w+)(\((?:\\.|[^\\()])+\))?');
/// Extracts the path parameters from a [pattern] such as `/user/:id`.
///
/// The path parameters can be specified by prefixing them with `:`.
///
/// For example:
///
/// ```dart
/// final pattern = '/user/:id/book/:bookId';
/// final pathParameters = pathParametersFromPattern(pattern); // {'id', 'bookId'}
/// ```
Set<String> pathParametersFromPattern(String pattern) => <String>{
for (final RegExpMatch match in _parameterRegExp.allMatches(pattern))
match[1]!,
};
/// Reconstructs the full path from a [pattern] and path parameters.
///
/// For example:
///
/// ```dart
/// final pattern = '/family/:id';
/// final path = patternToPath(pattern, {'id': 'family-id'}); // '/family/family-id'
/// ```
String patternToPath(String pattern, Map<String, String> pathParameters) {
final StringBuffer buffer = StringBuffer();
int start = 0;
for (final RegExpMatch match in _parameterRegExp.allMatches(pattern)) {
if (match.start > start) {
buffer.write(pattern.substring(start, match.start));
}
final String name = match[1]!;
buffer.write(pathParameters[name]);
start = match.end;
}
if (start < pattern.length) {
buffer.write(pattern.substring(start));
}
return buffer.toString();
}

View File

@ -11,10 +11,10 @@ import 'package:analyzer/dart/element/type.dart';
import 'package:collection/collection.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as p;
import 'package:path_to_regexp/path_to_regexp.dart';
import 'package:source_gen/source_gen.dart';
import 'package:source_helper/source_helper.dart';
import 'path_utils.dart';
import 'type_helpers.dart';
/// Custom [Iterable] implementation with extra info.
@ -79,12 +79,8 @@ class GoRouteConfig extends RouteBaseConfig {
/// The name of the GoRoute to be created by this configuration.
final String? name;
late final Set<String> _pathParams = Set<String>.unmodifiable(_parsedPath
.whereType<ParameterToken>()
.map((ParameterToken e) => e.name));
late final List<Token> _parsedPath =
List<Token>.unmodifiable(parse(_rawJoinedPath));
late final Set<String> _pathParams =
pathParametersFromPattern(_rawJoinedPath);
String get _rawJoinedPath {
final List<String> pathSegments = <String>[];
@ -103,22 +99,18 @@ class GoRouteConfig extends RouteBaseConfig {
// construct path bits using parent bits
// if there are any queryParam objects, add in the `queryParam` bits
String get _locationArgs {
final Iterable<String> pathItems = _parsedPath.map((Token e) {
if (e is ParameterToken) {
final Map<String, String> 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(e.name)?.returnType;
return '\${Uri.encodeComponent(${_encodeFor(e.name)}${type?.isEnum ?? false ? '!' : ''})}';
}
if (e is PathToken) {
return e.value;
}
throw UnsupportedError(
'$likelyIssueMessage '
'Token ($e) of type ${e.runtimeType} is not supported.',
);
});
return "'${pathItems.join()}'";
final DartType? type = _field(pathParameter)?.returnType;
final String value =
'\${Uri.encodeComponent(${_encodeFor(pathParameter)}${type?.isEnum ?? false ? '!' : ''})}';
return MapEntry<String, String>(pathParameter, value);
}),
);
final String location = patternToPath(_rawJoinedPath, pathParameters);
return "'$location'";
}
ParameterElement? get _extraParam => _ctor.parameters

View File

@ -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.2
version: 2.2.3
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
@ -17,7 +17,6 @@ dependencies:
collection: ^1.14.0
meta: ^1.7.0
path: ^1.8.0
path_to_regexp: ^0.4.0
source_gen: ^1.0.0
source_helper: ^1.3.0

View File

@ -0,0 +1,40 @@
// 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_builder/src/path_utils.dart';
import 'package:test/test.dart';
void main() {
group('pathParametersFromPattern', () {
test('It should return the parameters of the path', () {
expect(pathParametersFromPattern('/'), const <String>{});
expect(pathParametersFromPattern('/user'), const <String>{});
expect(pathParametersFromPattern('/user/:id'), const <String>{'id'});
expect(pathParametersFromPattern('/user/:id/book'), const <String>{'id'});
expect(
pathParametersFromPattern('/user/:id/book/:bookId'),
const <String>{'id', 'bookId'},
);
});
});
group('patternToPath', () {
test('It should replace the path parameters with their values', () {
expect(patternToPath('/', const <String, String>{}), '/');
expect(patternToPath('/user', const <String, String>{}), '/user');
expect(
patternToPath('/user/:id', const <String, String>{'id': 'user-id'}),
'/user/user-id');
expect(
patternToPath(
'/user/:id/book', const <String, String>{'id': 'user-id'}),
'/user/user-id/book');
expect(
patternToPath('/user/:id/book/:bookId',
const <String, String>{'id': 'user-id', 'bookId': 'book-id'}),
'/user/user-id/book/book-id',
);
});
});
}

View File

@ -13,7 +13,6 @@
# cautious about adding to this list.
- build_verify
- google_maps
- path_to_regexp
- win32
## Allowed by default