fix(define): only match escaped dots in escapedDotRE so $-prefixed define keys are replaced - #23249
Open
contactjawad wants to merge 1 commit into
Open
fix(define): only match escaped dots in escapedDotRE so $-prefixed define keys are replaced#23249contactjawad wants to merge 1 commit into
contactjawad wants to merge 1 commit into
Conversation
…keys are replaced escapedDotRE was /(?<!\\)\\./g, whose `.` is unescaped, so it matched a backslash followed by any character. For a define key like `$FOO`, escapeRegex produces `\$FOO`, and the replaceAll then rewrote `\$` to `\??\.`, producing the pattern `\??\.FOO`, which never matches `$FOO`. The `pattern.test(code)` pre-check was therefore false and the define was silently skipped. Escape the dot so only an escaped literal dot is rewritten. Dotted keys like `import.meta.env` are unaffected (both regexes yield `foo\??\.bar`), while escaped metacharacters such as `\$` are now left intact.
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.
Description
The
defineplugin silently drops define keys that contain a$(e.g.define: { $FOO: '"bar"' }) — the transform leaves the source unchanged instead of replacing$FOO.Root cause:
escapedDotRE = /(?<!\\)\\./gis meant to rewrite an escaped dot (\.) inside an already-escapeRegex-escaped key into\??\.(so it also matches optional chaining,?.). But the.in that regex is unescaped, so it actually matches a backslash followed by any character.escapeRegex('$FOO')produces\$FOO; thereplaceAllthen rewrites\$→\??\., producing the pattern\??\.FOO, which never matches$FOO. Thepattern.test(code)pre-check is then false and the replacement is skipped.Fix: escape the dot (
/(?<!\\)\\\./g) so only an escaped literal dot is rewritten. Dotted keys likeimport.meta.envare unaffected (both regexes yieldfoo\??\.bar), while escaped metacharacters such as\$are left intact.Added a regression test (
replaces define keys containing $) that fails onmain(the transform returnsundefined) and passes with this change.