diff --git a/packages/go_router_builder/CHANGELOG.md b/packages/go_router_builder/CHANGELOG.md index 7e474dd5309..28743dd7c51 100644 --- a/packages/go_router_builder/CHANGELOG.md +++ b/packages/go_router_builder/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.3.0 + +* Removes `path_to_regexp` from the dependencies. + ## 2.2.2 * Bumps example go_router version and migrate example code. diff --git a/packages/go_router_builder/lib/src/route_config.dart b/packages/go_router_builder/lib/src/route_config.dart index c009b12c797..17f9d965e86 100644 --- a/packages/go_router_builder/lib/src/route_config.dart +++ b/packages/go_router_builder/lib/src/route_config.dart @@ -11,11 +11,11 @@ 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 'type_helpers.dart'; +import 'utils/path_token.dart'; /// Custom [Iterable] implementation with extra info. class InfoIterable extends IterableBase { @@ -84,7 +84,7 @@ class GoRouteConfig extends RouteBaseConfig { .map((ParameterToken e) => e.name)); late final List _parsedPath = - List.unmodifiable(parse(_rawJoinedPath)); + List.unmodifiable(Token.parse(_rawJoinedPath)); String get _rawJoinedPath { final List pathSegments = []; @@ -111,7 +111,7 @@ class GoRouteConfig extends RouteBaseConfig { return '\${Uri.encodeComponent(${_encodeFor(e.name)}${type?.isEnum ?? false ? '!' : ''})}'; } if (e is PathToken) { - return e.value; + return e.name; } throw UnsupportedError( '$likelyIssueMessage ' diff --git a/packages/go_router_builder/lib/src/utils/path_token.dart b/packages/go_router_builder/lib/src/utils/path_token.dart new file mode 100644 index 00000000000..df3345ea090 --- /dev/null +++ b/packages/go_router_builder/lib/src/utils/path_token.dart @@ -0,0 +1,61 @@ +// 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. + +/// The base type of all tokens produced by a path specification. +abstract class Token { + /// The name of the parameter. + String get name; + + /// Parses a [path] specification. + static List parse(String path) => _parse(path); +} + +/// Corresponds to a parameter of a path specification. +class ParameterToken implements Token { + /// Creates a parameter token for [name]. + ParameterToken(this.name); + + /// The parameter name. + @override + final String name; +} + +/// Corresponds to a non-parameterized section of a path specification. +class PathToken implements Token { + /// Creates a path token with [name]. + PathToken(this.name); + + /// A substring of the path specification. + @override + final String name; +} + +/// The regular expression used to extract parameters from a path specification. +/// +/// Capture groups: +/// 1. The parameter name. +/// 2. An optional pattern. +final RegExp _parameterRegExp = RegExp( + /* (1) */ r':(\w+)' + /* (2) */ r'(\((?:\\.|[^\\()])+\))?', +); + +/// Parses a [path] specification. +List _parse(String path) { + final Iterable matches = _parameterRegExp.allMatches(path); + final List tokens = []; + int start = 0; + for (final RegExpMatch match in matches) { + if (match.start > start) { + tokens.add(PathToken(path.substring(start, match.start))); + } + final String name = match[1]!; + tokens.add(ParameterToken(name)); + start = match.end; + } + if (start < path.length) { + tokens.add(PathToken(path.substring(start))); + } + return tokens; +} diff --git a/packages/go_router_builder/pubspec.yaml b/packages/go_router_builder/pubspec.yaml index 81b6d54b234..fb0153cd1d0 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.2 +version: 2.3.0 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