-
-
Notifications
You must be signed in to change notification settings - Fork 384
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
Improve prefer-string-slice
autofix
#2505
Comments
prefer-string-slice
autofixes to not use Math.maxprefer-string-slice
autofixes to not use Math.max
n.b. you should only autofix if the number being substracted is a literal you know to be non-zero.
is usally equal to
but if y is 0 |
Autofixes should be safe, an option shouldn't change that. I think this suggestion would be to improve the autofix of this specific scenario from: - STR.substr(0, STR.length - NUM)
+ STR.slice(0, Math.max(0, STR.length - NUM)) To: - STR.substr(0, STR.length - NUM)
+ STR.slice(0, -NUM) As long as the behavior is always the same. |
Agreed that we shouldn't ever do unsafe autofixes. My original idea was to add an option to skip rather than incorrectly fix. Changing to |
👍 |
prefer-string-slice
autofixes to not use Math.maxprefer-string-slice
autofix
unicorn/prefer-string-slice is good when all the parameters are constants, or when there is only one parameter. But when there are two parameters and they're variables, the autofixes will do things like this:
editsummary = editsummary.substr(0, editsummary.length - 2); // remove trailing comma
->
editsummary = editsummary.slice(0, Math.max(0, editsummary.length - 2)); // remove trailing comma
I find the Math.max pretty hard to read. I'd prefer that it not autofix that particular sub-pattern. Any interest in adding an option to this rule so that it can be configured to skip that particular sub-pattern when autofixing?
The text was updated successfully, but these errors were encountered: