UNPKG

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