Skip to content

Commit 5bae547

Browse files
authored
fix(replace)!: do not escape delimiters (#1088)
* fix(replace)!: do not escape delimiters BREAKING CHANGES: user is now responsible for escaping delimiters * docs(replace): document delimiter escaping
1 parent 45c6d80 commit 5bae547

File tree

7 files changed

+41
-8
lines changed

7 files changed

+41
-8
lines changed

packages/replace/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ In addition to the properties and values specified for replacement, users may al
6161
### `delimiters`
6262

6363
Type: `Array[String, String]`<br>
64-
Default: `['\b', '\b(?!\.)']`
64+
Default: `['\\b', '\\b(?!\\.)']`
6565

6666
Specifies the boundaries around which strings will be replaced. By default, delimiters are [word boundaries](https://www.regular-expressions.info/wordboundaries.html) and also prevent replacements of instances with nested access. See [Word Boundaries](#word-boundaries) below for more information.
6767
For example, if you pass `typeof window` in `values` to-be-replaced, then you could expect the following scenarios:
@@ -70,6 +70,8 @@ For example, if you pass `typeof window` in `values` to-be-replaced, then you co
7070
- `typeof window.document` **will not** be replaced due to `(?!\.)` boundary
7171
- `typeof windowSmth` **will not** be replaced due to a `\b` boundary
7272

73+
Delimiters will be used to build a `Regexp`. To match special characters (any of `.*+?^${}()|[]\`), be sure to [escape](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping) them.
74+
7375
### `objectGuards`
7476

7577
Type: `Boolean`<br>

packages/replace/src/index.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,16 @@ function expandTypeofReplacements(replacements) {
6868

6969
export default function replace(options = {}) {
7070
const filter = createFilter(options.include, options.exclude);
71-
const { delimiters, preventAssignment, objectGuards } = options;
71+
const { delimiters = ['\\b', '\\b(?!\\.)'], preventAssignment, objectGuards } = options;
7272
const replacements = getReplacements(options);
7373
if (objectGuards) expandTypeofReplacements(replacements);
7474
const functionValues = mapToFunctions(replacements);
7575
const keys = Object.keys(functionValues).sort(longest).map(escape);
7676
const lookahead = preventAssignment ? '(?!\\s*=[^=])' : '';
77-
const pattern = delimiters
78-
? new RegExp(
79-
`${escape(delimiters[0])}(${keys.join('|')})${escape(delimiters[1])}${lookahead}`,
80-
'g'
81-
)
82-
: new RegExp(`\\b(${keys.join('|')})\\b(?!\\.)${lookahead}`, 'g');
77+
const pattern = new RegExp(
78+
`${delimiters[0]}(${keys.join('|')})${delimiters[1]}${lookahead}`,
79+
'g'
80+
);
8381

8482
return {
8583
name: 'replace',
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
description: 'allows delimiters with special characters',
3+
options: {
4+
special: 'replaced',
5+
delimiters: ['\\b', '\\b']
6+
}
7+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
console.log(`
2+
special
3+
(special)
4+
specially
5+
especial
6+
special.doSomething()
7+
`);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
console.log(`
2+
replaced
3+
(replaced)
4+
specially
5+
especial
6+
replaced.doSomething()
7+
`);

packages/replace/test/snapshots/form.js.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,15 @@ Generated by [AVA](https://avajs.dev).
7474
`const one = 1; // eslint-disable-line␊
7575
7676
console.log(one);`
77+
78+
## special-delimiters: allows delimiters with special characters
79+
80+
> Snapshot 1
81+
82+
`console.log(`␊
83+
replaced␊
84+
(replaced)␊
85+
specially␊
86+
especial␊
87+
replaced.doSomething()␊
88+
`);`
62 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)