Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add prefer-exponentiation-operator rule (#185)
Fixes #173
- Loading branch information
1 parent
2283cda
commit 9f7f811
Showing
5 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Prefer the exponentiation operator over `Math.pow()` | ||
|
||
Enforces the use of the [exponentiation operator](http://2ality.com/2016/02/exponentiation-operator.html) over [`Math.pow()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow). | ||
|
||
|
||
## Fail | ||
|
||
```js | ||
Math.pow(2, 4); | ||
``` | ||
|
||
|
||
## Pass | ||
|
||
```js | ||
2 ** 4 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict'; | ||
const getDocsUrl = require('./utils/get-docs-url'); | ||
|
||
const isMathPow = node => { | ||
const {callee} = node; | ||
return ( | ||
callee.type === 'MemberExpression' && | ||
callee.object.type === 'Identifier' && | ||
callee.object.name === 'Math' && | ||
callee.property.type === 'Identifier' && | ||
callee.property.name === 'pow' | ||
); | ||
}; | ||
|
||
const parseArgument = (context, arg) => { | ||
if (arg.type === 'Identifier') { | ||
return arg.name; | ||
} | ||
|
||
return context.getSourceCode().getText(arg); | ||
}; | ||
|
||
const fix = (context, node, fixer) => { | ||
const base = parseArgument(context, node.arguments[0]); | ||
const exponent = parseArgument(context, node.arguments[1]); | ||
|
||
const replacement = `${base} ** ${exponent}`; | ||
|
||
return fixer.replaceText(node, replacement); | ||
} | ||
|
||
const create = context => { | ||
return { | ||
CallExpression(node) { | ||
if (isMathPow(node)) { | ||
context.report({ | ||
node, | ||
message: 'Prefer the exponentiation operator over `Math.pow()`.', | ||
fix: fixer => fix(context, node, fixer), | ||
}); | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
docs: { | ||
url: getDocsUrl(__filename) | ||
}, | ||
fixable: 'code' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import test from 'ava'; | ||
import avaRuleTester from 'eslint-ava-rule-tester'; | ||
import rule from '../rules/prefer-exponentiation-operator'; | ||
|
||
const ruleTester = avaRuleTester(test, { | ||
parserOptions: { | ||
ecmaVersion: 2016 | ||
} | ||
}); | ||
|
||
const message = 'Prefer the exponentiation operator over `Math.pow()`.'; | ||
|
||
ruleTester.run('prefer-exponentiation-operator', rule, { | ||
valid: [ | ||
'a ** b;', | ||
'2 ** 4;', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'const x = Math.pow(2, 4);', | ||
errors: [{message}], | ||
output: 'const x = 2 ** 4;' | ||
}, | ||
{ | ||
code: 'const x = Math.pow(-2, (2 - 4) +0 -0.2);', | ||
errors: [{message}], | ||
output: 'const x = -2 ** (2 - 4) +0 -0.2;' | ||
}, | ||
{ | ||
code: 'const x = Math.pow(Math.pow(2, 4), 8);', | ||
errors: [ | ||
{message, column: 11, line: 1}, | ||
{message, column: 20, line: 1}, | ||
], | ||
output: 'const x = Math.pow(2, 4) ** 8;' | ||
}, | ||
{ | ||
code: 'const x = Math.pow(2, b);', | ||
errors: [{message}], | ||
output: 'const x = 2 ** b;' | ||
}, | ||
{ | ||
code: 'const x = Math.pow(c, 4);', | ||
errors: [{message}], | ||
output: 'const x = c ** 4;' | ||
}, | ||
{ | ||
code: 'const x = Math.pow(foo(), bar());', | ||
errors: [{message}], | ||
output: 'const x = foo() ** bar();' | ||
} | ||
] | ||
}); |