-
-
Notifications
You must be signed in to change notification settings - Fork 430
feat: support parameter patterns #362
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
Open
Kakulukian
wants to merge
3
commits into
pillarjs:master
Choose a base branch
from
Kakulukian:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ const NOOP_VALUE = (value: string) => value; | |
const ID_START = /^[$_\p{ID_Start}]$/u; | ||
const ID_CONTINUE = /^[$\u200c\u200d\p{ID_Continue}]$/u; | ||
const DEBUG_URL = "https://git.new/pathToRegexpError"; | ||
const INVALID_PATTERN_CHARS = "^$.+*?[]{}\\^"; | ||
|
||
/** | ||
* Encode a string into another string. | ||
|
@@ -63,6 +64,7 @@ type TokenType = | |
| "}" | ||
| "WILDCARD" | ||
| "PARAM" | ||
| "PATTERN" | ||
| "CHAR" | ||
| "ESCAPED" | ||
| "END" | ||
|
@@ -89,7 +91,7 @@ const SIMPLE_TOKENS: Record<string, TokenType> = { | |
"{": "{", | ||
"}": "}", | ||
// Reserved. | ||
"(": "(", | ||
// "(": "(", | ||
")": ")", | ||
"[": "[", | ||
"]": "]", | ||
|
@@ -156,6 +158,45 @@ function* lexer(str: string): Generator<LexToken, LexToken> { | |
return value; | ||
} | ||
|
||
function pattern() { | ||
const pos = i++; | ||
let depth = 1; | ||
let pattern = ""; | ||
|
||
while (i < chars.length && depth > 0) { | ||
const char = chars[i]; | ||
|
||
if (INVALID_PATTERN_CHARS.includes(char)) { | ||
throw new TypeError( | ||
`Only "|" is allowed as a special character in patterns at ${i}: ${DEBUG_URL}`, | ||
); | ||
} | ||
|
||
if (char === ")") { | ||
depth--; | ||
if (depth === 0) { | ||
i++; | ||
break; | ||
} | ||
} else if (char === "(") { | ||
depth++; | ||
} | ||
|
||
pattern += char; | ||
i++; | ||
} | ||
|
||
if (depth) { | ||
throw new TypeError(`Unbalanced pattern at ${pos}: ${DEBUG_URL}`); | ||
} | ||
|
||
if (!pattern) { | ||
throw new TypeError(`Missing pattern at ${pos}: ${DEBUG_URL}`); | ||
} | ||
|
||
return pattern; | ||
} | ||
|
||
while (i < chars.length) { | ||
const value = chars[i]; | ||
const type = SIMPLE_TOKENS[value]; | ||
|
@@ -167,6 +208,9 @@ function* lexer(str: string): Generator<LexToken, LexToken> { | |
} else if (value === ":") { | ||
const value = name(); | ||
yield { type: "PARAM", index: i, value }; | ||
} else if (value === "(") { | ||
const value = pattern(); | ||
yield { type: "PATTERN", index: i, value }; | ||
} else if (value === "*") { | ||
const value = name(); | ||
yield { type: "WILDCARD", index: i, value }; | ||
|
@@ -231,6 +275,7 @@ export interface Text { | |
export interface Parameter { | ||
type: "param"; | ||
name: string; | ||
pattern?: string; | ||
} | ||
|
||
/** | ||
|
@@ -287,9 +332,11 @@ export function parse(str: string, options: ParseOptions = {}): TokenData { | |
|
||
const param = it.tryConsume("PARAM"); | ||
if (param) { | ||
const pattern = it.tryConsume("PATTERN"); | ||
Kakulukian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tokens.push({ | ||
type: "param", | ||
name: param, | ||
pattern, | ||
}); | ||
continue; | ||
} | ||
|
@@ -579,7 +626,14 @@ function toRegExp(tokens: Flattened[], delimiter: string, keys: Keys) { | |
} | ||
|
||
if (token.type === "param") { | ||
result += `(${negate(delimiter, isSafeSegmentParam ? "" : backtrack)}+)`; | ||
if (token.pattern) { | ||
result += `(${token.pattern})`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should do the validation of the pattern here instead. Create a new function for validation so it can be expanded over time without refactoring inside this existing code. |
||
} else { | ||
result += `(${negate( | ||
delimiter, | ||
isSafeSegmentParam ? "" : backtrack, | ||
)}+)`; | ||
} | ||
} else { | ||
result += `([\\s\\S]+)`; | ||
} | ||
|
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.
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.
Unfortunately it’s more complex than this since you can be escaping a meta character to make it a valid character to use, so we actually need to iterate over the string and do some light parsing.
I’d also recommend leaving the parsing out of this section, as it should be part of the logic translating it into a regex as the library does accept raw tokens.