fix(generator): escape leading caret in non-negative character class#280
Open
greymoth-jp wants to merge 1 commit into
Open
fix(generator): escape leading caret in non-negative character class#280greymoth-jp wants to merge 1 commit into
greymoth-jp wants to merge 1 commit into
Conversation
A non-negative character class whose first element is a literal `^` was generated as a bare `[^...]`, which parses back as a negated class. This surfaces through the optimizer: charClassClassrangesMerge sorts class members by code point, so `/[a^]/` becomes `/[^a]/` (`^` is U+005E, before `a`), inverting the regex. Calling generate() on a hand-built or otherwise reordered AST hits the same path. Escape a leading literal `^` when the class is not negative.
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.
regexp-tree.optimize('/[a^]/')returns/[^a]/, which inverts the regex:[a^]matchesaor^, while[^a]matches everything excepta.The optimizer's
charClassClassrangesMergesorts character-class members by code point, and^(U+005E) sorts beforea(U+0061), so the class is reordered to put^first. The generator'sCharacterClasshandler emits the members verbatim, so the reordered class is written as[^a], and a leading^in a class is parsed back as negation.It is more visible when the reordered class is short enough for the optimizer to keep:
The same path is reachable through the public
generate()API: building or transforming an AST so that a non-negative class starts with a literal^produces an inverted regex today.The fix is in the generator: when a character class is not negative and its first member is an unescaped literal
^, escape it.[a^]then round-trips correctly, andoptimize('/[a^]/')returns/[a^]/again. Genuine negative classes and an already-escaped[\^a]are unaffected.Tests added for the generator and for the
char-class-classranges-mergetransform. The fullsrcsuite passes.