-
-
Notifications
You must be signed in to change notification settings - Fork 360
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
base: main
Are you sure you want to change the base?
Conversation
prefer-string-slice
: fix autofix from .substr(start, length) when length equals to strings' length
…gth equals to strings' length
rules/prefer-string-slice.js
Outdated
@@ -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'); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
prefer-string-slice
: fix autofix from .substr(start, length) when length equals to strings' lengthprefer-string-slice
: remove unsafe autofix for String#substr()
Let's just remove the autofix not much value to provide a suggestion. |
Fix #2291
When the second argument
length
is equal to the length of the string,it will autofix to
.slice(start, 0)
Now we make to
.slice(start, 0 || undefined)
to align the behavior of.substr(start, length)