fix(minifier): validate RegExp patterns before marking as pure#18125
fix(minifier): validate RegExp patterns before marking as pure#18125graphite-app[bot] merged 1 commit intomainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where the minifier incorrectly removed invalid RegExp patterns that should throw SyntaxError at runtime. The fix uses the oxc_regular_expression parser to validate patterns and flags at compile time, ensuring that only valid RegExp constructors are marked as pure and can be safely removed.
Changes:
- Added
is_valid_regexpfunction that validates RegExp patterns and flags using the regex parser - Updated minifier's normalize pass to use validation for RegExp constructor calls
- Updated tests to reflect correct behavior for valid/invalid patterns
Reviewed changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| crates/oxc_ecmascript/src/side_effects/expressions.rs | Implements is_valid_regexp validation function using the regex parser |
| crates/oxc_ecmascript/src/side_effects/mod.rs | Exports the new is_valid_regexp function |
| crates/oxc_minifier/src/peephole/normalize.rs | Integrates RegExp validation in the normalize pass for NewExpression |
| crates/oxc_ecmascript/Cargo.toml | Adds oxc_regular_expression dependency |
| Cargo.lock | Updates lock file with new dependency |
| crates/oxc_minifier/tests/peephole/substitute_alternate_syntax.rs | Updates tests with correct behavior for valid/invalid RegExp patterns |
| crates/oxc_minifier/tests/peephole/normalize.rs | Updates tests to handle non-literal RegExp arguments correctly |
| crates/oxc_minifier/tests/ecmascript/may_have_side_effects.rs | Adds tests for invalid patterns and flags |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
b0db38f to
2e4911a
Compare
Merging this PR will not alter performance
Comparing Footnotes
|
2e4911a to
8563299
Compare
Merge activity
|
## Summary
Invalid RegExp patterns like `RegExp("[")` or invalid flags like `RegExp("a", "xyz")` throw `SyntaxError` at runtime, but the minifier was incorrectly removing them.
This change uses `oxc_regular_expression` to validate patterns at compile time:
- Valid patterns are marked as pure and can be removed when unused
- Invalid patterns or non-literal arguments are kept (preserving runtime errors)
### Behavior
| Input | Valid? | Removed? |
|-------|--------|----------|
| `RegExp()` | ✅ | ✅ |
| `RegExp('a', 'g')` | ✅ | ✅ |
| `RegExp(/foo/)` | ✅ | ✅ |
| `RegExp('[')` | ❌ | ❌ (throws SyntaxError) |
| `RegExp('a', 'xyz')` | ❌ | ❌ (throws SyntaxError) |
| `RegExp(variable)` | ? | ❌ (can't determine) |
Fixes #18050
## Test plan
- [x] Updated existing tests to reflect correct behavior
- [x] Added tests for invalid patterns and flags
- [x] `cargo test -p oxc_minifier` passes
- [x] `cargo test -p oxc_ecmascript` passes
🤖 Generated with [Claude Code](https://claude.com/claude-code)
8563299 to
64e73a5
Compare
## Summary
Invalid RegExp patterns like `RegExp("[")` or invalid flags like `RegExp("a", "xyz")` throw `SyntaxError` at runtime, but the minifier was incorrectly removing them.
This change uses `oxc_regular_expression` to validate patterns at compile time:
- Valid patterns are marked as pure and can be removed when unused
- Invalid patterns or non-literal arguments are kept (preserving runtime errors)
### Behavior
| Input | Valid? | Removed? |
|-------|--------|----------|
| `RegExp()` | ✅ | ✅ |
| `RegExp('a', 'g')` | ✅ | ✅ |
| `RegExp(/foo/)` | ✅ | ✅ |
| `RegExp('[')` | ❌ | ❌ (throws SyntaxError) |
| `RegExp('a', 'xyz')` | ❌ | ❌ (throws SyntaxError) |
| `RegExp(variable)` | ? | ❌ (can't determine) |
Fixes #18050
## Test plan
- [x] Updated existing tests to reflect correct behavior
- [x] Added tests for invalid patterns and flags
- [x] `cargo test -p oxc_minifier` passes
- [x] `cargo test -p oxc_ecmascript` passes
🤖 Generated with [Claude Code](https://claude.com/claude-code)
64e73a5 to
38e4b53
Compare
Summary
Invalid RegExp patterns like
RegExp("[")or invalid flags likeRegExp("a", "xyz")throwSyntaxErrorat runtime, but the minifier was incorrectly removing them.This change uses
oxc_regular_expressionto validate patterns at compile time:Behavior
RegExp()RegExp('a', 'g')RegExp(/foo/)RegExp('[')RegExp('a', 'xyz')RegExp(variable)Fixes #18050
Test plan
cargo test -p oxc_minifierpassescargo test -p oxc_ecmascriptpasses🤖 Generated with Claude Code