question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect autofix for prefer-math-trunc rule

See original GitHub issue

eslint-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:closed
  • Created 3 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
noftalycommented, Jan 12, 2021

I think it’s only for | 0. Are there other cases where the expression is transformed into Math.trunc()? In general, the idea would be that no transformation should return a result different than what it replaces.

Yes, it also does it for ~~x, x >> 0, x << 0 and x ^ 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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found