Skip to content

Commit

Permalink
prefer-string-slice: Remove unsafe autofix for String#substr() (#…
Browse files Browse the repository at this point in the history
…2427)

Co-authored-by: fisker <[email protected]>
  • Loading branch information
axetroy and fisker authored Aug 24, 2024
1 parent 7369077 commit 891842d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 24 deletions.
8 changes: 0 additions & 8 deletions rules/prefer-string-slice.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -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();
}

Expand Down
14 changes: 4 additions & 10 deletions test/prefer-string-slice.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
{
Expand All @@ -101,10 +99,6 @@ test({
const length = 123;
"foo".substr(1, length)
`,
output: outdent`
const length = 123;
"foo".slice(1, 1 + length)
`,
errors: errorsSubstr,
},
{
Expand Down Expand Up @@ -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,
},
{
Expand Down Expand Up @@ -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);
`,
],
});
23 changes: 17 additions & 6 deletions test/snapshots/prefer-string-slice.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
`␊
Expand Down Expand Up @@ -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()\`.␊
`
Binary file modified test/snapshots/prefer-string-slice.mjs.snap
Binary file not shown.

0 comments on commit 891842d

Please sign in to comment.