-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[go_router_builder] Removes path_to_regexp from the dependencies
#4524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
auto-submit
merged 11 commits into
flutter:main
from
ValentinVignal:go-router-builder/remove-path-to-regexp-from-dependencies-2
Jul 21, 2023
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c9487b3
chore: Copy path_utils.dart from go_router
ValentinVignal 0cc50db
revamp: Remove unused methods
ValentinVignal 205da04
refactor: Simplify path utils methods to only keep what is used
ValentinVignal e48e2af
chore: Remove path_to_regexp dependency
ValentinVignal 31a3785
refactor: Update the code to use path_utils
ValentinVignal f898d88
chore: Increase package version
ValentinVignal 57f71a9
chore: Only update patch version
ValentinVignal 5969d6f
ci: Remove path_to_regexp from allowed unpinned deps
ValentinVignal b85acdb
Merge branch 'main' into go-router-builder/remove-path-to-regexp-from…
stuartmorgan-g d8391de
tests: Add path_utils unit test
ValentinVignal 79e3bb6
Merge remote-tracking branch 'upstream/main' into go-router-builder/r…
ValentinVignal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
chore: Copy path_utils.dart from go_router
- Loading branch information
commit c9487b301293bedc308e1b4561055db1f9ec6e61
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| // 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 'route.dart'; | ||
|
|
||
| final RegExp _parameterRegExp = RegExp(r':(\w+)(\((?:\\.|[^\\()])+\))?'); | ||
|
|
||
| /// Converts a [pattern] such as `/user/:id` into [RegExp]. | ||
| /// | ||
| /// The path parameters can be specified by prefixing them with `:`. The | ||
| /// `parameters` are used for storing path parameter names. | ||
| /// | ||
| /// | ||
| /// For example: | ||
| /// | ||
| /// `pattern` = `/user/:id/book/:bookId` | ||
| /// | ||
| /// The `parameters` would contain `['id', 'bookId']` as a result of calling | ||
| /// this method. | ||
| /// | ||
| /// To extract the path parameter values from a [RegExpMatch], pass the | ||
| /// [RegExpMatch] into [extractPathParameters] with the `parameters` that are | ||
| /// used for generating the [RegExp]. | ||
| RegExp patternToRegExp(String pattern, List<String> parameters) { | ||
| final StringBuffer buffer = StringBuffer('^'); | ||
| int start = 0; | ||
| for (final RegExpMatch match in _parameterRegExp.allMatches(pattern)) { | ||
| if (match.start > start) { | ||
| buffer.write(RegExp.escape(pattern.substring(start, match.start))); | ||
| } | ||
| final String name = match[1]!; | ||
| final String? optionalPattern = match[2]; | ||
| final String regex = optionalPattern != null | ||
| ? _escapeGroup(optionalPattern, name) | ||
| : '(?<$name>[^/]+)'; | ||
| buffer.write(regex); | ||
| parameters.add(name); | ||
| start = match.end; | ||
| } | ||
|
|
||
| if (start < pattern.length) { | ||
| buffer.write(RegExp.escape(pattern.substring(start))); | ||
| } | ||
|
|
||
| if (!pattern.endsWith('/')) { | ||
| buffer.write(r'(?=/|$)'); | ||
| } | ||
| return RegExp(buffer.toString(), caseSensitive: false); | ||
| } | ||
|
|
||
| String _escapeGroup(String group, [String? name]) { | ||
| final String escapedGroup = group.replaceFirstMapped( | ||
| RegExp(r'[:=!]'), (Match match) => '\\${match[0]}'); | ||
| if (name != null) { | ||
| return '(?<$name>$escapedGroup)'; | ||
| } | ||
| return escapedGroup; | ||
| } | ||
|
|
||
| /// Reconstructs the full path from a [pattern] and path parameters. | ||
| /// | ||
| /// This is useful for restoring the original path from a [RegExpMatch]. | ||
| /// | ||
| /// For example, A path matched a [RegExp] returned from [patternToRegExp] and | ||
| /// produced a [RegExpMatch]. To reconstruct the path from the match, one | ||
| /// can follow these steps: | ||
| /// | ||
| /// 1. Get the `pathParameters` by calling [extractPathParameters] with the | ||
| /// [RegExpMatch] and the parameters used for generating the [RegExp]. | ||
| /// 2. Call [patternToPath] with the `pathParameters` from the first step and | ||
| /// the original `pattern` used for generating the [RegExp]. | ||
| 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(); | ||
| } | ||
|
|
||
| /// Extracts arguments from the `match` and maps them by parameter name. | ||
| /// | ||
| /// The [parameters] should originate from the call to [patternToRegExp] that | ||
| /// creates the [RegExp]. | ||
| Map<String, String> extractPathParameters( | ||
| List<String> parameters, RegExpMatch match) { | ||
| return <String, String>{ | ||
| for (int i = 0; i < parameters.length; ++i) | ||
| parameters[i]: match.namedGroup(parameters[i])! | ||
| }; | ||
| } | ||
|
|
||
| /// Concatenates two paths. | ||
| /// | ||
| /// e.g: pathA = /a, pathB = c/d, concatenatePaths(pathA, pathB) = /a/c/d. | ||
| String concatenatePaths(String parentPath, String childPath) { | ||
| // at the root, just return the path | ||
| if (parentPath.isEmpty) { | ||
| assert(childPath.startsWith('/')); | ||
| assert(childPath == '/' || !childPath.endsWith('/')); | ||
| return childPath; | ||
| } | ||
|
|
||
| // not at the root, so append the parent path | ||
| assert(childPath.isNotEmpty); | ||
| assert(!childPath.startsWith('/')); | ||
| assert(!childPath.endsWith('/')); | ||
| return '${parentPath == '/' ? '' : parentPath}/$childPath'; | ||
| } | ||
|
|
||
| /// Normalizes the location string. | ||
| String canonicalUri(String loc) { | ||
| String canon = Uri.parse(loc).toString(); | ||
| canon = canon.endsWith('?') ? canon.substring(0, canon.length - 1) : canon; | ||
|
|
||
| // remove trailing slash except for when you shouldn't, e.g. | ||
| // /profile/ => /profile | ||
| // / => / | ||
| // /login?from=/ => login?from=/ | ||
| canon = canon.endsWith('/') && canon != '/' && !canon.contains('?') | ||
| ? canon.substring(0, canon.length - 1) | ||
| : canon; | ||
|
|
||
| // /login/?from=/ => /login?from=/ | ||
| // /?from=/ => /?from=/ | ||
| canon = canon.replaceFirst('/?', '?', 1); | ||
|
|
||
| return canon; | ||
| } | ||
|
|
||
| /// Builds an absolute path for the provided route. | ||
| String? fullPathForRoute( | ||
| RouteBase targetRoute, String parentFullpath, List<RouteBase> routes) { | ||
| for (final RouteBase route in routes) { | ||
| final String fullPath = (route is GoRoute) | ||
| ? concatenatePaths(parentFullpath, route.path) | ||
| : parentFullpath; | ||
|
|
||
| if (route == targetRoute) { | ||
| return fullPath; | ||
| } else { | ||
| final String? subRoutePath = | ||
| fullPathForRoute(targetRoute, fullPath, route.routes); | ||
| if (subRoutePath != null) { | ||
| return subRoutePath; | ||
| } | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file in this commit (chore: Copy path_utils.dart from go_router) is a literal copy past from
go_router'spath_utils.dartfile https://github.com/flutter/packages/blob/main/packages/go_router/lib/src/path_utils.dart