Skip to content

Commit 93cf0a7

Browse files
o-alexandrovshellscape
authored andcommitted
fix(replace)!: issues with nested objects replacements (#903)
* fix(replace): issues with nested objects replacements * fix(replace): update tests & snapshots
1 parent 6a32b4d commit 93cf0a7

File tree

15 files changed

+33
-132
lines changed

15 files changed

+33
-132
lines changed

.github/workflows/release.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ on:
88
jobs:
99
publish:
1010
# let's ignore release commits, otherwise it'll try to run twice
11-
if: !startsWith(github.event.head_commit.message , 'chore(release):')
11+
if: |
12+
!startsWith(github.event.head_commit.message , 'chore(release):')
1213
1314
runs-on: ubuntu-latest
1415

packages/replace/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,14 @@ 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

66-
Specifies the boundaries around which strings will be replaced. By default, delimiters are [word boundaries](https://www.regular-expressions.info/wordboundaries.html). See [Word Boundaries](#word-boundaries) below for more information.
66+
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.
67+
For example, if you pass `typeof window` in `values` to-be-replaced, then you could expect the following scenarios:
68+
69+
- `typeof window` **will** be replaced
70+
- `typeof window.document` **will not** be replaced due to `(?!\.)` boundary
71+
- `typeof windowSmth` **will not** be replaced due to a `\b` boundary
6772

6873
### `preventAssignment`
6974

packages/replace/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default function replace(options = {}) {
4646
`${escape(delimiters[0])}(${keys.join('|')})${escape(delimiters[1])}${lookahead}`,
4747
'g'
4848
)
49-
: new RegExp(`\\b(${keys.join('|')})\\b${lookahead}`, 'g');
49+
: new RegExp(`\\b(${keys.join('|')})\\b(?!\\.)${lookahead}`, 'g');
5050

5151
return {
5252
name: 'replace',
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
module.exports = {
22
description: 'replaces nothing',
3-
options: {}
3+
options: {
4+
'typeof window': `"object"`
5+
}
46
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
console.log('as-it'); // eslint-disable-line
2+
console.log(typeof window.document); // eslint-disable-line
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
console.log('as-it');
2+
console.log(typeof window.document); // eslint-disable-line
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module.exports = {
22
description: 'replaces strings',
33
options: {
4-
ANSWER: '42'
4+
ANSWER: '42',
5+
'typeof window': `"object"`
56
}
67
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
console.log(ANSWER); // eslint-disable-line
2+
console.log(typeof window);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
console.log(42);
2+
console.log('object');

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
The actual snapshot is saved in `form.js.snap`.
44

5-
Generated by [AVA](https://ava.li).
5+
Generated by [AVA](https://avajs.dev).
6+
7+
## assignment: doesn't replace lvalue in assignment
8+
9+
> Snapshot 1
10+
11+
'process.env.DEBUG = \'test\';'
612

713
## delimiters: observes delimiters
814

@@ -37,13 +43,15 @@ Generated by [AVA](https://ava.li).
3743

3844
> Snapshot 1
3945
40-
'console.log(\'as-it\'); // eslint-disable-line'
46+
`console.log('as-it'); // eslint-disable-line␊
47+
console.log(typeof window.document); // eslint-disable-line`
4148

4249
## replace-strings: replaces strings
4350

4451
> Snapshot 1
4552
46-
'console.log(42); // eslint-disable-line'
53+
`console.log(42); // eslint-disable-line␊
54+
console.log("object");`
4755

4856
## replacement-function: allows replacement to be a function
4957

@@ -58,9 +66,3 @@ Generated by [AVA](https://ava.li).
5866
`const one = 1; // eslint-disable-line␊
5967
6068
console.log(one);`
61-
62-
## assignment: doesn't replace lvalue in assignment
63-
64-
> Snapshot 1
65-
66-
'process.env.DEBUG = \'test\';'

0 commit comments

Comments
 (0)