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): fix replace for ternary operators #1712

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/replace/src/index.js
Expand Up @@ -74,9 +74,10 @@ export default function replace(options = {}) {
if (objectGuards) expandTypeofReplacements(replacements);
const functionValues = mapToFunctions(replacements);
const keys = Object.keys(functionValues).sort(longest).map(escape);
const lookahead = preventAssignment ? '(?!\\s*(=[^=]|:[^:]))' : '';
const lookbehind = preventAssignment ? '(?<!\\b(?:const|let|var)\\s*)' : '';
const lookahead = preventAssignment ? '(?!\\s*=[^=])' : '';
const pattern = new RegExp(
`${delimiters[0]}(${keys.join('|')})${delimiters[1]}${lookahead}`,
`${lookbehind}${delimiters[0]}(${keys.join('|')})${delimiters[1]}${lookahead}`,
'g'
);

Expand Down
@@ -0,0 +1,9 @@
module.exports = {
description: "replaces value inside ternary operators",
options: {
condition: 'first',
exprIfTrue: 'second',
exprIfFalse: 'third',
preventAssignment: true
}
};
@@ -0,0 +1,3 @@
/* eslint-disable */
condition ? exprIfTrue : exprIfFalse;
console.log(condition, exprIfTrue, exprIfFalse);
@@ -0,0 +1,3 @@
/* eslint-disable */
first ? second : third;
console.log(first, second, third);
8 changes: 8 additions & 0 deletions packages/replace/test/snapshots/form.js.md
Expand Up @@ -87,6 +87,14 @@ Generated by [AVA](https://avajs.dev).
replaced.doSomething()␊
\`);`

## ternary-operator: replaces value inside ternary operators

> Snapshot 1

`/* eslint-disable */␊
first ? second : third;␊
console.log(first, second, third);`

## typescript-declare: doesn't replace lvalue in typescript declare

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