fimbullinter / wotan

Pluggable TypeScript and JavaScript linter
Apache License 2.0
281 stars 23 forks source link

add rule to prefer exponentiation operator #307

Open ajafff opened 6 years ago

ajafff commented 6 years ago

ES2016 added the exponentiation operator, which is a drop-in replacement of Math.pow.

Math.pow(2, 2);
// fixed to
2 ** 2

The fixer needs to add parens if necessary:

Math.pow(2, 2).toString();
// fixed to
(2 ** 2).toString()
ajafff commented 6 years ago

Also needs to add parens to both sides of the operator if necessary:

Math.pos(1 + 1, 2 + 2).toString();
// fixed to
((1 + 1) ** (2 + 2)).toString()
ajafff commented 6 years ago

Turns fixing parens will be a real mess. Maybe this rule should be pessimistic in more involved cases and simply add parens even if not strictly necessary.