forked from e18e/eslint-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprefer-exponentiation-operator.ts
More file actions
55 lines (49 loc) · 1.41 KB
/
Copy pathprefer-exponentiation-operator.ts
File metadata and controls
55 lines (49 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import type {Rule} from 'eslint';
import type {CallExpression} from 'estree';
export const preferExponentiationOperator: Rule.RuleModule = {
meta: {
type: 'suggestion',
docs: {
description: 'Prefer the exponentiation operator ** over Math.pow()',
recommended: true
},
fixable: 'code',
schema: [],
messages: {
preferExponentiation: 'Use the ** operator instead of Math.pow()'
}
},
create(context) {
const sourceCode = context.sourceCode;
return {
CallExpression(node: CallExpression) {
if (
node.callee.type !== 'MemberExpression' ||
node.callee.object.type !== 'Identifier' ||
node.callee.object.name !== 'Math' ||
node.callee.property.type !== 'Identifier' ||
node.callee.property.name !== 'pow'
) {
return;
}
const base = node.arguments[0];
const exponent = node.arguments[1];
if (!base || !exponent || node.arguments.length !== 2) {
return;
}
context.report({
node,
messageId: 'preferExponentiation',
fix(fixer) {
const baseText = sourceCode.getText(base);
const exponentText = sourceCode.getText(exponent);
return fixer.replaceText(
node,
`(${baseText}) ** (${exponentText})`
);
}
});
}
};
}
};