From 047d21c6935f0a9a3c9c7876db25edbb5afa37a3 Mon Sep 17 00:00:00 2001 From: hhgnsc Date: Mon, 30 Oct 2023 02:26:52 +0800 Subject: [PATCH] fix(replace): prevent variable before colons from being replaced when preventAssigment is true (#1609) * fix: prevent variable before colons from being replaced when preventAssignment is true. * feat: add typescript-declare test. --------- Co-authored-by: huanghao36 --- packages/replace/src/index.js | 2 +- .../fixtures/form/typescript-declare/_config.js | 8 ++++++++ .../fixtures/form/typescript-declare/input.ts | 2 ++ .../fixtures/form/typescript-declare/output.ts | 3 +++ packages/replace/test/form.js | 5 +++-- packages/replace/test/snapshots/form.js.md | 7 +++++++ packages/replace/test/snapshots/form.js.snap | Bin 691 -> 733 bytes 7 files changed, 24 insertions(+), 3 deletions(-) create mode 100755 packages/replace/test/fixtures/form/typescript-declare/_config.js create mode 100755 packages/replace/test/fixtures/form/typescript-declare/input.ts create mode 100755 packages/replace/test/fixtures/form/typescript-declare/output.ts diff --git a/packages/replace/src/index.js b/packages/replace/src/index.js index cd1ff5563..acfb91990 100755 --- a/packages/replace/src/index.js +++ b/packages/replace/src/index.js @@ -74,7 +74,7 @@ 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 lookahead = preventAssignment ? '(?!\\s*(=[^=]|:[^:]))' : ''; const pattern = new RegExp( `${delimiters[0]}(${keys.join('|')})${delimiters[1]}${lookahead}`, 'g' diff --git a/packages/replace/test/fixtures/form/typescript-declare/_config.js b/packages/replace/test/fixtures/form/typescript-declare/_config.js new file mode 100755 index 000000000..859fe5f64 --- /dev/null +++ b/packages/replace/test/fixtures/form/typescript-declare/_config.js @@ -0,0 +1,8 @@ +module.exports = { + description: "doesn't replace lvalue in typescript declare", + isTypescript: true, + options: { + 'NAME': 'replaced', + preventAssignment: true + } +}; diff --git a/packages/replace/test/fixtures/form/typescript-declare/input.ts b/packages/replace/test/fixtures/form/typescript-declare/input.ts new file mode 100755 index 000000000..1e39e3e8c --- /dev/null +++ b/packages/replace/test/fixtures/form/typescript-declare/input.ts @@ -0,0 +1,2 @@ +declare const NAME: string +console.log(NAME) diff --git a/packages/replace/test/fixtures/form/typescript-declare/output.ts b/packages/replace/test/fixtures/form/typescript-declare/output.ts new file mode 100755 index 000000000..ea8a01a4f --- /dev/null +++ b/packages/replace/test/fixtures/form/typescript-declare/output.ts @@ -0,0 +1,3 @@ +// eslint-disable-next-line @typescript-eslint/no-unused-vars +declare const NAME: string +console.log('replaced') diff --git a/packages/replace/test/form.js b/packages/replace/test/form.js index 9685dcc99..287a1d508 100644 --- a/packages/replace/test/form.js +++ b/packages/replace/test/form.js @@ -21,10 +21,11 @@ readdirSync('./fixtures/form').forEach((dir) => { test(`${dir}: ${config.description}`, (t) => { const { transform } = replace(config.options); - const input = readFileSync(`fixtures/form/${dir}/input.js`, 'utf-8'); + const extname = config.isTypescript ? 'ts' : 'js'; + const input = readFileSync(`fixtures/form/${dir}/input.${extname}`, 'utf-8'); return Promise.resolve( - transform.call(transformContext, input, `${__dirname}/fixtures/form/${dir}/input.js`) + transform.call(transformContext, input, `${__dirname}/fixtures/form/${dir}/input.${extname}`) ).then((transformed) => { const actual = (transformed ? transformed.code : input).trim(); t.snapshot(actual); diff --git a/packages/replace/test/snapshots/form.js.md b/packages/replace/test/snapshots/form.js.md index 0f2bae2c6..cc254b516 100644 --- a/packages/replace/test/snapshots/form.js.md +++ b/packages/replace/test/snapshots/form.js.md @@ -86,3 +86,10 @@ Generated by [AVA](https://avajs.dev). especial␊ replaced.doSomething()␊ \`);` + +## typescript-declare: doesn't replace lvalue in typescript declare + +> Snapshot 1 + + `declare const NAME: string␊ + console.log(replaced)` diff --git a/packages/replace/test/snapshots/form.js.snap b/packages/replace/test/snapshots/form.js.snap index 050826daae485371bc3938baaff64c36bcfad148..e907e7442b28b234108504ee7b6f80061beeb5d4 100644 GIT binary patch literal 733 zcmV<30wVoERzV@T zj3LuVDmGAJAsI)IOC~D>p`eWtq*y_7ZRqz+mNCP=4Vt5>u&XgR?DYfHP|z z?tm`EOeiLIj_P%@1iL;Pt|3MXX81f=vQn_AL`zaTEXYEW0V`0gQ>hGdp)iYKcNZeNmsQ+p5y8amw~lxSKP`9AgzoOa)~}|JZI6~% zrRPG;#tmz&z4P>WmjUlQNW&-*#%6%MJwTE|nIseBh92No}ME&Q9A^$J8ez!O4aI)fk~+^soQff8m0&g znxH=5AOL)VSA{N(9n_pv(m*6sQJM6dqIL4U`c^V&CyNj#OS_qxD64 zj7Z`~^B;={00000000Axlud8cFc5}YA%wIyu5e%)sm<;o*)2i>w%r~ozJ%a}D#5K@ zd$Q}+YeyL;yA|TVm48uwDRws5BnqvX)696BdHl}Ux5-=?eocS=qLo(RX2OW%La$KU zDF}n4Lkk(Eig5&0FqI=n4NaCL%~Dz#OTTZ0V3vI!J=%M+=fC~Odr$YDr)^lKNKe>vJgOjgYDWmK?CvbjP>QU)Bz%8&(0k;_DK*d_F-CLOAvO%=}3 zGAR6EjE3WQ5>LXW_Ur1k<4>`jWW$XO=H&rDU@56yqTsRNEO zfuY}TlHKnPW^QnDEijQ9MF>Yn5V$pwbFW1JGr!&h!V&y*+?^)0S5F4N>Vq6qXpvKT zDfMDnvzFTXNT0Xp@Gd)L7)8>$3NRi6l9Jj+LXsbv$T>J@Cs&7DOD)Vg7KZ0upgZ^W zkjZ9_k6Or8q>ucigM8Mec=hsOw1JslA-6#@1aDhyjgnlK>P~?vW!BX0IamX81O|0d zA8^o__(~4Ot}eU8ROdrg!0tyBd8XkS?rhm&?))G zpVflV=pL!#I~D0j;*uLRW;IazMXR)ELay6(`5?SS2k*x{*LhgmBY%arzg9Q?;#N*8 Z-Hq8dv%+$+ha-Qb{{cmy;pPYi000~VQqBMX