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

unicorn/prefer-string-slice autofix is sometimes a bit hard to read #595

Open
NovemLinguae opened this issue Nov 30, 2024 · 2 comments
Open

Comments

@NovemLinguae
Copy link

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. Thoughts?

@edg2s
Copy link
Member

edg2s commented Dec 2, 2024

Slice supports negative indices: summary.slice( 0, -2 ). It may be possible to have the fixer recognise this specific use case, but that would be an issue for upstream...

@NovemLinguae
Copy link
Author

Thanks. Filed upstream ticket sindresorhus/eslint-plugin-unicorn#2505. Will circle back if they write a patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants