Incorrect autofix for prefer-math-trunc rule
See original GitHub issueeslint-plugin-unicorn@23.0.0
Before:
// Very basic hash implementation that mimics Java's hash function, used for generating unique ids for aggregate items
// Found here: https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery
export function generateHash(input) {
let hash = 0;
for (let i = 0; i < input.length; i++) {
const char = input.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash |= 0; // Convert to 32bit integer
}
return hash;
}
Example result (correct):
generateHash('abc123;')
-1207861333
After autofix by unicorn/prefer-math-trunc:
// Very basic hash implementation that mimics Java's hash function, used for generating unique ids for aggregate items
// Found here: https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery
export function generateHash(input) {
let hash = 0;
for (let i = 0; i < input.length; i++) {
const char = input.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash = Math.trunc(hash); // Convert to 32bit integer
}
return hash;
}
Example result (after autofix, incorrect):
generateHash('abc123;')
3087105963
Looks related to #862.
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (6 by maintainers)
Top Results From Across the Web
Testing autofix behavior of SAST rules - r2c
Testing autofix behavior of SAST rules. Automatically test the autofix behavior of custom Semgrep rules. Pieter De Cremer.
Read more >sindresorhus/eslint-plugin-unicorn (Raised $2856.00)
Incorrect autofix for prefer-math-trunc rule. Unfunded#911created bybmish. $0.00. prevent-abbreviations: Fix objects must not be overlapped in a report.
Read more >eslint-plugin-unicorn: Versions - Openbase
Fix expiring-todo-comments crashing on invalid package version (#494) fd46adc; Fix invalid autofix with array destructuring in no-for-loops rule (#489) ...
Read more >Auto-fix and format your JavaScript with ESLint - IBM Developer
Get the quick tips and tricks you need to use ESLint to autofix and format your JavaScript.
Read more >Any solution to autofix or autocorrect ESLint rules?
If you run eslint with the --fix flag it will try to fix any linting changes that it can, and let you know...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fixed in https://github.com/sindresorhus/eslint-plugin-unicorn/releases/tag/v26.0.1
Yes, it also does it for
~~x
,x >> 0
,x << 0
andx ^ 0
.So I’ve done the fix locally (only for
| 0
), but I’m not sure how to update the test. I’ll open the PR anyway and we’ll figure this out there!