Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/replace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +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: `['(?<![_$a-zA-Z0-9\\xA0-\\uFFFF])', '(?![_$a-zA-Z0-9\\xA0-\\uFFFF])(?!\\.)']`

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.
Specifies the boundaries around which strings will be replaced. By default, delimiters match JavaScript identifier boundaries 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
- `typeof window.document` **will not** be replaced due to the `(?!\.)` boundary
- `typeof windowSmth` **will not** be replaced due to identifier boundaries

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.

Expand Down Expand Up @@ -194,7 +194,7 @@ replace({

## Word Boundaries

By default, values will only match if they are surrounded by _word boundaries_.
By default, values will only match if they are surrounded by _word boundaries_ that respect JavaScript's rules for valid identifiers (including `$` and `_` as valid identifier characters).

Consider the following options and build file:

Expand Down
6 changes: 5 additions & 1 deletion packages/replace/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ function expandTypeofReplacements(replacements) {

export default function replace(options = {}) {
const filter = createFilter(options.include, options.exclude);
const { delimiters = ['\\b', '\\b(?!\\.)'], preventAssignment, objectGuards } = options;
const {
delimiters = ['(?<![_$a-zA-Z0-9\\xA0-\\uFFFF])', '(?![_$a-zA-Z0-9\\xA0-\\uFFFF])(?!\\.)'],
preventAssignment,
objectGuards
} = options;
const replacements = getReplacements(options);
if (objectGuards) expandTypeofReplacements(replacements);
const functionValues = mapToFunctions(replacements);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
description: 'should not replace when followed by valid identifier characters',
options: {
values: {
'typeof window': '"undefined"'
},
preventAssignment: true
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-disable no-undef */
// Should NOT be replaced - window$1 is a different identifier
if (typeof window$1 === 'undefined') {
console.log('no window$1');
}

// Should be replaced - standalone typeof window
if (typeof window === 'undefined') {
console.log('no window');
}

// Should NOT be replaced - window_ is a different identifier
if (typeof window_ !== 'undefined') {
console.log('has window_');
}

// Should be replaced - typeof window followed by dot
if (typeof window.document !== 'undefined') {
console.log('has document');
}
25 changes: 25 additions & 0 deletions packages/replace/test/snapshots/form.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,31 @@ Generated by [AVA](https://avajs.dev).
<%original %>␊
\`);`

## dollar-sign-boundary: should not replace when followed by valid identifier characters

> Snapshot 1

`/* eslint-disable no-undef */␊
// Should NOT be replaced - window$1 is a different identifier␊
if (typeof window$1 === 'undefined') {␊
console.log('no window$1');␊
}␊
// Should be replaced - standalone "undefined"␊
if ("undefined" === 'undefined') {␊
console.log('no window');␊
}␊
// Should NOT be replaced - window_ is a different identifier ␊
if (typeof window_ !== 'undefined') {␊
console.log('has window_');␊
}␊
// Should be replaced - "undefined" followed by dot␊
if (typeof window.document !== 'undefined') {␊
console.log('has document');␊
}`

## match-variables: matches most specific variables

> Snapshot 1
Expand Down
Binary file modified packages/replace/test/snapshots/form.js.snap
Binary file not shown.
Loading