From 04895fa7236e1e11fbc661747d14baee2fccb8c2 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Fri, 7 Jan 2022 16:45:52 -0800 Subject: [PATCH 01/13] feat: typeofReplacements option for plugin-replace --- packages/replace/README.md | 33 ++++++++++++++++++ packages/replace/src/index.js | 22 ++++++++++-- .../fixtures/form/process-check/_config.js | 8 +++++ .../test/fixtures/form/process-check/input.js | 3 ++ .../fixtures/form/process-check/output.js | 1 + packages/replace/test/snapshots/form.js.md | 10 +++++- packages/replace/test/snapshots/form.js.snap | Bin 529 -> 595 bytes 7 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 packages/replace/test/fixtures/form/process-check/_config.js create mode 100644 packages/replace/test/fixtures/form/process-check/input.js create mode 100644 packages/replace/test/fixtures/form/process-check/output.js diff --git a/packages/replace/README.md b/packages/replace/README.md index 533e09c9d..8b1e4185d 100644 --- a/packages/replace/README.md +++ b/packages/replace/README.md @@ -70,6 +70,39 @@ 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 +### `typeofReplacements` + +Type: `Boolean`
+Default: `false` + +When replacing dot-separated object properties like `process.env.NODE_ENV`, will also replace typeof checks against the objects +with `"object"`. + +For example: + +```js +replace({ + values: { + 'process.env.NODE_ENV': '"production"', + }, +}); +``` + +```js +// Input +if (typeof process !== 'undefined' && process.env.NODE_ENV === 'production') { + console.log('production'); +} +// Without `typeofReplacements` +if (typeof process !== 'undefined' && 'production' === 'production') { + console.log('production'); +} +// With `typeofReplacements` +if ('object' !== 'undefined' && 'production' === 'production') { + console.log('production'); +} +``` + ### `preventAssignment` Type: `Boolean`
diff --git a/packages/replace/src/index.js b/packages/replace/src/index.js index f2085e7e8..5747ec2e7 100755 --- a/packages/replace/src/index.js +++ b/packages/replace/src/index.js @@ -24,6 +24,7 @@ function getReplacements(options) { delete values.exclude; delete values.sourcemap; delete values.sourceMap; + delete values.typeofReplacements; return values; } @@ -35,10 +36,27 @@ function mapToFunctions(object) { }, {}); } +const objKeyRegEx = /^([_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*)(\.([_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*))+$/; +function expandTypeofReplacements(replacements) { + Object.keys(replacements).forEach((key) => { + const objMatch = key.match(objKeyRegEx); + if (!objMatch) return; + let dotIndex = objMatch[1].length; + let lastIndex = 0; + do { + replacements[`typeof ${key.slice(lastIndex, dotIndex)}`] = '"object"'; + lastIndex = dotIndex + 1; + dotIndex = key.indexOf('.', lastIndex); + } while (dotIndex !== -1); + }); +} + export default function replace(options = {}) { const filter = createFilter(options.include, options.exclude); - const { delimiters, preventAssignment } = options; - const functionValues = mapToFunctions(getReplacements(options)); + const { delimiters, preventAssignment, typeofReplacements } = options; + const replacements = getReplacements(options); + if (typeofReplacements) expandTypeofReplacements(replacements); + const functionValues = mapToFunctions(replacements); const keys = Object.keys(functionValues).sort(longest).map(escape); const lookahead = preventAssignment ? '(?!\\s*=[^=])' : ''; const pattern = delimiters diff --git a/packages/replace/test/fixtures/form/process-check/_config.js b/packages/replace/test/fixtures/form/process-check/_config.js new file mode 100644 index 000000000..48b59e8ee --- /dev/null +++ b/packages/replace/test/fixtures/form/process-check/_config.js @@ -0,0 +1,8 @@ +module.exports = { + description: 'Handles process type guards in replacements', + options: { + 'process.env.NODE_ENV': '"production"', + preventAssignment: true, + typeofReplacements: true + } +}; diff --git a/packages/replace/test/fixtures/form/process-check/input.js b/packages/replace/test/fixtures/form/process-check/input.js new file mode 100644 index 000000000..dcce0a89f --- /dev/null +++ b/packages/replace/test/fixtures/form/process-check/input.js @@ -0,0 +1,3 @@ +if (typeof process !== 'undefined' && process.env.NODE_ENV === 'production') { + console.log('production'); +} diff --git a/packages/replace/test/fixtures/form/process-check/output.js b/packages/replace/test/fixtures/form/process-check/output.js new file mode 100644 index 000000000..5fa7f8f80 --- /dev/null +++ b/packages/replace/test/fixtures/form/process-check/output.js @@ -0,0 +1 @@ +process.env.DEBUG = 'test'; diff --git a/packages/replace/test/snapshots/form.js.md b/packages/replace/test/snapshots/form.js.md index eaeb3189d..efc780818 100644 --- a/packages/replace/test/snapshots/form.js.md +++ b/packages/replace/test/snapshots/form.js.md @@ -39,12 +39,20 @@ Generated by [AVA](https://avajs.dev). exclude␊ `);` +## process-check: Handles process type guards in replacements + +> Snapshot 1 + + `if ("object" !== 'undefined' && "production" === 'production') {␊ + console.log('production');␊ + }` + ## replace-nothing: replaces nothing > Snapshot 1 `console.log('as-it'); // eslint-disable-line␊ - console.log(typeof window.document); // eslint-disable-line` + console.log("object".document); // eslint-disable-line` ## replace-strings: replaces strings diff --git a/packages/replace/test/snapshots/form.js.snap b/packages/replace/test/snapshots/form.js.snap index f0d05a94e1b772b14bfd01f9f6bcadf20a2e1a6a..0ec95e2fa48c512d2d129b1a56b14b1408acc34c 100644 GIT binary patch literal 595 zcmV-Z0<8T(RzVJ_HuNxpMLz-Y(Vq-$6DGfn zlH%2}4HTKCzz7z#16p@>@z&5LrTKjCEFUR8oh{YG2o_xp#J3{F{~j{T+QzWPW(kku z8dpZJ=vN@V_jXN7%Ew1%*Xt$uTw}bmnGq}+!NkDuYIW**;R9W}Z|z)nS@z6Kbw;r0 zJRoLfUS1zQF6lGNf7 zbyS7PC=N(0*3B#d+Nq$guaH`tlbKhdo03_an3R*M3*@A7AuCqOPs&P7E>Y4;$xklL zP0cI8sSU+KJ7hCVj7YN!Ei~dY(-h!x3W~NsFP7#3g9fNQMO{HnO+g753@N3_CBX1h zQm_TdBLvhn6{@)u6j1z+ENjhGi(;7#vdRRY&Z5+UoW$hR6d=<^RUyA9Gd(jeF-O%7 z%!hFmfcykBXNTf3DJ3;0GdHsYm8Rql=&{Rp@$JH`W=Xm{$yyIF!^ni z6t9+TpvW`@MzE+e(7IcZ;(rerW^H3wW3z3wC8Q8%-U}R+wWK>Gd&nwQ)N!82A zPuEaSN-arLC`&CW&dkqKFw`^9Gf>yGMo}-7T2YW+RHBfQnwD6aQ=*`hnO9I+qL)>y zgsM)upeR2%wYXR>HLpz1#nma)UBOmCy(G1`L>*OO28sg`i*++gfOabA>no%d=Va!U z=%!>ACnn{j>H;~bT*!(`DhpEc(-g`x^HTE5^-}VaOLJ56N^t5$vDFUQAQL0vEL6%* z%1TWxQ9=t68)P*JTnY+BsRcQS$*Czorj4pXeoD zH77GSvjiA7Ku&RfX;E@&ZejtP?F(jS<|XHprlbN{sTB|wx)l~6D@qjd^HPEFVTjY) TDE3 Date: Sat, 8 Jan 2022 10:48:42 -0800 Subject: [PATCH 02/13] prettier fix --- packages/replace/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/replace/README.md b/packages/replace/README.md index 8b1e4185d..002578b41 100644 --- a/packages/replace/README.md +++ b/packages/replace/README.md @@ -83,8 +83,8 @@ For example: ```js replace({ values: { - 'process.env.NODE_ENV': '"production"', - }, + 'process.env.NODE_ENV': '"production"' + } }); ``` From 1e72eae5586c22b8812617bc2df8e03c5f0fba57 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sat, 8 Jan 2022 10:51:17 -0800 Subject: [PATCH 03/13] lint fix --- packages/replace/src/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/replace/src/index.js b/packages/replace/src/index.js index 5747ec2e7..406815093 100755 --- a/packages/replace/src/index.js +++ b/packages/replace/src/index.js @@ -44,6 +44,7 @@ function expandTypeofReplacements(replacements) { let dotIndex = objMatch[1].length; let lastIndex = 0; do { + // eslint-disable-next-line no-param-reassign replacements[`typeof ${key.slice(lastIndex, dotIndex)}`] = '"object"'; lastIndex = dotIndex + 1; dotIndex = key.indexOf('.', lastIndex); From 8618c3c76fefd1940711918ae746af918f854253 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sat, 8 Jan 2022 11:20:43 -0800 Subject: [PATCH 04/13] prettier fix v2 --- packages/replace/src/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/replace/src/index.js b/packages/replace/src/index.js index 406815093..7be20c34a 100755 --- a/packages/replace/src/index.js +++ b/packages/replace/src/index.js @@ -36,7 +36,8 @@ function mapToFunctions(object) { }, {}); } -const objKeyRegEx = /^([_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*)(\.([_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*))+$/; +const objKeyRegEx = + /^([_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*)(\.([_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*))+$/; function expandTypeofReplacements(replacements) { Object.keys(replacements).forEach((key) => { const objMatch = key.match(objKeyRegEx); @@ -44,7 +45,7 @@ function expandTypeofReplacements(replacements) { let dotIndex = objMatch[1].length; let lastIndex = 0; do { - // eslint-disable-next-line no-param-reassign + // eslint-disable-next-line no-param-reassign replacements[`typeof ${key.slice(lastIndex, dotIndex)}`] = '"object"'; lastIndex = dotIndex + 1; dotIndex = key.indexOf('.', lastIndex); From a857632a48c28b19d1412555594b4ad819297674 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sat, 8 Jan 2022 11:46:48 -0800 Subject: [PATCH 05/13] prettier fix --- packages/replace/src/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/replace/src/index.js b/packages/replace/src/index.js index 7be20c34a..7007b2ded 100755 --- a/packages/replace/src/index.js +++ b/packages/replace/src/index.js @@ -36,8 +36,7 @@ function mapToFunctions(object) { }, {}); } -const objKeyRegEx = - /^([_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*)(\.([_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*))+$/; +const objKeyRegEx = /^([_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*)(\.([_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*))+$/; function expandTypeofReplacements(replacements) { Object.keys(replacements).forEach((key) => { const objMatch = key.match(objKeyRegEx); From 24bcb02b80ccfcdcb3c6d2c37ee489b4c793ff87 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sat, 8 Jan 2022 12:00:16 -0800 Subject: [PATCH 06/13] test fix --- packages/replace/src/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/replace/src/index.js b/packages/replace/src/index.js index 7007b2ded..cb488721f 100755 --- a/packages/replace/src/index.js +++ b/packages/replace/src/index.js @@ -45,7 +45,9 @@ function expandTypeofReplacements(replacements) { let lastIndex = 0; do { // eslint-disable-next-line no-param-reassign - replacements[`typeof ${key.slice(lastIndex, dotIndex)}`] = '"object"'; + 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); } while (dotIndex !== -1); From 10873513021eb787c2ca27cb0b035d1ff6cedfee Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sat, 8 Jan 2022 12:25:42 -0800 Subject: [PATCH 07/13] test fixes --- packages/replace/src/index.js | 16 ++++++++++++++-- packages/replace/test/snapshots/form.js.md | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/replace/src/index.js b/packages/replace/src/index.js index cb488721f..b704f08fc 100755 --- a/packages/replace/src/index.js +++ b/packages/replace/src/index.js @@ -45,9 +45,21 @@ function expandTypeofReplacements(replacements) { let lastIndex = 0; do { // eslint-disable-next-line no-param-reassign - replacements[`typeof ${key.slice(lastIndex, dotIndex)} ==`] = '"object" =='; + replacements[`typeof ${key.slice(lastIndex, dotIndex)} ===`] = '"object" ==='; // eslint-disable-next-line no-param-reassign - replacements[`typeof ${key.slice(lastIndex, dotIndex)} !=`] = '"object" !='; + 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); } while (dotIndex !== -1); diff --git a/packages/replace/test/snapshots/form.js.md b/packages/replace/test/snapshots/form.js.md index efc780818..aac44894f 100644 --- a/packages/replace/test/snapshots/form.js.md +++ b/packages/replace/test/snapshots/form.js.md @@ -52,7 +52,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `console.log('as-it'); // eslint-disable-line␊ - console.log("object".document); // eslint-disable-line` + console.log(typeof window.document); // eslint-disable-line` ## replace-strings: replaces strings From 4c284ed4b07f4fb62ef2d0e67b24cd414d0737e6 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sat, 8 Jan 2022 12:44:33 -0800 Subject: [PATCH 08/13] update snapshots --- packages/replace/test/snapshots/form.js.md | 34 +++++++++---------- packages/replace/test/snapshots/form.js.snap | Bin 595 -> 611 bytes 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/replace/test/snapshots/form.js.md b/packages/replace/test/snapshots/form.js.md index aac44894f..33905ec0d 100644 --- a/packages/replace/test/snapshots/form.js.md +++ b/packages/replace/test/snapshots/form.js.md @@ -14,10 +14,10 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 - `console.log(`␊ - replaced␊ - <% original%>␊ - <%original %>␊ + `console.log(`␍␊ + replaced␍␊ + <% original%>␍␊ + <%original %>␍␊ `);` ## match-variables: matches most specific variables @@ -30,35 +30,35 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 - `console.log(`␊ - replaced␊ - delimiters␊ - sourcemap␊ - sourceMap␊ - include␊ - exclude␊ + `console.log(`␍␊ + replaced␍␊ + delimiters␍␊ + sourcemap␍␊ + sourceMap␍␊ + include␍␊ + exclude␍␊ `);` ## process-check: Handles process type guards in replacements > Snapshot 1 - `if ("object" !== 'undefined' && "production" === 'production') {␊ - console.log('production');␊ + `if ("object" !== 'undefined' && "production" === 'production') {␍␊ + console.log('production');␍␊ }` ## replace-nothing: replaces nothing > Snapshot 1 - `console.log('as-it'); // eslint-disable-line␊ + `console.log('as-it'); // eslint-disable-line␍␊ console.log(typeof window.document); // eslint-disable-line` ## replace-strings: replaces strings > Snapshot 1 - `console.log(42); // eslint-disable-line␊ + `console.log(42); // eslint-disable-line␍␊ console.log("object");` ## replacement-function: allows replacement to be a function @@ -71,6 +71,6 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 - `const one = 1; // eslint-disable-line␊ - ␊ + `const one = 1; // eslint-disable-line␍␊ + ␍␊ console.log(one);` diff --git a/packages/replace/test/snapshots/form.js.snap b/packages/replace/test/snapshots/form.js.snap index 0ec95e2fa48c512d2d129b1a56b14b1408acc34c..9017fd6a2716e395fa2c7cdf9e07b5cffb19e7f3 100644 GIT binary patch delta 519 zcmV+i0{H#Y1mgrEK~_N^Q*L2!b7*gLAa*he0swed$Kc+mcSFcFMuV5A`5loWXn%hI z@zI|QZ4)NHjgsQkvJDiOroadmbp={?cJbEGCZ+j&?<^lFKAkPq#0VDM0>rl>#s3~M z%-Y7V#%2kR;~JnaNR)|*f#KfUH7zM0ADvyVm*jJe@y=#OuxJhtzgnHTUid)Q?pr(8 zU6wsFQ=Jhkx&?@t8Q8&oV`OCzWPemj&d)2(&q>wG$xqi%Pf9IGR47X=D$dN$Q!vys z&@)iiv_?@cm0D4dUsR%ylA4xSnp2{nl$lphTB4U#tc0pgx}YdOIkmW0FEy`B&&Aa# z)Lp?=LA@lkxI`UQVJ3+OMrGN=<6$_7UyK! z3sUpb6v{L6Qu537Qu32ab5rw5aB4=e)*jg)6C)yRRLW1vN=+_NLJO3{%rphKoPwe) z(5Iz&zz_oJPf=G;Q&Ug^hDJ(hatSb4l@x42@(2NSO@(S+E(HaYa6p!}=H;qIvCaWm zX#z-ZQEEX>VsdHT1sk8W^QH)FiC)T z#rdU0$*H-C1qhBWgp-+T3Sy>KK$++^S%GXSQOM6r1*Qc<+#!w{=0N3|)&Q`H JmNcXT003s1>Wu&Z delta 503 zcmVc``CMbXvzZYr8o|WC@M?AHdf@|IyKn7W zcUkt#Om#-E=sX~1W?%>Vjggf>kbhArIX|yBKPOc$CqG?7Jt?&$QK2lgs5mn}Pr*>n zK+iy3(;7v+RBA;*eo=`+N@`kSX-4KvC Date: Sat, 8 Jan 2022 12:47:04 -0800 Subject: [PATCH 09/13] windows snapshot fix --- packages/replace/test/snapshots/form.js.md | 34 +++++++++++----------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/replace/test/snapshots/form.js.md b/packages/replace/test/snapshots/form.js.md index 33905ec0d..4e6eccca2 100644 --- a/packages/replace/test/snapshots/form.js.md +++ b/packages/replace/test/snapshots/form.js.md @@ -14,10 +14,10 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 - `console.log(`␍␊ - replaced␍␊ - <% original%>␍␊ - <%original %>␍␊ + `console.log(`␍ + replaced␍ + <% original%>␍ + <%original %>␍ `);` ## match-variables: matches most specific variables @@ -30,35 +30,35 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 - `console.log(`␍␊ - replaced␍␊ - delimiters␍␊ - sourcemap␍␊ - sourceMap␍␊ - include␍␊ - exclude␍␊ + `console.log(`␍ + replaced␍ + delimiters␍ + sourcemap␍ + sourceMap␍ + include␍ + exclude␍ `);` ## process-check: Handles process type guards in replacements > Snapshot 1 - `if ("object" !== 'undefined' && "production" === 'production') {␍␊ - console.log('production');␍␊ + `if ("object" !== 'undefined' && "production" === 'production') {␍ + console.log('production');␊ }` ## replace-nothing: replaces nothing > Snapshot 1 - `console.log('as-it'); // eslint-disable-line␍␊ + `console.log('as-it'); // eslint-disable-line␍ console.log(typeof window.document); // eslint-disable-line` ## replace-strings: replaces strings > Snapshot 1 - `console.log(42); // eslint-disable-line␍␊ + `console.log(42); // eslint-disable-line␍ console.log("object");` ## replacement-function: allows replacement to be a function @@ -71,6 +71,6 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 - `const one = 1; // eslint-disable-line␍␊ - ␍␊ + `const one = 1; // eslint-disable-line␍ + ␍ console.log(one);` From e439f375125ef01aae0fe60615aff5f68ed47f5b Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sat, 8 Jan 2022 20:05:51 -0800 Subject: [PATCH 10/13] update snapshots from posix --- packages/replace/test/snapshots/form.js.md | 32 +++++++++---------- packages/replace/test/snapshots/form.js.snap | Bin 611 -> 610 bytes 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/packages/replace/test/snapshots/form.js.md b/packages/replace/test/snapshots/form.js.md index 4e6eccca2..c0d1e0c93 100644 --- a/packages/replace/test/snapshots/form.js.md +++ b/packages/replace/test/snapshots/form.js.md @@ -14,10 +14,10 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 - `console.log(`␍ - replaced␍ - <% original%>␍ - <%original %>␍ + `console.log(`␊ + replaced␊ + <% original%>␊ + <%original %>␊ `);` ## match-variables: matches most specific variables @@ -30,20 +30,20 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 - `console.log(`␍ - replaced␍ - delimiters␍ - sourcemap␍ - sourceMap␍ - include␍ - exclude␍ + `console.log(`␊ + replaced␊ + delimiters␊ + sourcemap␊ + sourceMap␊ + include␊ + exclude␊ `);` ## process-check: Handles process type guards in replacements > Snapshot 1 - `if ("object" !== 'undefined' && "production" === 'production') {␍ + `if (typeof process !== 'undefined' && "production" === 'production') {␊ console.log('production');␊ }` @@ -51,14 +51,14 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 - `console.log('as-it'); // eslint-disable-line␍ + `console.log('as-it'); // eslint-disable-line␊ console.log(typeof window.document); // eslint-disable-line` ## replace-strings: replaces strings > Snapshot 1 - `console.log(42); // eslint-disable-line␍ + `console.log(42); // eslint-disable-line␊ console.log("object");` ## replacement-function: allows replacement to be a function @@ -71,6 +71,6 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 - `const one = 1; // eslint-disable-line␍ - ␍ + `const one = 1; // eslint-disable-line␊ + ␊ console.log(one);` diff --git a/packages/replace/test/snapshots/form.js.snap b/packages/replace/test/snapshots/form.js.snap index 9017fd6a2716e395fa2c7cdf9e07b5cffb19e7f3..f87dfde2149f4d95f910955d1ef4d9c968321533 100644 GIT binary patch delta 518 zcmV+h0{Q*p1mXlDK~_N^Q*L2!b7*gLAa*he0ssv>>h@YKkF`F>K1?-d1zM3JXn(&0 z@zI|QZ4)NHjgsQkvJDiOroadmbp~2@cJbEGCZ+j&?<^lFKAkPq#0VDM2E?}_#s3~M z%-Y7V#%2kR;~G~+uqXo)1H-+yYg$r1K03QzFUjW`mPb$bYDmoS#>mpOdPWlb^1ko|IaWs8E(#RGgWgr(mdO zpl6`2X^o;@Dz%~@zoN zq~@n7lxOCpeX$mmMKz*&C zXbbdhX&x|?fQF=~E2ya{C;{bCN|Q@~L93)-3zA0&sB0=zb15jG1O>9JHCHW)l{UyK z6M#C4QVVhtlT%ZGOdC~&{9B^T^vt}(9926oAI4Px@)OV;ABxAMl+>Ke+{_YSasYCQ z^Gl18Q*#pw;A~$oJ2Njir!*xM$V#n%u+Xir09jF@ke`S)0TmQl IFqs1Y01Crl>#s3~M z%-Y7V#%2kR;~JnaNR)|*f#KfUH7zM0ADvyVm*jJe@y=#OuxJhtzgnHTUid)Q?pr(8 zU6wsFQ=Jhkx&?@t8Q8&oV`OCzWPemj&d)2(&q>wG$xqi%Pf9IGR47X=D$dN$Q!vys z&@)iiv_?@cm0D4dUsR%ylA4xSnp2{nl$lphTB4U#tc0pgx}YdOIkmW0FEy`B&&Aa# z)Lp?=LA@lkxI`UQVJ3+OMrGN=<6$_7UyK! z3sUpb6v{L6Qu537Qu32ab5rw5aB4=e)*jg)6C)yRRLW1vN=+_NLJO3{%rphKoPwe) z(5Iz&zz_oJPf=G;Q&Ug^hDJ(hatSb4l@x42@(2NSO@(S+E(HaYa6p!}=H;qIvCaWm zX#z-ZQEEX>VsdHT1sk8W^QH)FiC)T z#rdU0$*H-C1qhBWgp-+T3Sy>KK$++^S%GXSQOM6r1*Qc<+#!w{=0N3|)&Q`H JmNcXT004yp>YM-o From d0745867ace89221c8af8f699dc5ee3e1196a3ec Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Fri, 14 Jan 2022 15:27:15 -0800 Subject: [PATCH 11/13] rename to objectGuards --- packages/replace/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/replace/README.md b/packages/replace/README.md index 002578b41..bd5ffb26f 100644 --- a/packages/replace/README.md +++ b/packages/replace/README.md @@ -70,13 +70,13 @@ 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 -### `typeofReplacements` +### `objectGuards` Type: `Boolean`
Default: `false` -When replacing dot-separated object properties like `process.env.NODE_ENV`, will also replace typeof checks against the objects -with `"object"`. +When replacing dot-separated object properties like `process.env.NODE_ENV`, will also replace `typeof process` object guard +checks against the objects with the string `"object"`. For example: @@ -93,11 +93,11 @@ replace({ if (typeof process !== 'undefined' && process.env.NODE_ENV === 'production') { console.log('production'); } -// Without `typeofReplacements` +// Without `objectGuards` if (typeof process !== 'undefined' && 'production' === 'production') { console.log('production'); } -// With `typeofReplacements` +// With `objectGuards` if ('object' !== 'undefined' && 'production' === 'production') { console.log('production'); } From 1604751aac875ba190329ebb77a0e777ab1ed1d2 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Fri, 14 Jan 2022 15:27:57 -0800 Subject: [PATCH 12/13] typeofReplacemens -> objectGuards --- packages/replace/src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/replace/src/index.js b/packages/replace/src/index.js index b704f08fc..dd001b234 100755 --- a/packages/replace/src/index.js +++ b/packages/replace/src/index.js @@ -24,7 +24,7 @@ function getReplacements(options) { delete values.exclude; delete values.sourcemap; delete values.sourceMap; - delete values.typeofReplacements; + delete values.objectGuards; return values; } @@ -68,9 +68,9 @@ function expandTypeofReplacements(replacements) { export default function replace(options = {}) { const filter = createFilter(options.include, options.exclude); - const { delimiters, preventAssignment, typeofReplacements } = options; + const { delimiters, preventAssignment, objectGuards } = options; const replacements = getReplacements(options); - if (typeofReplacements) expandTypeofReplacements(replacements); + if (objectGuards) expandTypeofReplacements(replacements); const functionValues = mapToFunctions(replacements); const keys = Object.keys(functionValues).sort(longest).map(escape); const lookahead = preventAssignment ? '(?!\\s*=[^=])' : ''; From 9f07ce67d2fa35aa7d339bc2e8a6bcad608331a0 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Fri, 14 Jan 2022 15:28:25 -0800 Subject: [PATCH 13/13] typeofReplacements -> objectGuards --- packages/replace/test/fixtures/form/process-check/_config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/replace/test/fixtures/form/process-check/_config.js b/packages/replace/test/fixtures/form/process-check/_config.js index 48b59e8ee..43c1a7f9d 100644 --- a/packages/replace/test/fixtures/form/process-check/_config.js +++ b/packages/replace/test/fixtures/form/process-check/_config.js @@ -3,6 +3,6 @@ module.exports = { options: { 'process.env.NODE_ENV': '"production"', preventAssignment: true, - typeofReplacements: true + objectGuards: true } };