diff --git a/packages/replace/src/index.js b/packages/replace/src/index.js index cdbe6419a..48f624ad8 100755 --- a/packages/replace/src/index.js +++ b/packages/replace/src/index.js @@ -43,26 +43,10 @@ function expandTypeofReplacements(replacements) { const objMatch = key.match(objKeyRegEx); if (!objMatch) return; let dotIndex = objMatch[1].length; - let lastIndex = 0; do { // eslint-disable-next-line no-param-reassign - replacements[`typeof ${key.slice(lastIndex, dotIndex)} ===`] = '"object" ==='; - // eslint-disable-next-line no-param-reassign - replacements[`typeof ${key.slice(lastIndex, dotIndex)} !==`] = '"object" !=='; - // eslint-disable-next-line no-param-reassign - replacements[`typeof ${key.slice(lastIndex, dotIndex)}===`] = '"object"==='; - // eslint-disable-next-line no-param-reassign - replacements[`typeof ${key.slice(lastIndex, dotIndex)}!==`] = '"object"!=='; - // eslint-disable-next-line no-param-reassign - replacements[`typeof ${key.slice(lastIndex, dotIndex)} ==`] = '"object" ==='; - // eslint-disable-next-line no-param-reassign - replacements[`typeof ${key.slice(lastIndex, dotIndex)} !=`] = '"object" !=='; - // eslint-disable-next-line no-param-reassign - replacements[`typeof ${key.slice(lastIndex, dotIndex)}==`] = '"object"==='; - // eslint-disable-next-line no-param-reassign - replacements[`typeof ${key.slice(lastIndex, dotIndex)}!=`] = '"object"!=='; - lastIndex = dotIndex + 1; - dotIndex = key.indexOf('.', lastIndex); + replacements[`typeof ${key.slice(0, dotIndex)}`] = '"object"'; + dotIndex = key.indexOf('.', dotIndex + 1); } while (dotIndex !== -1); }); } diff --git a/packages/replace/test/fixtures/form/process-check/input.js b/packages/replace/test/fixtures/form/process-check/input.js index dcce0a89f..8e6bf9ddc 100644 --- a/packages/replace/test/fixtures/form/process-check/input.js +++ b/packages/replace/test/fixtures/form/process-check/input.js @@ -1,3 +1,3 @@ -if (typeof process !== 'undefined' && process.env.NODE_ENV === 'production') { +if (typeof process !== 'undefined' && typeof process.env === "object" && process.env.NODE_ENV === 'production') { console.log('production'); } diff --git a/packages/replace/test/snapshots/form.js.md b/packages/replace/test/snapshots/form.js.md index 04ba72de9..014ff876f 100644 --- a/packages/replace/test/snapshots/form.js.md +++ b/packages/replace/test/snapshots/form.js.md @@ -43,7 +43,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 - `if (typeof process !== 'undefined' && "production" === 'production') {␊ + `if ("object" !== 'undefined' && "object" === "object" && "production" === 'production') {␊ console.log('production');␊ }` diff --git a/packages/replace/test/snapshots/form.js.snap b/packages/replace/test/snapshots/form.js.snap index 242f1c08c..297ea1cd9 100644 Binary files a/packages/replace/test/snapshots/form.js.snap and b/packages/replace/test/snapshots/form.js.snap differ