/
require-number-to-fixed-digits-argument.js
51 lines (46 loc) · 1.13 KB
/
require-number-to-fixed-digits-argument.js
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
'use strict';
const {methodCallSelector, not} = require('./selectors/index.js');
const {appendArgument} = require('./fix/index.js');
const MESSAGE_ID = 'require-number-to-fixed-digits-argument';
const messages = {
[MESSAGE_ID]: 'Missing the digits argument.',
};
const mathToFixed = [
methodCallSelector({
method: 'toFixed',
argumentsLength: 0,
}),
not('[callee.object.type="NewExpression"]'),
].join('');
/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
const sourceCode = context.getSourceCode();
return {
[mathToFixed](node) {
const [
openingParenthesis,
closingParenthesis,
] = sourceCode.getLastTokens(node, 2);
return {
loc: {
start: openingParenthesis.loc.start,
end: closingParenthesis.loc.end,
},
messageId: MESSAGE_ID,
/** @param {import('eslint').Rule.RuleFixer} fixer */
fix: fixer => appendArgument(fixer, node, '0', sourceCode),
};
},
};
};
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Enforce using the digits argument with `Number#toFixed()`.',
},
fixable: 'code',
messages,
},
};