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

feat(replace): prevent accidental replacement within assignment #798

Merged
merged 7 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 7 additions & 0 deletions packages/replace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ 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.

### `preventAssignment`

Type: `Boolean`<br>
Default: `false`

Prevents replacing strings where they are followed by a single equals sign (e.g. `process.env.DEBUG = false`).
danielroe marked this conversation as resolved.
Show resolved Hide resolved

### `exclude`

Type: `String` | `Array[...String]`<br>
Expand Down
20 changes: 14 additions & 6 deletions packages/replace/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,22 @@ function mapToFunctions(object) {

export default function replace(options = {}) {
const filter = createFilter(options.include, options.exclude);
const { delimiters } = options;
const { delimiters, preventAssignment } = options;
const functionValues = mapToFunctions(getReplacements(options));
const keys = Object.keys(functionValues)
.sort(longest)
.map(escape);
const keys = Object.keys(functionValues).sort(longest).map(escape);
if (![true, false].includes(preventAssignment)) {
danielroe marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line no-console
console.warn(
danielroe marked this conversation as resolved.
Show resolved Hide resolved
"replace: 'preventAssignment' currently defaults to false. It is recommended to configure this option explicitly, as it may default to true in a future major version."
danielroe marked this conversation as resolved.
Show resolved Hide resolved
);
}
const lookahead = preventAssignment ? '(?!\\s*=[^=])' : '';
const pattern = delimiters
? new RegExp(`${escape(delimiters[0])}(${keys.join('|')})${escape(delimiters[1])}`, 'g')
: new RegExp(`\\b(${keys.join('|')})\\b`, 'g');
? new RegExp(
`${escape(delimiters[0])}(${keys.join('|')})${escape(delimiters[1])}${lookahead}`,
'g'
)
: new RegExp(`\\b(${keys.join('|')})\\b${lookahead}`, 'g');

return {
name: 'replace',
Expand Down
7 changes: 7 additions & 0 deletions packages/replace/test/fixtures/form/assignment/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
description: "doesn't replace lvalue in assignment",
options: {
'process.env.DEBUG': 'replaced',
preventAssignment: true
}
};
1 change: 1 addition & 0 deletions packages/replace/test/fixtures/form/assignment/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.env.DEBUG = 'test';
1 change: 1 addition & 0 deletions packages/replace/test/fixtures/form/assignment/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.env.DEBUG = 'test';
6 changes: 6 additions & 0 deletions packages/replace/test/snapshots/form.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,9 @@ 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.