Bug: no-implicit-coercion errors when multiplied by fractions with numerator of 1
See original GitHub issueEnvironment
Node version: N/A npm version: N/A Local ESLint version: 8.24.0 Global ESLint version: N/A Operating System: N/A
What parser are you using?
Default (Espree)
What did you do?
console.log(Math.PI * 1/4)
What did you expect to happen?
This would not be an error since the * 1
isn’t logically part of the previous token but part of the following division.
What actually happened?
This is marked as an implicit conversion error and suggested to be changed to the following which is clearly missing the intent:
console.log(Number(Math.PI)/4)
Participation
- I am willing to submit a pull request for this issue.
Additional comments
This is a special case that seems specific to the following pattern: <number> * 1 / <number>
. While I do realize that the * 1
is unnecessary here, for readability of math expressions it is very valuable such as the example of working with PI
but applies to any number multiplied by a fraction.
I’d like a special case/exception made to the rule’s checks that if the * 1
is immediately followed by a /
then it is not an error.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (4 by maintainers)
I can work on this.
That’s a good point, as
/
coerces its operands to numbers anyway. I’m not opposed to adding this exception (by default, no option) and treating this as a bug fix, but let’s hear from the rest of the team first.