/
number-literal-case.js
119 lines (108 loc) · 2.09 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import test from 'ava';
import avaRuleTester from 'eslint-ava-rule-tester';
import {outdent} from 'outdent';
import rule from '../rules/number-literal-case';
const ruleTester = avaRuleTester(test, {
env: {
es6: true
},
parserOptions: {
ecmaVersion: 2020
}
});
const error = {
message: 'Invalid number literal casing.'
};
// TODO: Add numeric separator tests when ESLint supports it.
ruleTester.run('number-literal-case', rule, {
valid: [
// Number
'const foo = 1234',
'const foo = 0777',
'const foo = 0888',
'const foo = 0b10',
'const foo = 0o1234567',
'const foo = 0xABCDEF',
// BigInt
'const foo = 1234n',
'const foo = 0b10n',
'const foo = 0o1234567n',
'const foo = 0xABCDEFn',
// Symbolic value
'const foo = NaN',
'const foo = +Infinity',
'const foo = -Infinity',
// Exponential notation
'const foo = 1.2e3',
'const foo = 1.2e-3',
'const foo = 1.2e+3',
// Not number
'const foo = \'0Xff\'',
'const foo = \'0Xffn\''
],
invalid: [
// Number
{
code: 'const foo = 0B10',
errors: [error],
output: 'const foo = 0b10'
},
{
code: 'const foo = 0O1234567',
errors: [error],
output: 'const foo = 0o1234567'
},
{
code: 'const foo = 0XaBcDeF',
errors: [error],
output: 'const foo = 0xABCDEF'
},
// BigInt
{
code: 'const foo = 0B10n',
errors: [error],
output: 'const foo = 0b10n'
},
{
code: 'const foo = 0O1234567n',
errors: [error],
output: 'const foo = 0o1234567n'
},
{
code: 'const foo = 0XaBcDeFn',
errors: [error],
output: 'const foo = 0xABCDEFn'
},
// Exponential notation
{
code: 'const foo = 1.2E3',
errors: [error],
output: 'const foo = 1.2e3'
},
{
code: 'const foo = 1.2E-3',
errors: [error],
output: 'const foo = 1.2e-3'
},
{
code: 'const foo = 1.2E+3',
errors: [error],
output: 'const foo = 1.2e+3'
},
{
code: outdent`
const foo = 255;
if (foo === 0xff) {
console.log('invalid');
}
`,
errors: [error],
output: outdent`
const foo = 255;
if (foo === 0xFF) {
console.log('invalid');
}
`
}
]
});