forked from eslint/eslint
/
no-loss-of-precision.js
273 lines (261 loc) · 10.4 KB
/
no-loss-of-precision.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
*@fileoverview Tests for no-loss-of-precision rule.
*@author Jacob Moore
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/no-loss-of-precision"),
{ RuleTester } = require("../../../lib/rule-tester");
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
const ruleTester = new RuleTester();
ruleTester.run("no-loss-of-precision", rule, {
valid: [
"var x = 12345",
"var x = 123.456",
"var x = -123.456",
"var x = -123456",
"var x = 123e34",
"var x = 123.0e34",
"var x = 123e-34",
"var x = -123e34",
"var x = -123e-34",
"var x = 12.3e34",
"var x = 12.3e-34",
"var x = -12.3e34",
"var x = -12.3e-34",
"var x = 12300000000000000000000000",
"var x = -12300000000000000000000000",
"var x = 0.00000000000000000000000123",
"var x = -0.00000000000000000000000123",
"var x = 9007199254740991",
"var x = 0",
"var x = 0.0",
"var x = 0.000000000000000000000000000000000000000000000000000000000000000000000000000000",
"var x = -0",
"var x = 123.0000000000000000000000",
"var x = 019.5",
"var x = 0195",
"var x = 0e5",
{ code: "var x = 12_34_56", parserOptions: { ecmaVersion: 2021 } },
{ code: "var x = 12_3.4_56", parserOptions: { ecmaVersion: 2021 } },
{ code: "var x = -12_3.4_56", parserOptions: { ecmaVersion: 2021 } },
{ code: "var x = -12_34_56", parserOptions: { ecmaVersion: 2021 } },
{ code: "var x = 12_3e3_4", parserOptions: { ecmaVersion: 2021 } },
{ code: "var x = 123.0e3_4", parserOptions: { ecmaVersion: 2021 } },
{ code: "var x = 12_3e-3_4", parserOptions: { ecmaVersion: 2021 } },
{ code: "var x = 12_3.0e-3_4", parserOptions: { ecmaVersion: 2021 } },
{ code: "var x = -1_23e-3_4", parserOptions: { ecmaVersion: 2021 } },
{ code: "var x = -1_23.8e-3_4", parserOptions: { ecmaVersion: 2021 } },
{ code: "var x = 1_230000000_00000000_00000_000", parserOptions: { ecmaVersion: 2021 } },
{ code: "var x = -1_230000000_00000000_00000_000", parserOptions: { ecmaVersion: 2021 } },
{ code: "var x = 0.0_00_000000000_000000000_00123", parserOptions: { ecmaVersion: 2021 } },
{ code: "var x = -0.0_00_000000000_000000000_00123", parserOptions: { ecmaVersion: 2021 } },
{ code: "var x = 0e5_3", parserOptions: { ecmaVersion: 2021 } },
{ code: "var x = 0b11111111111111111111111111111111111111111111111111111", parserOptions: { ecmaVersion: 6 } },
{ code: "var x = 0b111_111_111_111_1111_11111_111_11111_1111111111_11111111_111_111", parserOptions: { ecmaVersion: 2021 } },
{ code: "var x = 0B11111111111111111111111111111111111111111111111111111", parserOptions: { ecmaVersion: 6 } },
{ code: "var x = 0B111_111_111_111_1111_11111_111_11111_1111111111_11111111_111_111", parserOptions: { ecmaVersion: 2021 } },
{ code: "var x = 0o377777777777777777", parserOptions: { ecmaVersion: 6 } },
{ code: "var x = 0o3_77_777_777_777_777_777", parserOptions: { ecmaVersion: 2021 } },
{ code: "var x = 0O377777777777777777", parserOptions: { ecmaVersion: 6 } },
"var x = 0377777777777777777",
"var x = 0x1FFFFFFFFFFFFF",
"var x = 0X1FFFFFFFFFFFFF",
"var x = true",
"var x = 'abc'",
"var x = ''",
"var x = null",
"var x = undefined",
"var x = {}",
"var x = ['a', 'b']",
"var x = new Date()",
"var x = '9007199254740993'",
{ code: "var x = 0x1FFF_FFFF_FFF_FFF", parserOptions: { ecmaVersion: 2021 } },
{ code: "var x = 0X1_FFF_FFFF_FFF_FFF", parserOptions: { ecmaVersion: 2021 } }
],
invalid: [
{
code: "var x = 9007199254740993",
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 9007199254740.993e3",
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 9.007199254740993e15",
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = -9007199254740993",
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 900719.9254740994",
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = -900719.9254740994",
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 900719925474099_3",
parserOptions: { ecmaVersion: 2021 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 90_0719925_4740.9_93e3",
parserOptions: { ecmaVersion: 2021 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 9.0_0719925_474099_3e15",
parserOptions: { ecmaVersion: 2021 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = -9_00719_9254_740993",
parserOptions: { ecmaVersion: 2021 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 900_719.92_54740_994",
parserOptions: { ecmaVersion: 2021 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = -900_719.92_5474_0994",
parserOptions: { ecmaVersion: 2021 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 5123000000000000000000000000001",
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = -5123000000000000000000000000001",
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 1230000000000000000000000.0",
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 1.0000000000000000000000123",
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 17498005798264095394980017816940970922825355447145699491406164851279623993595007385788105416184430592",
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 2e999",
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = .1230000000000000000000000",
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 0b100000000000000000000000000000000000000000000000000001",
parserOptions: { ecmaVersion: 6 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 0B100000000000000000000000000000000000000000000000000001",
parserOptions: { ecmaVersion: 6 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 0o400000000000000001",
parserOptions: { ecmaVersion: 6 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 0O400000000000000001",
parserOptions: { ecmaVersion: 6 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 0400000000000000001",
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 0x20000000000001",
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 0X20000000000001",
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 5123_00000000000000000000000000_1",
parserOptions: { ecmaVersion: 2021 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = -5_12300000000000000000000_0000001",
parserOptions: { ecmaVersion: 2021 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 123_00000000000000000000_00.0_0",
parserOptions: { ecmaVersion: 2021 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 1.0_00000000000000000_0000123",
parserOptions: { ecmaVersion: 2021 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 174_980057982_640953949800178169_409709228253554471456994_914061648512796239935950073857881054_1618443059_2",
parserOptions: { ecmaVersion: 2021 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 2e9_99",
parserOptions: { ecmaVersion: 2021 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = .1_23000000000000_00000_0000_0",
parserOptions: { ecmaVersion: 2021 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 0b1_0000000000000000000000000000000000000000000000000000_1",
parserOptions: { ecmaVersion: 2021 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 0B10000000000_0000000000000000000000000000_000000000000001",
parserOptions: { ecmaVersion: 2021 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 0o4_00000000000000_001",
parserOptions: { ecmaVersion: 2021 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 0O4_0000000000000000_1",
parserOptions: { ecmaVersion: 2021 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 0x2_0000000000001",
parserOptions: { ecmaVersion: 2021 },
errors: [{ messageId: "noLossOfPrecision" }]
},
{
code: "var x = 0X200000_0000000_1",
parserOptions: { ecmaVersion: 2021 },
errors: [{ messageId: "noLossOfPrecision" }]
}
]
});