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 2 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
3 changes: 2 additions & 1 deletion rules/prefer-string-slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ function * fixSubstrArguments({node, fixer, context, abort}) {
if (argumentNodes.every(node => isNumber(node, scope))) {
const firstArgumentText = getParenthesizedText(firstArgument, sourceCode);

yield fixer.insertTextBeforeRange(secondArgumentRange, `${firstArgumentText} + `);
yield fixer.insertTextBeforeRange(secondArgumentRange, `(${firstArgumentText} + `);
yield fixer.insertTextAfterRange(secondArgumentRange, ') || undefined');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original Math.min() already ugly, let's turn this fix into suggestion, I don't think people will use the fixed code directly.

Copy link
Contributor Author

@axetroy axetroy Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I admit, this is ugly, but I can't found a more elegant way to fix

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's turn this fix into suggestion

Use suggestion instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The source code is too complicated, and I don't have the confidence to refactor it. Close PR

return;
}

Expand Down
12 changes: 8 additions & 4 deletions test/prefer-string-slice.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ test({
},
{
code: '"foo".substr(bar.length, Math.min(baz, 100))',
output: '"foo".slice(bar.length, bar.length + Math.min(baz, 100))',
output: '"foo".slice(bar.length, (bar.length + Math.min(baz, 100)) || undefined)',
errors: errorsSubstr,
},
{
code: '"foo".substr(1, "abc".length)',
output: '"foo".slice(1, 1 + "abc".length)',
output: '"foo".slice(1, (1 + "abc".length) || undefined)',
errors: errorsSubstr,
},
{
Expand All @@ -103,7 +103,7 @@ test({
`,
output: outdent`
const length = 123;
"foo".slice(1, 1 + length)
"foo".slice(1, (1 + length) || undefined)
`,
errors: errorsSubstr,
},
Expand Down Expand Up @@ -142,7 +142,7 @@ test({
`,
output: outdent`
const length = 123;
"foo".slice(1, 1 + length - 4)
"foo".slice(1, (1 + length - 4) || undefined)
`,
errors: errorsSubstr,
},
Expand Down Expand Up @@ -326,5 +326,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);
`,
],
});
26 changes: 25 additions & 1 deletion test/snapshots/prefer-string-slice.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Generated by [AVA](https://avajs.dev).
> Output

`␊
1 | foo.slice((0, bar.length), (0, bar.length) + (0, baz.length))␊
1 | foo.slice((0, bar.length), ((0, bar.length) + (0, baz.length)) || undefined)␊
`

> Error 1/1
Expand Down Expand Up @@ -218,3 +218,27 @@ 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);␊
`

> Output

`␊
1 | const string = "::";␊
2 | const output = string.slice(-2, (-2 + 2) || undefined);␊
`

> 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.
Loading