From 93cf0a73aa9c386005923fcb45c6cf85d58d4a1b Mon Sep 17 00:00:00 2001 From: Olzhas Alexandrov Date: Fri, 16 Jul 2021 09:24:17 -0500 Subject: [PATCH] fix(replace)!: issues with nested objects replacements (#903) * fix(replace): issues with nested objects replacements * fix(replace): update tests & snapshots --- .github/workflows/release.yml | 3 +- packages/replace/README.md | 9 ++- packages/replace/src/index.js | 2 +- .../fixtures/form/replace-nothing/_config.js | 4 +- .../fixtures/form/replace-nothing/input.js | 1 + .../fixtures/form/replace-nothing/output.js | 1 + .../fixtures/form/replace-strings/_config.js | 3 +- .../fixtures/form/replace-strings/input.js | 1 + .../fixtures/form/replace-strings/output.js | 1 + packages/replace/test/snapshots/form.js.md | 20 ++--- packages/replace/test/snapshots/form.js.snap | Bin 492 -> 529 bytes packages/replace/test/snapshots/misc.js.md | 44 +--------- packages/replace/test/snapshots/misc.js.snap | Bin 716 -> 408 bytes .../replace/test/snapshots/sourcemaps.js.md | 76 +----------------- .../replace/test/snapshots/sourcemaps.js.snap | Bin 435 -> 356 bytes 15 files changed, 33 insertions(+), 132 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 957480a34..d761813ef 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -8,7 +8,8 @@ on: jobs: publish: # let's ignore release commits, otherwise it'll try to run twice - if: !startsWith(github.event.head_commit.message , 'chore(release):') + if: | + !startsWith(github.event.head_commit.message , 'chore(release):') runs-on: ubuntu-latest diff --git a/packages/replace/README.md b/packages/replace/README.md index fd9a607ee..feb809a3e 100644 --- a/packages/replace/README.md +++ b/packages/replace/README.md @@ -61,9 +61,14 @@ In addition to the properties and values specified for replacement, users may al ### `delimiters` Type: `Array[...String, String]`
-Default: `['\b', '\b']` +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. +Specifies the boundaries around which strings will be replaced. By default, delimiters are [word boundaries](https://www.regular-expressions.info/wordboundaries.html) and also prevent replacements of instances with nested access. See [Word Boundaries](#word-boundaries) below for more information. +For example, if you pass `typeof window` in `values` to-be-replaced, then you could expect the following scenarios: + +- `typeof window` **will** be replaced +- `typeof window.document` **will not** be replaced due to `(?!\.)` boundary +- `typeof windowSmth` **will not** be replaced due to a `\b` boundary ### `preventAssignment` diff --git a/packages/replace/src/index.js b/packages/replace/src/index.js index de9262d59..f2085e7e8 100755 --- a/packages/replace/src/index.js +++ b/packages/replace/src/index.js @@ -46,7 +46,7 @@ export default function replace(options = {}) { `${escape(delimiters[0])}(${keys.join('|')})${escape(delimiters[1])}${lookahead}`, 'g' ) - : new RegExp(`\\b(${keys.join('|')})\\b${lookahead}`, 'g'); + : new RegExp(`\\b(${keys.join('|')})\\b(?!\\.)${lookahead}`, 'g'); return { name: 'replace', diff --git a/packages/replace/test/fixtures/form/replace-nothing/_config.js b/packages/replace/test/fixtures/form/replace-nothing/_config.js index cd7021b8c..4b650a64e 100755 --- a/packages/replace/test/fixtures/form/replace-nothing/_config.js +++ b/packages/replace/test/fixtures/form/replace-nothing/_config.js @@ -1,4 +1,6 @@ module.exports = { description: 'replaces nothing', - options: {} + options: { + 'typeof window': `"object"` + } }; diff --git a/packages/replace/test/fixtures/form/replace-nothing/input.js b/packages/replace/test/fixtures/form/replace-nothing/input.js index ea6b2fff5..03677c018 100755 --- a/packages/replace/test/fixtures/form/replace-nothing/input.js +++ b/packages/replace/test/fixtures/form/replace-nothing/input.js @@ -1 +1,2 @@ console.log('as-it'); // eslint-disable-line +console.log(typeof window.document); // eslint-disable-line diff --git a/packages/replace/test/fixtures/form/replace-nothing/output.js b/packages/replace/test/fixtures/form/replace-nothing/output.js index 670fff8fd..7941f6136 100755 --- a/packages/replace/test/fixtures/form/replace-nothing/output.js +++ b/packages/replace/test/fixtures/form/replace-nothing/output.js @@ -1 +1,2 @@ console.log('as-it'); +console.log(typeof window.document); // eslint-disable-line diff --git a/packages/replace/test/fixtures/form/replace-strings/_config.js b/packages/replace/test/fixtures/form/replace-strings/_config.js index a97790980..3392d78ad 100755 --- a/packages/replace/test/fixtures/form/replace-strings/_config.js +++ b/packages/replace/test/fixtures/form/replace-strings/_config.js @@ -1,6 +1,7 @@ module.exports = { description: 'replaces strings', options: { - ANSWER: '42' + ANSWER: '42', + 'typeof window': `"object"` } }; diff --git a/packages/replace/test/fixtures/form/replace-strings/input.js b/packages/replace/test/fixtures/form/replace-strings/input.js index 5125c78ef..eb0ae6d13 100755 --- a/packages/replace/test/fixtures/form/replace-strings/input.js +++ b/packages/replace/test/fixtures/form/replace-strings/input.js @@ -1 +1,2 @@ console.log(ANSWER); // eslint-disable-line +console.log(typeof window); diff --git a/packages/replace/test/fixtures/form/replace-strings/output.js b/packages/replace/test/fixtures/form/replace-strings/output.js index 753a47d52..1b88a6cbd 100755 --- a/packages/replace/test/fixtures/form/replace-strings/output.js +++ b/packages/replace/test/fixtures/form/replace-strings/output.js @@ -1 +1,2 @@ console.log(42); +console.log('object'); diff --git a/packages/replace/test/snapshots/form.js.md b/packages/replace/test/snapshots/form.js.md index d35412356..eaeb3189d 100644 --- a/packages/replace/test/snapshots/form.js.md +++ b/packages/replace/test/snapshots/form.js.md @@ -2,7 +2,13 @@ The actual snapshot is saved in `form.js.snap`. -Generated by [AVA](https://ava.li). +Generated by [AVA](https://avajs.dev). + +## assignment: doesn't replace lvalue in assignment + +> Snapshot 1 + + 'process.env.DEBUG = \'test\';' ## delimiters: observes delimiters @@ -37,13 +43,15 @@ Generated by [AVA](https://ava.li). > 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 @@ -58,9 +66,3 @@ 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\';' diff --git a/packages/replace/test/snapshots/form.js.snap b/packages/replace/test/snapshots/form.js.snap index fdeb4d7d8de0f2ebd74e19e5a944c81335320b11..f0d05a94e1b772b14bfd01f9f6bcadf20a2e1a6a 100644 GIT binary patch delta 438 zcmV;n0ZIPs1CazGK~_N^Q*L2!b7*gLAa*he0ss|%NW68Ugxyl2;51>UpA(THXMg$~ zh>!kcXqzzkZIl$RmTjQOGzCVms58*ITan^_4;f}{V_0LegvW7>DW~wuSMa`HP7?>H@!9HMQWe{Xk zO3u$K&d*8J%gImIP)|xNNmM9HEq^M`%+FIW)HBdCP}j6ZQ7@HRQIKC$qL7lBmROoo zqM($SS5R7_msPBUs!qC~C_g#1xL7YWuT0Oy)hX0n!B#=NB(=Ci9aUiliUSghbu&wV zb}H!WE2I|ZWagFVreqc;Cgr5+0y(K%$cjrU3sUpb6v{L6Qu537Qu32abAMCwN^t5$ zvDFUQAQL0vEL6%*%1TWxQ9=t68)P*JTnY+BsRcQS$*Czorj4pXeoDH77GSvjiA7Ku&RfX;E@&ZejtP?F(jS<|XHprlbN{sTB|wx)l~6 gD@qjd^HPEFVTjY)DE!kcXqzzkZIl$RmTjQOGzCVm=m#La6)FDrkYUy~hBY=zcpTTbGJ-{|fDXC$c1=sl z$46(^>m~VIW4yDO5iB|vh+nNvT`zo~Yxk|4>n_WlnW@eQ7JUrF%na;cA26~q2r?=q z=jRpY=cMZ8C#9AoDwL%b6@O>u=P4NK8R!|PYg(hImrAWD$S*2UNJ&jgEX^rV zP|D0JC@s;;Dpo>OCtXmKpPX7;te2Wsrsv}76zZ;EtDs(zT3n)zs!#{T0g1)BnI%9w z74-ELQj2pk^GbA6GK&+Fa#D4HoKzH@>c~1xjEK`@gRCimOF^M1wIC-kIb}5k$h1*a z$S=xF&&*5AQMCi}VO#|uKLITyLh+cClA4p5n^^)3NFb*;zqBYhH8-&U&h`bfGxL&j uN>fsStken!3*8C}kQF5g`FW|pz%#_ Snapshot 1 - - 'Sourcemap is likely to be incorrect: a plugin (replace) was used to transform files, but didn\'t generate a sourcemap for the transformation. Consult the plugin documentation for help' - -## generates sourcemaps - -> Snapshot 1 - - { - character: 12, - column: 12, - line: 1, - } - -> Snapshot 2 - - { - column: 15, - line: 1, - name: null, - source: 'other.js', - } - -> Snapshot 3 - - { - character: 17, - column: 0, - line: 3, - } - -> Snapshot 4 - - { - column: 0, - line: 2, - name: null, - source: 'main.js', - } +Generated by [AVA](https://avajs.dev). ## no explicit setting of preventAssignment diff --git a/packages/replace/test/snapshots/misc.js.snap b/packages/replace/test/snapshots/misc.js.snap index d4325ce9f8ff2aedb31250fba0e7889fe75a8ddb..c08f3e6da3254d9c6cb0ab77cfaa0ae4a90f68d8 100644 GIT binary patch literal 408 zcmV;J0cZX}RzVWOAM^Lk%+Aaf03g_RFVEu#_xioO_HpofziLAA-Y^9kDb_ViD1ODT zghoTlZH*-?)G*%?!kHjunq^sl3xEi?h`5_2<++xz#;wL!7r()2fx^+{fVv8enOdz@ z0n30nKudFsWi$cvHO#r(qnS}TK0Ir8?2Gf(iQTc=bJXIHO6m9++jxXM!5NFA&4|T* z5N-$>F~_Alq#g-6qM>R@8TjE)YjxtrLTH6eh(~))%#+pMab8f6c`uo|G z(v?Mx29j+ zyj%Tn{Nje2Sl65sV{VGgChp{8-rj@<`XD!g=Ly6<=qW;eLTk1H(7F{01p>PX0D(OK zW38mT!ZSiVLNu_@htPbW=iI^~_blYv*TKO-0{sMB0tJl8?FWZIz6h?TD!lbEaQ^J< z+Z(!=dI&T-pg^{F)5# zHcY)pYrKdRTH#d|32UfTw916xB|2rPZ>Z*;3NX6DE3waTOKlzPDzv_mv1#7wh|iBu zhRJnKC118QV2euAC9X{z)bNF9F>Ie*%Cyy!R^RTj*#&_CfEOre%h6}mjMcuZ5jYU% z7gMSGPDj#<8NliIK)HWRcgLarNl2c=iqEJ>dpuxate5bwa?}$$Tj11}oI-g0=t5*^#jnU=OKV(m%`C?^Oq&X06S>=oEN-zx zPbq0qnR$z`qP!@ccM_2#*0~5f7TrCGXS~6e5!Xm&U*VU1bu!c1<<*&Hp5;2RxUe@n zPEUy_kUMx?;Y;51K}T8nQ9Q>HbBI;M{oj|C{VV; Snapshot 1 @@ -111,75 +111,3 @@ Generated by [AVA](https://ava.li). name: null, source: 'main.js', } - -## generates sourcemaps by dfault - -> Snapshot 1 - - { - character: 12, - column: 12, - line: 1, - } - -> Snapshot 2 - - { - column: 15, - line: 1, - name: null, - source: 'other.js', - } - -> Snapshot 3 - - { - character: 17, - column: 0, - line: 3, - } - -> Snapshot 4 - - { - column: 0, - line: 2, - name: null, - source: 'main.js', - } - -## generates sourcemaps by default - -> Snapshot 1 - - { - character: 12, - column: 12, - line: 1, - } - -> Snapshot 2 - - { - column: 15, - line: 1, - name: null, - source: 'other.js', - } - -> Snapshot 3 - - { - character: 17, - column: 0, - line: 3, - } - -> Snapshot 4 - - { - column: 0, - line: 2, - name: null, - source: 'main.js', - } diff --git a/packages/replace/test/snapshots/sourcemaps.js.snap b/packages/replace/test/snapshots/sourcemaps.js.snap index 87b75d6fe70add86dfc516ccb334f37d7c5cd156..458490a05d2bcd9876774df43bbcfb87b730089a 100644 GIT binary patch literal 356 zcmV-q0h|6oRzVQ3`UvhRFI+*$Q9pQ|*dL1s00000000A< z!@$76%#eBEMeO-hwlk3pYTfU7-FC1rfWUMp4rYYlmr%TziGiVS&%HPGc`h?G79_3n zTeSZN3s}^O8G^q-aSsawyRkAb{JJnbVol3cWlhJEPRYsp8(6@iccFMD8wBgKL-0#> zkh|Ey?qK9#WHAHMHb5K;#MMA7%*ZCl$mXAvm6}|_$OvZ&2naAR@-i?oGcfW4)i8;G zSgeAKoXHu9MTyBJsYPHB7DgU41#HRrIi9e&t`PFyu@6H95XMvp~d;7Maii!E=PVzMrx5>Rxz$1t)fDZ3gYt^10H`d zV+&D`t15wb3WXtx&tfLB1C%{CF*6TOc#bB~(Ih&WL`Re8Xc8qRi2?w-GgAG92mksXgJ6Aj2!08~ zzOw(OitMQ6-+PmRm+y3;APZQufdhhVI3f5F6!&m3Fl1hM5qmzB?M!5YTK9Wiw;e2C zQDtrjz7NHvJP@qU%fP_Qzz+5+BL^dk8IZOC;#eTA24Z1GHbF)<|D>$cI3B*$<3{iX*Gm#yj?74}Vd3eHeG>MKT(a|J2nnXvF d=x7ohO`@YobTo;MCQ(`^Q2?Ey^rtTl004<5zvBP^