Bug or at least unexpected side effect of replaceAll

Open
#176 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
javascript
Domain
tooling

Research direction

Start by locating the PureScript strings implementation of replaceAll and its JavaScript replace entry point, then inspect how replacement strings are passed through. Verify the suggested escaping behavior with a regression case involving $' and other dollar-prefixed replacement text; done means replacement strings remain literal.

Written by the indexing model from the issue text.

Description

In Javascript, the replace function when the first parameter is a Regex, there are special character strings for replacements, e.g. $'.

The problem is that the PureScript replaceAll relies on the JavaScript replace using a Regex with the global flag.

This means that replacement strings are simple string replacements, but instead are interpreted.

Suggested fix:

export const replaceAll = p => r => s => s.replace(new RegExp(p.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&"), "g"), r.replace(/\$/g, "$$$$")); // eslint-disable-line no-useless-escape

We're replacing the $ with $$ globally in the replacement string.

Dominant language
PureScript
Stars
57
Forks
76
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from purescript/purescript-strings

All issues in purescript/purescript-strings

Similar issues

More DevTools issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.