Repository navigation
fix: escape dots when a brace range falls back to literal matching - #231
Open
Kumar44developer wants to merge 1 commit into
Open
Kumar44developer wants to merge 1 commit into
Kumar44developer wants to merge 1 commit into
Conversation
When a brace range cannot be expressed as a character class, expandRange falls back to matching the range as literal text. The fallback escaped each bound but joined them with a raw '..', so the dots were emitted as regex wildcards.
As a result '{01..03}', whose character class '[01-03]' is an invalid range, compiled to /^(?:01..03)$/ and matched '01ab03', '01__03' and even '01/x03', crossing a path separator. Escaping the dots makes the fallback match only the literal text it represents.
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.
Escape dots when a brace range falls back to literal matching
Problem
When a brace range can't be expressed as a character class,
expandRangefalls back to matching the range as literal text. That fallback escapes each bound but joins them with a raw'..':escapeRegexescapes., so the bounds are safe — but the two dots in the joiner are not, and they reach the generated regex as wildcards.{01..03}takes this path, because its character class[01-03]contains the invalid range1-0andnew RegExpthrows. The result is an overly permissive pattern:The last case is the most surprising one: the wildcards let the pattern match across a path separator, which a brace range should never do.
A realistic version:
Fix
Escape the dots in the fallback so it matches exactly the literal text it represents:
Ranges that can be expressed as a character class are untouched, since they never reach the fallback —
{a..c}still compiles to[a-c]and{1..3}to[1-3].Scope
This PR only fixes the escaping of the fallback output. It deliberately does not add zero-padded range expansion (
{01..03}→01 02 03), since the readme points tooptions.expandRangewith fill-range for that, and adding it here would change matching behaviour rather than fix a regex-escaping bug. Happy to follow up separately if you'd like that supported natively.Tests
Added a test to
test/braces.jscovering the literal match, the wildcard false-positives, and the path-separator case. Verified it fails on currentmaster:With the fix the full suite passes — 1999 passing (1998 before, plus the new test) — and
eslintis clean.