-
Notifications
You must be signed in to change notification settings - Fork 2
feat(cli): workspaces #487
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
base: main
Are you sure you want to change the base?
Conversation
| const regexPattern = pattern | ||
| .replace(/\./g, "\\.") |
Check failure
Code scanning / CodeQL
Incomplete string escaping or encoding High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI about 8 hours ago
In general, when converting glob patterns to regular expressions, every regex metacharacter (including the backslash itself) must be escaped before you introduce your own special handling for * and **. Doing this manually with chained .replace calls is fragile; using a single replacement that escapes all regex-special characters is much safer.
The best minimal fix here is to first escape all regex metacharacters in the pattern, then translate the escaped glob wildcards to their regex equivalents. That means:
- Take the raw
pattern. - Escape all regex metacharacters (
[\^$.*+?()|{}]and\). - Replace the escaped glob sequences for
**and*with placeholders / appropriate regex:- Convert the escaped
**to a placeholder (e.g.{{GLOBSTAR}}) and then to.*. - Convert the escaped
*to[^/]*.
- Convert the escaped
- Build the final
RegExpfrom this fully-escaped, transformed pattern.
Concretely in packages/core/src/commands/assign.ts, inside matchFiles, we should:
- Introduce a helper that escapes regex metacharacters, or inline the logic in the
regexPatterncomputation. - Change the current chain:
to:
const regexPattern = pattern .replace(/\./g, "\\.") .replace(/\*\*/g, "{{GLOBSTAR}}") .replace(/\*/g, "[^/]*") .replace(/{{GLOBSTAR}}/g, ".*");
const escapedPattern = pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); const regexPattern = escapedPattern .replace(/\\\*\\\*/g, "{{GLOBSTAR}}") .replace(/\\\*/g, "[^/]*") .replace(/{{GLOBSTAR}}/g, ".*");
- Keep the rest of the function unchanged.
No new imports are needed; we rely only on the built-in RegExp and String.prototype.replace.
-
Copy modified lines R25-R30
| @@ -22,10 +22,12 @@ | ||
|
|
||
| for (const pattern of patterns) { | ||
| // Convert glob pattern to regex | ||
| const regexPattern = pattern | ||
| .replace(/\./g, "\\.") | ||
| .replace(/\*\*/g, "{{GLOBSTAR}}") | ||
| .replace(/\*/g, "[^/]*") | ||
| // First escape all regex metacharacters, including backslashes, | ||
| // then translate glob wildcards (** and *) into regex equivalents. | ||
| const escapedPattern = pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | ||
| const regexPattern = escapedPattern | ||
| .replace(/\\\*\\\*/g, "{{GLOBSTAR}}") | ||
| .replace(/\\\*/g, "[^/]*") | ||
| .replace(/{{GLOBSTAR}}/g, ".*"); | ||
|
|
||
| const regex = new RegExp(`^${regexPattern}$`); |
wip