From 8c213e05398f3bea487b244715a4898a56873278 Mon Sep 17 00:00:00 2001 From: ikkz Date: Mon, 23 Sep 2024 23:14:17 +0800 Subject: [PATCH] fix(replace)!: objectGuards doesn't take effects (#1764) BREAKING CHANGES: Fixed the problem that the objectGuards option did not take effect, so the bundle results before and after may be inconsistent Co-authored-by: shellscape --- packages/replace/src/index.js | 20 ++---------------- .../test/fixtures/form/process-check/input.js | 2 +- packages/replace/test/snapshots/form.js.md | 2 +- packages/replace/test/snapshots/form.js.snap | Bin 801 -> 808 bytes 4 files changed, 4 insertions(+), 20 deletions(-) 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 242f1c08c07fa821ff2f54c6fba036f3c48d578e..297ea1cd9c369fa459394862cd73bffa5f8bd375 100644 GIT binary patch literal 808 zcmV+@1K0dPRzVf#q(1H zYEObA4j+pM00000000Axl}&HcKoEvoA%x@`S2!?@RGd_Gk|HFiQ-=>xK#0NtRe}Rj zH@lN$+s&@Evrbwe4qQ0#C(18H{)%nfR?TU5_I2i&cW3O&=|mZRMt}aIlTP9JkP*q5 zUZ8d%2!o`32Nq++ID%R-l^{qBl}eIkG0lymUzb8K$G-ONY~0&Of17tV?r%Pft>H-2 zN4@O7IedNm4kGX!lJiGp(nkW7Tu6tOLNF6TyF`L2*v|2xk>h|HO-5l~nOXP5gA!PH z00-M(tei>B)b=>rms{8#dndg$#BjkJp9f23C7URu5V8OgEDSlI7`dEE4r@W1YS^Ia z)T#UlItI(M7^7jYKkN_vD)+OZ=pa_fOzObIP8v=54S+V)732aa1Z@P~Ksdg^n95!c zfJ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