route-pattern: PartPattern.{parse,variants,toString}#10935
Merged
pcattori merged 1 commit intopedro/route-patternfrom Jan 12, 2026
Merged
route-pattern: PartPattern.{parse,variants,toString}#10935pcattori merged 1 commit intopedro/route-patternfrom
pcattori merged 1 commit intopedro/route-patternfrom
Conversation
This was referenced Jan 8, 2026
6ba1947 to
effd092
Compare
pcattori
commented
Jan 9, 2026
effd092 to
75c1cad
Compare
pcattori
added a commit
that referenced
this pull request
Jan 14, 2026
pcattori
added a commit
that referenced
this pull request
Jan 16, 2026
pcattori
added a commit
that referenced
this pull request
Jan 20, 2026
pcattori
added a commit
that referenced
this pull request
Jan 21, 2026
pcattori
added a commit
that referenced
this pull request
Jan 22, 2026
pcattori
added a commit
that referenced
this pull request
Jan 23, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Context
During the development of
route-pattern, the design for the Route Pattern DSL changed many times. This wasn't unexpected, but it did mean that the matching APIs (ArrayMatcher,TrieMatcher, etc.) and parsing algorithms always had to "catch up" to the newest DSL changes. Over time, this led to some bolted-on bits and some gaps in the matching/parsing.Additionally, the current
ArrayMatcherandTrieMatcheraren't guaranteed to return the same "best" match.ArrayMatcherjust bails as soon as it finds its first match andTrieMatcherdoes some ad-hoc scoring.The goal moving forward is to spec out the parsing/matching (via unit tests + docs) and implement a simple, consistent ranking metric for
ArrayMatcher,TrieMatcher, or any other matcher.Here's my plan:
PartPattern.{parse,variants,toString}(this PR)RoutePattern.{parse,join}TrieMatcher.{add,matchAll}(w/o ranking)TrieMatcher.{match, matchAll}ArrayMatcher.{add,match,matchAll}PartPatternAs a first step towards this goal, this PR implements a new design for the parts of a Route Pattern that are allowed to use the pattern DSL. Today, that includes the protocol, hostname, and pathname, but in the future its likely that the protocol will be simplified to just accept the known network schemes (
http,https,http(s),ws,wss,ws(s),ftp, ...).Knowing that we want to eventually use
PartPatterns withinRoutePatterns to power aTrieMatcher, this new implementation aims for a simpler flat AST:This AST representation has a couple benefits over the previous implementation:
Traversal is simpler and faster since its just a for-loop where you can decide to skip any optionals as you go. Specifically, variants (for construction trie branches) are trivial to implement.
Feature detection is simpler and faster. For example, does this part pattern have any params? Check
ast.paramNames.length > 0? Any optionals? Checkast.optionals.size > 0. Etc.Joining patterns is more efficient. Previously,
RoutePattern.joinreparsed the entire pathname on every invocation whereas with this AST, we can simple concatenate the twopathnames(adding a/text node between them as necessary) and quickly apply an offset to the second pattern'sparamNamesandoptionals.To be clear, the goal isn't necessary to be faster at parsing part patterns — that can come later with optimizations if needed — but to have a purpose-built structure for our DSL.