From 757ea7766e8f9e41d59c081f2a029a6b8b688449 Mon Sep 17 00:00:00 2001 From: fisker Date: Sat, 19 Nov 2022 17:07:46 +0800 Subject: [PATCH 1/8] `prefer-string-replace-all`: Check pattern even it's already using `.replaceAll` --- rules/prefer-string-replace-all.js | 45 +++++++++++++----- test/prefer-string-replace-all.mjs | 5 +- .../prefer-string-replace-all.mjs.md | 32 +++++++++++++ .../prefer-string-replace-all.mjs.snap | Bin 1628 -> 1771 bytes 4 files changed, 69 insertions(+), 13 deletions(-) diff --git a/rules/prefer-string-replace-all.js b/rules/prefer-string-replace-all.js index 09a22a617a..a302d55798 100644 --- a/rules/prefer-string-replace-all.js +++ b/rules/prefer-string-replace-all.js @@ -5,17 +5,19 @@ const escapeString = require('./utils/escape-string.js'); const {methodCallSelector} = require('./selectors/index.js'); const {isRegexLiteral, isNewExpression} = require('./ast/index.js'); -const MESSAGE_ID = 'prefer-string-replace-all'; +const MESSAGE_ID_USE_REPLACE_ALL = 'method'; +const MESSAGE_ID_USE_STRING = 'pattern'; const messages = { - [MESSAGE_ID]: 'Prefer `String#replaceAll()` over `String#replace()`.', + [MESSAGE_ID_USE_REPLACE_ALL]: 'Prefer `String#replaceAll()` over `String#replace()`.', + [MESSAGE_ID_USE_STRING]: 'This pattern can be replaced with a string {{replacement}}.', }; const selector = methodCallSelector({ - method: 'replace', + methods: ['replace', 'replaceAll'], argumentsLength: 2, }); -function * convertRegExpToString(node, fixer) { +function getPatternReplacement(node) { if (!isRegexLiteral(node)) { return; } @@ -39,7 +41,7 @@ function * convertRegExpToString(node, fixer) { // TODO: Preserve escape const string = String.fromCodePoint(...parts.map(part => part.codePoint)); - yield fixer.replaceText(node, escapeString(string)); + return escapeString(string); } const isRegExpWithGlobalFlag = (node, scope) => { @@ -82,14 +84,35 @@ const create = context => ({ return; } + const methodName = property.name; + const patternReplacement = getPatternReplacement(pattern); + if (methodName !== 'replaceAll') { + return { + node: property, + messageId: MESSAGE_ID_USE_REPLACE_ALL, + /** @param {import('eslint').Rule.RuleFixer} fixer */ + * fix(fixer) { + yield fixer.insertTextAfter(property, 'All'); + + if (!patternReplacement) { + return; + } + + yield fixer.replaceText(pattern, patternReplacement); + }, + }; + } + + if (!patternReplacement) { + return; + } + return { - node: property, - messageId: MESSAGE_ID, + node: pattern, + messageId: MESSAGE_ID_USE_STRING, + data: {replacement: patternReplacement}, /** @param {import('eslint').Rule.RuleFixer} fixer */ - * fix(fixer) { - yield fixer.insertTextAfter(property, 'All'); - yield * convertRegExpToString(pattern, fixer); - }, + fix: fixer => fixer.replaceText(pattern, patternReplacement), }; }, }); diff --git a/test/prefer-string-replace-all.mjs b/test/prefer-string-replace-all.mjs index 508a672a16..a4aaed85e2 100644 --- a/test/prefer-string-replace-all.mjs +++ b/test/prefer-string-replace-all.mjs @@ -36,8 +36,6 @@ test.snapshot({ 'const pattern = "not-a-regexp"; foo.replace(pattern, bar)', 'const pattern = new RegExp("foo", "i"); foo.replace(pattern, bar)', 'foo.replace(new NotRegExp("foo", "g"), bar)', - // We are not checking this - 'foo.replaceAll(/a/g, bar)', ], invalid: [ 'foo.replace(/a/g, bar)', @@ -90,5 +88,8 @@ test.snapshot({ 'foo.replace(/\\u{1f600}/gu, _)', 'foo.replace(/\\n/g, _)', 'foo.replace(/\\u{20}/gu, _)', + + 'foo.replaceAll(/a]/g, _)', + 'foo.replaceAll(/\\r\\n\\u{1f600}/gu, _)', ], }); diff --git a/test/snapshots/prefer-string-replace-all.mjs.md b/test/snapshots/prefer-string-replace-all.mjs.md index a10963392c..4b6ee4661c 100644 --- a/test/snapshots/prefer-string-replace-all.mjs.md +++ b/test/snapshots/prefer-string-replace-all.mjs.md @@ -565,3 +565,35 @@ Generated by [AVA](https://avajs.dev). > 1 | foo.replace(/\\u{20}/gu, _)␊ | ^^^^^^^ Prefer \`String#replaceAll()\` over \`String#replace()\`.␊ ` + +## Invalid #35 + 1 | foo.replaceAll(/a]/g, _) + +> Output + + `␊ + 1 | foo.replaceAll('a]', _)␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo.replaceAll(/a]/g, _)␊ + | ^^^^^ This pattern can be replaced with a string 'a]'.␊ + ` + +## Invalid #36 + 1 | foo.replaceAll(/\r\n\u{1f600}/gu, _) + +> Output + + `␊ + 1 | foo.replaceAll('\\r\\n😀', _)␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo.replaceAll(/\\r\\n\\u{1f600}/gu, _)␊ + | ^^^^^^^^^^^^^^^^^ This pattern can be replaced with a string '\\r\\n😀'.␊ + ` diff --git a/test/snapshots/prefer-string-replace-all.mjs.snap b/test/snapshots/prefer-string-replace-all.mjs.snap index 2acc7296e3e0e290bff534022038dbfa90de658d..73826ce46518c23dd2a3d12d574b9cae63f382eb 100644 GIT binary patch literal 1771 zcmV0RzVfAET9k*MMXKZ6cvq_xQi_7vU2P~V3#8x9%Vc^5(E!| zhz1WL!7C*iB_tRz7M6HKtY|{w5j8QQ!K)0BN>6VK3(O!G2c4?7HTC-U%30KpBs-lo{RWKW&<%hm*5_P+3$~kS(21K+bF3fRqeOl4r126n;uyR7E88N zAMo5;(7Wp1^^@U&Du~$%0#BsX`g7kyE2g`S`Eh2R@8ZY}X%Mpu32G52$!9vj`# zs9yf)Eetz{;3Q)~Z?p@o&%5f?e^z#}C=;!PVV@90bq8Sblo5liG4+~X2%nJg)q%VT?p4dd~#)!c${Nd{CSAkYXm`N z$l{F6ym3EVUmd;fqPXynHE1miyOKa)4uCA8(%Q~GJFtYl5qTcmjDVP(PvFo40Qv1J z|7sgxj_s8&^U~DDsSvX>2~Hqr>cz4XgX~L4|0)YgN6jNHPb^ zSmkF6F?*XJxHkZ6t`!U`Jhb`gw)`=-Oybm9h}jH+ZhZjA4i@xPn1+|{s97@cLf$bS zh*_GT5MiUIOmj3Z3TucC-CSCz;4%~ zb^S)}IIz#exHj9n3P8*z5IjRDni_D)B_7^s*yK<$)GcQV#H^#09t8+RYe$#Ov)w!7 z`(u}aG-`m>!m!l@ldJ*wI#svpVp`qq8~eW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mdW^SIR12v^u|teZ7N0g zGiSf@%;)wrues0B+NBF0->05`H(bc?-Rn)RB5zI~Yq Date: Sat, 19 Nov 2022 17:09:37 +0800 Subject: [PATCH 2/8] Update docs --- docs/rules/prefer-string-replace-all.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/rules/prefer-string-replace-all.md b/docs/rules/prefer-string-replace-all.md index 63f6580ffb..ea147892d5 100644 --- a/docs/rules/prefer-string-replace-all.md +++ b/docs/rules/prefer-string-replace-all.md @@ -27,6 +27,10 @@ string.replace(/\(It also checks for escaped regex symbols\)/g, ''); string.replace(/Works for u flag too/gu, ''); ``` +```js +string.replaceAll(/foo/g, 'bar'); +``` + ## Pass ```js @@ -44,3 +48,7 @@ string.replaceAll('string', ''); ```js string.replaceAll(/\s/, ''); ``` + +```js +string.replaceAll('foo', 'bar'); +``` From b6cc11f726000826b40a9ba4921b1976aa4f9490 Mon Sep 17 00:00:00 2001 From: fisker Date: Sat, 19 Nov 2022 17:12:53 +0800 Subject: [PATCH 3/8] More tests --- test/prefer-string-replace-all.mjs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/prefer-string-replace-all.mjs b/test/prefer-string-replace-all.mjs index a4aaed85e2..b993b3ecf6 100644 --- a/test/prefer-string-replace-all.mjs +++ b/test/prefer-string-replace-all.mjs @@ -7,25 +7,36 @@ test.snapshot({ valid: [ // No global flag 'foo.replace(/a/, bar)', + 'foo.replaceAll(/a/, bar)', // Not regex literal 'foo.replace("string", bar)', + 'foo.replaceAll("string", bar)', // Not 2 arguments 'foo.replace(/a/g)', + 'foo.replaceAll(/a/g)', 'foo.replace(/\\\\./g)', + 'foo.replaceAll(/\\\\./g)', // Not `CallExpression` 'new foo.replace(/a/g, bar)', + 'new foo.replaceAll(/a/g, bar)', // Not `MemberExpression` 'replace(/a/g, bar)', + 'replaceAll(/a/g, bar)', // Computed 'foo[replace](/a/g, bar);', + 'foo[replaceAll](/a/g, bar);', // Not replace 'foo.methodNotReplace(/a/g, bar);', // `callee.property` is not a `Identifier` 'foo[\'replace\'](/a/g, bar)', + 'foo[\'replaceAll\'](/a/g, bar)', // More or less argument(s) 'foo.replace(/a/g, bar, extra);', + 'foo.replaceAll(/a/g, bar, extra);', 'foo.replace();', + 'foo.replaceAll();', 'foo.replace(...argumentsArray, ...argumentsArray2)', + 'foo.replaceAll(...argumentsArray, ...argumentsArray2)', // Unknown/non-regexp/non-global value 'foo.replace(unknown, bar)', 'const pattern = new RegExp("foo", unknown); foo.replace(pattern, bar)', From 03ce16d5ebd0e14f1a5fd06186f882e07ded4657 Mon Sep 17 00:00:00 2001 From: fisker Date: Sat, 19 Nov 2022 17:30:50 +0800 Subject: [PATCH 4/8] Reduce diff --- rules/prefer-string-replace-all.js | 38 ++++++++++++++---------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/rules/prefer-string-replace-all.js b/rules/prefer-string-replace-all.js index a302d55798..72765e9c06 100644 --- a/rules/prefer-string-replace-all.js +++ b/rules/prefer-string-replace-all.js @@ -86,33 +86,31 @@ const create = context => ({ const methodName = property.name; const patternReplacement = getPatternReplacement(pattern); - if (methodName !== 'replaceAll') { - return { - node: property, - messageId: MESSAGE_ID_USE_REPLACE_ALL, - /** @param {import('eslint').Rule.RuleFixer} fixer */ - * fix(fixer) { - yield fixer.insertTextAfter(property, 'All'); - if (!patternReplacement) { - return; - } - yield fixer.replaceText(pattern, patternReplacement); - }, + if (methodName === 'replaceAll' && patternReplacement) { + return { + node: pattern, + messageId: MESSAGE_ID_USE_STRING, + data: {replacement: patternReplacement}, + /** @param {import('eslint').Rule.RuleFixer} fixer */ + fix: fixer => fixer.replaceText(pattern, patternReplacement), }; } - if (!patternReplacement) { - return; - } - return { - node: pattern, - messageId: MESSAGE_ID_USE_STRING, - data: {replacement: patternReplacement}, + node: property, + messageId: MESSAGE_ID_USE_REPLACE_ALL, /** @param {import('eslint').Rule.RuleFixer} fixer */ - fix: fixer => fixer.replaceText(pattern, patternReplacement), + * fix(fixer) { + yield fixer.insertTextAfter(property, 'All'); + + if (!patternReplacement) { + return; + } + + yield fixer.replaceText(pattern, patternReplacement); + }, }; }, }); From c8f061d2095257ce79c45f0dc9fcce8396cdcff3 Mon Sep 17 00:00:00 2001 From: fisker Date: Sat, 19 Nov 2022 17:34:12 +0800 Subject: [PATCH 5/8] Don't should long strings in message --- rules/prefer-string-replace-all.js | 22 +++++++++++------- test/prefer-string-replace-all.mjs | 1 + .../prefer-string-replace-all.mjs.md | 16 +++++++++++++ .../prefer-string-replace-all.mjs.snap | Bin 1771 -> 1837 bytes 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/rules/prefer-string-replace-all.js b/rules/prefer-string-replace-all.js index 72765e9c06..caf6015b58 100644 --- a/rules/prefer-string-replace-all.js +++ b/rules/prefer-string-replace-all.js @@ -88,14 +88,20 @@ const create = context => ({ const patternReplacement = getPatternReplacement(pattern); - if (methodName === 'replaceAll' && patternReplacement) { - return { - node: pattern, - messageId: MESSAGE_ID_USE_STRING, - data: {replacement: patternReplacement}, - /** @param {import('eslint').Rule.RuleFixer} fixer */ - fix: fixer => fixer.replaceText(pattern, patternReplacement), - }; + if (methodName === 'replaceAll') { + if (patternReplacement) { + return { + node: pattern, + messageId: MESSAGE_ID_USE_STRING, + data: { + replacement: patternReplacement.length < 20 ? patternReplacement : 'literal', + }, + /** @param {import('eslint').Rule.RuleFixer} fixer */ + fix: fixer => fixer.replaceText(pattern, patternReplacement), + }; + } + + return; } return { diff --git a/test/prefer-string-replace-all.mjs b/test/prefer-string-replace-all.mjs index b993b3ecf6..1ac58c935d 100644 --- a/test/prefer-string-replace-all.mjs +++ b/test/prefer-string-replace-all.mjs @@ -102,5 +102,6 @@ test.snapshot({ 'foo.replaceAll(/a]/g, _)', 'foo.replaceAll(/\\r\\n\\u{1f600}/gu, _)', + `foo.replaceAll(/a${' very'.repeat(30)} string/g, _)`, ], }); diff --git a/test/snapshots/prefer-string-replace-all.mjs.md b/test/snapshots/prefer-string-replace-all.mjs.md index 4b6ee4661c..1bc94d70d9 100644 --- a/test/snapshots/prefer-string-replace-all.mjs.md +++ b/test/snapshots/prefer-string-replace-all.mjs.md @@ -597,3 +597,19 @@ Generated by [AVA](https://avajs.dev). > 1 | foo.replaceAll(/\\r\\n\\u{1f600}/gu, _)␊ | ^^^^^^^^^^^^^^^^^ This pattern can be replaced with a string '\\r\\n😀'.␊ ` + +## Invalid #37 + 1 | foo.replaceAll(/a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very string/g, _) + +> Output + + `␊ + 1 | foo.replaceAll('a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very string', _)␊ + ` + +> Error 1/1 + + `␊ + > 1 | foo.replaceAll(/a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very string/g, _)␊ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This pattern can be replaced with a string literal.␊ + ` diff --git a/test/snapshots/prefer-string-replace-all.mjs.snap b/test/snapshots/prefer-string-replace-all.mjs.snap index 73826ce46518c23dd2a3d12d574b9cae63f382eb..1ba0cdf437e4f456ab08735b2ac49e18452cc802 100644 GIT binary patch literal 1837 zcmV+|2h#XKRzV;)O(|wyQ1b7d@`jDOZGH<^9H}Bhj|NZ~B`>-xiGG=K9T#T$cthJC~pufs%Zo7krdCV9daD*xZSg-rt|jA6l41WkJmDClHwe;5Nu(@8|m!CrUJbT19mW zKyP8#nFOa83wph6*rVKQ-hHQK7mG5{Tp0E_L6jK)<0lLqWF1#-YWH_ex<%YMONiMV zf*$6`BFAa7zaPozX)1iKLCpFPe2p-vzor1LdGh>X)3%4(t1!<}zJ!=;o2VvwdyHLCnSz+(Xzp zuw0y0l+<;5#G>ss7b`!8m>t;DfGmW9NS#%Wpt3nt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nd{CSAkYXm`N z$l{F6ym3EVUmd;fqPXynHE1miyOKa)4uCA8(%Q~GJFtYl5qTcmjDVP(PvFo40Qv1J z|7sgxj_s8&^U~DDsSvX>2~Hqr>cz4XgX~L4|0)YgN6jNHPb^ zSmkF6F?*XJxHkZ6t`!U`Jhb`gw)`=-Oybm9h}jH+ZhZjA4i@xPn1+|{s97@cLf$bS zh*_GT5MiUIOmj3Z3TucC-CSCz;4%~ zb^S)}IIz#exHj9n3P8*z5IjRDni_D)B_7^s*yK<$)GcQV#H^#09t8+RYe$#Ov)w!7 z`(u}aG-`m>!m!l@ldJ*wI#svpVp`qq8~eW4kTbCO8N_S?!F`1C#63ezZuTB>H_}<$ z@Muvk#O&aHdSoF~B%Gd7Rjw`_(Ky#4^?phY#O!f`G5t||RO+K<^})53Da+&YRzJvq zm{k#6MflU@u&MXHv}XYsae)H=(4G*pmIL%yiSVexVSi9GaOU)g`-zc}OISX_XF7ZO#~jE4cMV z%~=#4N5#msT4k(;Qphz_sFEVjUPvW`YZp;+Dh@k`YSz{=j|3FQT2jy=kOx|=T*a2_ z(iA4wu`wU1E)cRyq~+tA-uq&a2FARI9Kzv8xvXtA2%76En@ny z5WQB|5tXcwZn`TU-}-r_3qx?~UiL7;8?BRR^H;e@WOQ^~F@L1#Z|C%e9$4-j2=C(4w8 z^&3#|S^ffQjyJDWpYudZt5`qhxLOmhUvpeBCp));F*mgC(sKs$Z)=B3reeBO5%G#Q N(0_O(`}Uz7000H(SfKy_ From bd6a03d47b521b20270d4c174e977286b76049f8 Mon Sep 17 00:00:00 2001 From: fisker Date: Sat, 19 Nov 2022 17:34:57 +0800 Subject: [PATCH 6/8] Reduce indention level --- rules/prefer-string-replace-all.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/rules/prefer-string-replace-all.js b/rules/prefer-string-replace-all.js index caf6015b58..9f180e6cc1 100644 --- a/rules/prefer-string-replace-all.js +++ b/rules/prefer-string-replace-all.js @@ -89,19 +89,19 @@ const create = context => ({ if (methodName === 'replaceAll') { - if (patternReplacement) { - return { - node: pattern, - messageId: MESSAGE_ID_USE_STRING, - data: { - replacement: patternReplacement.length < 20 ? patternReplacement : 'literal', - }, - /** @param {import('eslint').Rule.RuleFixer} fixer */ - fix: fixer => fixer.replaceText(pattern, patternReplacement), - }; + if (!patternReplacement) { + return; } - return; + return { + node: pattern, + messageId: MESSAGE_ID_USE_STRING, + data: { + replacement: patternReplacement.length < 20 ? patternReplacement : 'literal', + }, + /** @param {import('eslint').Rule.RuleFixer} fixer */ + fix: fixer => fixer.replaceText(pattern, patternReplacement), + }; } return { From f958889f2fb29c4d05184f1bcbbb8c47634b93e8 Mon Sep 17 00:00:00 2001 From: fisker Date: Sat, 19 Nov 2022 17:36:43 +0800 Subject: [PATCH 7/8] Linting --- rules/prefer-string-replace-all.js | 1 - 1 file changed, 1 deletion(-) diff --git a/rules/prefer-string-replace-all.js b/rules/prefer-string-replace-all.js index 9f180e6cc1..2a335a9303 100644 --- a/rules/prefer-string-replace-all.js +++ b/rules/prefer-string-replace-all.js @@ -87,7 +87,6 @@ const create = context => ({ const methodName = property.name; const patternReplacement = getPatternReplacement(pattern); - if (methodName === 'replaceAll') { if (!patternReplacement) { return; From 005b0b7b724575d1e0941ae5ba2ebd10731276b9 Mon Sep 17 00:00:00 2001 From: fisker Date: Sun, 20 Nov 2022 23:35:26 +0800 Subject: [PATCH 8/8] Add comment --- rules/prefer-string-replace-all.js | 1 + 1 file changed, 1 insertion(+) diff --git a/rules/prefer-string-replace-all.js b/rules/prefer-string-replace-all.js index 2a335a9303..5e80f9bc35 100644 --- a/rules/prefer-string-replace-all.js +++ b/rules/prefer-string-replace-all.js @@ -96,6 +96,7 @@ const create = context => ({ node: pattern, messageId: MESSAGE_ID_USE_STRING, data: { + // Show `This pattern can be replaced with a string literal.` for long strings replacement: patternReplacement.length < 20 ? patternReplacement : 'literal', }, /** @param {import('eslint').Rule.RuleFixer} fixer */