Skip to content

fix(text): treat authored replace targets as literal text - #394

Open
rahul05ranjan wants to merge 1 commit into
hypit-ai:mainfrom
rahul05ranjan:fix/replace-literal-substitution
Open

rahul05ranjan wants to merge 1 commit into
hypit-ai:mainfrom
rahul05ranjan:fix/replace-literal-substitution

Conversation

@rahul05ranjan

Copy link
Copy Markdown

What happens

The replace expression passes the authored to string as the replacement argument of String.prototype.replace/replaceAll (packages/text/src/program.ts). JavaScript treats $&, $$, $`` and $' in a plain replacement string as substitution patterns, so a target like "$& USD"` silently renders as the matched substring:

{ kind: "replace", input: { kind: "literal", value: "price: X" },
  replacements: [{ from: "X", to: "$& USD", mode: "all" }] }

renders price: X USD instead of the authored price: $& USD. $$ collapses to a single $; $ and $' splice in the text before/after the match. The corruption is silent becauseverifyTextTemplate` accepts the expression.

Fix

Pass a replacer function so the authored to is always inserted literally — no other change to the expression's semantics. from was already treated literally, so to now matches it.

Test

Adds a regression test to packages/text/test/text.test.ts pinning authored targets containing $&, $$, $ and $' across bothallandfirstmodes. Focused run of that file: 8 tests pass, including the new one; the new test fails on current main withprice: X $ USDwhereprice: $& $$ USD` is authored.

Fixes #393

String.prototype.replace/replaceAll interpret $&, $$, $` and $' in a plain replacement string as substitution patterns. The replace expression passed the authored `to` value straight through, so a target like "$& USD" silently rendered as the matched substring instead of the authored text. Pass a replacer function so `to` is always inserted literally, and pin the behavior with a regression test.

Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Text: the replace expression expands $&/$$/$'-patterns in the authored replacement string

1 participant