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 3665b0f409..3097c23706 100644 Binary files a/test/snapshots/prefer-string-slice.mjs.snap and b/test/snapshots/prefer-string-slice.mjs.snap differ