Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(replace)!: do not escape delimiters #1088

Merged
merged 2 commits into from
Feb 22, 2022
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
4 changes: 3 additions & 1 deletion packages/replace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ 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) 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:
Expand All @@ -70,6 +70,8 @@ For example, if you pass `typeof window` in `values` to-be-replaced, then you co
- `typeof window.document` **will not** be replaced due to `(?!\.)` boundary
- `typeof windowSmth` **will not** be replaced due to a `\b` boundary

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.

### `objectGuards`

Type: `Boolean`<br>
Expand Down
12 changes: 5 additions & 7 deletions packages/replace/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,16 @@ function expandTypeofReplacements(replacements) {

export default function replace(options = {}) {
const filter = createFilter(options.include, options.exclude);
const { delimiters, preventAssignment, objectGuards } = options;
const { delimiters = ['\\b', '\\b(?!\\.)'], preventAssignment, objectGuards } = options;
const replacements = getReplacements(options);
if (objectGuards) expandTypeofReplacements(replacements);
const functionValues = mapToFunctions(replacements);
const keys = Object.keys(functionValues).sort(longest).map(escape);
const lookahead = preventAssignment ? '(?!\\s*=[^=])' : '';
const pattern = delimiters
? new RegExp(
`${escape(delimiters[0])}(${keys.join('|')})${escape(delimiters[1])}${lookahead}`,
'g'
)
: new RegExp(`\\b(${keys.join('|')})\\b(?!\\.)${lookahead}`, 'g');
const pattern = new RegExp(
`${delimiters[0]}(${keys.join('|')})${delimiters[1]}${lookahead}`,
'g'
);

return {
name: 'replace',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
description: 'allows delimiters with special characters',
options: {
special: 'replaced',
delimiters: ['\\b', '\\b']
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
console.log(`
special
(special)
specially
especial
special.doSomething()
`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
console.log(`
replaced
(replaced)
specially
especial
replaced.doSomething()
`);
12 changes: 12 additions & 0 deletions packages/replace/test/snapshots/form.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,15 @@ Generated by [AVA](https://avajs.dev).
`const one = 1; // eslint-disable-line␊
console.log(one);`

## special-delimiters: allows delimiters with special characters

> Snapshot 1

`console.log(`␊
replaced␊
(replaced)␊
specially␊
especial␊
replaced.doSomething()␊
`);`
Binary file modified packages/replace/test/snapshots/form.js.snap
Binary file not shown.