1 | 'use strict';
|
2 | const getDocumentationUrl = require('./utils/get-documentation-url');
|
3 |
|
4 | const fix = (raw, isBigInt) => {
|
5 | let fixed = raw.toLowerCase();
|
6 | if (fixed.startsWith('0x')) {
|
7 | fixed = '0x' + fixed.slice(2).toUpperCase();
|
8 |
|
9 | if (isBigInt) {
|
10 | fixed = fixed.slice(0, -1) + 'n';
|
11 | }
|
12 | }
|
13 |
|
14 | return fixed;
|
15 | };
|
16 |
|
17 | const create = context => {
|
18 | return {
|
19 | Literal: node => {
|
20 | const {value, raw, bigint} = node;
|
21 | const isBigInt = Boolean(bigint);
|
22 |
|
23 | if (typeof value !== 'number' && !isBigInt) {
|
24 | return;
|
25 | }
|
26 |
|
27 | const fixed = fix(raw, isBigInt);
|
28 |
|
29 | if (raw !== fixed) {
|
30 | context.report({
|
31 | node,
|
32 | message: 'Invalid number literal casing.',
|
33 | fix: fixer => fixer.replaceText(node, fixed)
|
34 | });
|
35 | }
|
36 | }
|
37 | };
|
38 | };
|
39 |
|
40 | module.exports = {
|
41 | create,
|
42 | meta: {
|
43 | type: 'suggestion',
|
44 | docs: {
|
45 | url: getDocumentationUrl(__filename)
|
46 | },
|
47 | fixable: 'code'
|
48 | }
|
49 | };
|