-
-
Notifications
You must be signed in to change notification settings - Fork 353
/
number-literal-case.js
52 lines (45 loc) · 1.02 KB
/
number-literal-case.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
52
'use strict';
const {checkVueTemplate} = require('./utils/rule.js');
const {isNumber, isBigInt} = require('./utils/numeric.js');
const MESSAGE_ID = 'number-literal-case';
const messages = {
[MESSAGE_ID]: 'Invalid number literal casing.',
};
const fix = raw => {
let fixed = raw.toLowerCase();
if (fixed.startsWith('0x')) {
fixed = '0x' + fixed.slice(2).toUpperCase();
}
return fixed;
};
/** @param {import('eslint').Rule.RuleContext} context */
const create = () => ({
Literal(node) {
const {raw} = node;
let fixed = raw;
if (isNumber(node)) {
fixed = fix(raw);
} else if (isBigInt(node)) {
fixed = fix(raw.slice(0, -1)) + 'n';
}
if (raw !== fixed) {
return {
node,
messageId: MESSAGE_ID,
fix: fixer => fixer.replaceText(node, fixed),
};
}
},
});
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
create: checkVueTemplate(create),
meta: {
type: 'suggestion',
docs: {
description: 'Enforce proper case for numeric literals.',
},
fixable: 'code',
messages,
},
};