Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer-string-slice: remove unsafe autofix for String#substr() #2427

Merged
merged 4 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.