Skip to content

Commit

Permalink
fix(replace)!: issues with nested objects replacements (#903)
Browse files Browse the repository at this point in the history
* fix(replace): issues with nested objects replacements

* fix(replace): update tests & snapshots
  • Loading branch information
o-alexandrov authored and shellscape committed Jul 16, 2021
1 parent 6a32b4d commit 93cf0a7
Show file tree
Hide file tree
Showing 15 changed files with 33 additions and 132 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ on:
jobs:
publish:
# let's ignore release commits, otherwise it'll try to run twice
if: !startsWith(github.event.head_commit.message , 'chore(release):')
if: |
!startsWith(github.event.head_commit.message , 'chore(release):')
runs-on: ubuntu-latest

Expand Down
9 changes: 7 additions & 2 deletions packages/replace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@ In addition to the properties and values specified for replacement, users may al
### `delimiters`

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

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.
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.
For example, if you pass `typeof window` in `values` to-be-replaced, then you could expect the following scenarios:

- `typeof window` **will** be replaced
- `typeof window.document` **will not** be replaced due to `(?!\.)` boundary
- `typeof windowSmth` **will not** be replaced due to a `\b` boundary

### `preventAssignment`

Expand Down
2 changes: 1 addition & 1 deletion packages/replace/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function replace(options = {}) {
`${escape(delimiters[0])}(${keys.join('|')})${escape(delimiters[1])}${lookahead}`,
'g'
)
: new RegExp(`\\b(${keys.join('|')})\\b${lookahead}`, 'g');
: new RegExp(`\\b(${keys.join('|')})\\b(?!\\.)${lookahead}`, 'g');

return {
name: 'replace',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module.exports = {
description: 'replaces nothing',
options: {}
options: {
'typeof window': `"object"`
}
};
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
console.log('as-it'); // eslint-disable-line
console.log(typeof window.document); // eslint-disable-line
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
console.log('as-it');
console.log(typeof window.document); // eslint-disable-line
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
description: 'replaces strings',
options: {
ANSWER: '42'
ANSWER: '42',
'typeof window': `"object"`
}
};
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
console.log(ANSWER); // eslint-disable-line
console.log(typeof window);
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
console.log(42);
console.log('object');
20 changes: 11 additions & 9 deletions packages/replace/test/snapshots/form.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

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

Generated by [AVA](https://ava.li).
Generated by [AVA](https://avajs.dev).

## assignment: doesn't replace lvalue in assignment

> Snapshot 1
'process.env.DEBUG = \'test\';'

## delimiters: observes delimiters

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

> Snapshot 1
'console.log(\'as-it\'); // eslint-disable-line'
`console.log('as-it'); // eslint-disable-line␊
console.log(typeof window.document); // eslint-disable-line`

## replace-strings: replaces strings

> Snapshot 1
'console.log(42); // eslint-disable-line'
`console.log(42); // eslint-disable-line␊
console.log("object");`

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

Expand All @@ -58,9 +66,3 @@ Generated by [AVA](https://ava.li).
`const one = 1; // eslint-disable-line␊
console.log(one);`

## assignment: doesn't replace lvalue in assignment

> Snapshot 1
'process.env.DEBUG = \'test\';'
Binary file modified packages/replace/test/snapshots/form.js.snap
Binary file not shown.
44 changes: 1 addition & 43 deletions packages/replace/test/snapshots/misc.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,7 @@

The actual snapshot is saved in `misc.js.snap`.

Generated by [AVA](https://ava.li).

## does not generate sourcemaps if disabled

> Snapshot 1
'Sourcemap is likely to be incorrect: a plugin (replace) was used to transform files, but didn\'t generate a sourcemap for the transformation. Consult the plugin documentation for help'

## generates sourcemaps

> Snapshot 1
{
character: 12,
column: 12,
line: 1,
}

> Snapshot 2
{
column: 15,
line: 1,
name: null,
source: 'other.js',
}

> Snapshot 3
{
character: 17,
column: 0,
line: 3,
}

> Snapshot 4
{
column: 0,
line: 2,
name: null,
source: 'main.js',
}
Generated by [AVA](https://avajs.dev).

## no explicit setting of preventAssignment

Expand Down
Binary file modified packages/replace/test/snapshots/misc.js.snap
Binary file not shown.
76 changes: 2 additions & 74 deletions packages/replace/test/snapshots/sourcemaps.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

The actual snapshot is saved in `sourcemaps.js.snap`.

Generated by [AVA](https://ava.li).
Generated by [AVA](https://avajs.dev).

## generates sourcemaps when sourcemap is used as an output argument
## generates sourcemaps by default

> Snapshot 1
Expand Down Expand Up @@ -111,75 +111,3 @@ Generated by [AVA](https://ava.li).
name: null,
source: 'main.js',
}

## generates sourcemaps by dfault

> Snapshot 1
{
character: 12,
column: 12,
line: 1,
}

> Snapshot 2
{
column: 15,
line: 1,
name: null,
source: 'other.js',
}

> Snapshot 3
{
character: 17,
column: 0,
line: 3,
}

> Snapshot 4
{
column: 0,
line: 2,
name: null,
source: 'main.js',
}

## generates sourcemaps by default

> Snapshot 1
{
character: 12,
column: 12,
line: 1,
}

> Snapshot 2
{
column: 15,
line: 1,
name: null,
source: 'other.js',
}

> Snapshot 3
{
character: 17,
column: 0,
line: 3,
}

> Snapshot 4
{
column: 0,
line: 2,
name: null,
source: 'main.js',
}
Binary file modified packages/replace/test/snapshots/sourcemaps.js.snap
Binary file not shown.

0 comments on commit 93cf0a7

Please sign in to comment.