From 891842d539d0d002119e9c476a050a12d3936a75 Mon Sep 17 00:00:00 2001 From: Axetroy Date: Sat, 24 Aug 2024 16:01:28 +0800 Subject: [PATCH] `prefer-string-slice`: Remove unsafe autofix for `String#substr()` (#2427) Co-authored-by: fisker --- rules/prefer-string-slice.js | 8 ------- test/prefer-string-slice.mjs | 14 ++++-------- test/snapshots/prefer-string-slice.mjs.md | 23 +++++++++++++++----- test/snapshots/prefer-string-slice.mjs.snap | Bin 835 -> 895 bytes 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/rules/prefer-string-slice.js b/rules/prefer-string-slice.js index 386de12f20..422c0c5c11 100644 --- a/rules/prefer-string-slice.js +++ b/rules/prefer-string-slice.js @@ -1,7 +1,6 @@ 'use strict'; const {getStaticValue} = require('@eslint-community/eslint-utils'); const {getParenthesizedText, getParenthesizedRange} = require('./utils/parentheses.js'); -const isNumber = require('./utils/is-number.js'); const {replaceArgument} = require('./fix/index.js'); const {isNumberLiteral, isMethodCall} = require('./ast/index.js'); @@ -64,13 +63,6 @@ function * fixSubstrArguments({node, fixer, context, abort}) { return; } - if (argumentNodes.every(node => isNumber(node, scope))) { - const firstArgumentText = getParenthesizedText(firstArgument, sourceCode); - - yield fixer.insertTextBeforeRange(secondArgumentRange, `${firstArgumentText} + `); - return; - } - return abort(); } diff --git a/test/prefer-string-slice.mjs b/test/prefer-string-slice.mjs index 7776d86c55..b14c30e9dd 100644 --- a/test/prefer-string-slice.mjs +++ b/test/prefer-string-slice.mjs @@ -84,12 +84,10 @@ test({ }, { code: '"foo".substr(bar.length, Math.min(baz, 100))', - output: '"foo".slice(bar.length, bar.length + Math.min(baz, 100))', errors: errorsSubstr, }, { code: '"foo".substr(1, "abc".length)', - output: '"foo".slice(1, 1 + "abc".length)', errors: errorsSubstr, }, { @@ -101,10 +99,6 @@ test({ const length = 123; "foo".substr(1, length) `, - output: outdent` - const length = 123; - "foo".slice(1, 1 + length) - `, errors: errorsSubstr, }, { @@ -140,10 +134,6 @@ test({ const length = 123; "foo".substr(1, length - 4) `, - output: outdent` - const length = 123; - "foo".slice(1, 1 + length - 4) - `, errors: errorsSubstr, }, { @@ -326,5 +316,9 @@ test.snapshot({ 'foo.substring(0, (10, 1))', 'foo.substring(0, await 1)', 'foo.substring((10, bar))', + outdent` + const string = "::"; + const output = string.substr(-2, 2); + `, ], }); diff --git a/test/snapshots/prefer-string-slice.mjs.md b/test/snapshots/prefer-string-slice.mjs.md index 59884263b9..a7875964fd 100644 --- a/test/snapshots/prefer-string-slice.mjs.md +++ b/test/snapshots/prefer-string-slice.mjs.md @@ -122,12 +122,6 @@ Generated by [AVA](https://avajs.dev). 1 | foo.substr((0, bar.length), (0, baz.length))␊ ` -> Output - - `␊ - 1 | foo.slice((0, bar.length), (0, bar.length) + (0, baz.length))␊ - ` - > Error 1/1 `␊ @@ -218,3 +212,20 @@ Generated by [AVA](https://avajs.dev). > 1 | foo.substring((10, bar))␊ | ^^^^^^^^^^^^^^^^^^^^^^^^ Prefer \`String#slice()\` over \`String#substring()\`.␊ ` + +## invalid(11): const string = "::"; const output = string.substr(-2, 2); + +> Input + + `␊ + 1 | const string = "::";␊ + 2 | const output = string.substr(-2, 2);␊ + ` + +> Error 1/1 + + `␊ + 1 | const string = "::";␊ + > 2 | const output = string.substr(-2, 2);␊ + | ^^^^^^^^^^^^^^^^^^^^ Prefer \`String#slice()\` over \`String#substr()\`.␊ + ` diff --git a/test/snapshots/prefer-string-slice.mjs.snap b/test/snapshots/prefer-string-slice.mjs.snap index 3665b0f40998546ff38143c3b1b50714ebf97d9a..3097c237060be07073417e49e441a178d958b788 100644 GIT binary patch literal 895 zcmV-_1AzQNRzV= zmo7Ax7TZ}MiiyD^e}HE=c=n(1-|#;mG1KjIJIrji>@LBc9z);vz4w_nZz$*8w(EC3 zg=ZHwb*O6}|8cxS;yODzMlC260Yk9_y3WipGc9ISpja}PQDsYA-)DA>*$6SS&PQ?NG+t}wk{+vnb16%E zn#(KM`1oyj7ua1pfbI7YyQQc%b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