fix: only pass the groups object to a replacer when the pattern has named groups - #342
Open
theRizwan wants to merge 1 commit into
Open
fix: only pass the groups object to a replacer when the pattern has named groups#342theRizwan wants to merge 1 commit into
theRizwan wants to merge 1 commit into
Conversation
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.
_replaceRegexppassesmatch.groupsas the trailing argument to a replacer function on every call:String.prototype.replaceonly passes that argument when the pattern actually contains named capture groups. When it does not,match.groupsisundefinedand magic-string hands the replacer one extra argument that the reference implementation never sends.Replacers written against the documented signature are affected whenever they read from the end of the argument list rather than a fixed position, which is the usual way to reach the offset or the source string from a variadic replacer:
Replacers with a fixed arity are unaffected, since the extra argument is simply dropped, which is why the existing
replace function offsettest passes either way.Fix
Pass the groups object only when the match has one, matching the documented behaviour the source already links to. Named groups keep working exactly as before.
Tests
Two tests, both differential against
String.prototype.replaceon the same input, one with a named group and one without. The no-named-group test fails onmasterwith the extraundefinedand passes with the fix; the named-group test guards against over-correcting.pnpm run lint,pnpm run typecheckandpnpm test(264 tests) all pass locally.This is the remaining
String.prototype.replacedivergence noted in #340, split out as its own change; the two do not overlap in the diff.