]> git.proxmox.com Git - pve-eslint.git/blame - eslint/tests/lib/rules/no-loss-of-precision.js
import 7.12.1 upstream release
[pve-eslint.git] / eslint / tests / lib / rules / no-loss-of-precision.js
CommitLineData
ebb53d86
TL
1/**
2 *@fileoverview Tests for no-loss-of-precision rule.
3 *@author Jacob Moore
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const rule = require("../../../lib/rules/no-loss-of-precision"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15//------------------------------------------------------------------------------
16// Helpers
17//------------------------------------------------------------------------------
18
19const ruleTester = new RuleTester();
20
21ruleTester.run("no-loss-of-precision", rule, {
22 valid: [
23 "var x = 12345",
24 "var x = 123.456",
25 "var x = -123.456",
26 "var x = -123456",
27 "var x = 123e34",
28 "var x = 123.0e34",
29 "var x = 123e-34",
30 "var x = -123e34",
31 "var x = -123e-34",
32 "var x = 12.3e34",
33 "var x = 12.3e-34",
34 "var x = -12.3e34",
35 "var x = -12.3e-34",
36 "var x = 12300000000000000000000000",
37 "var x = -12300000000000000000000000",
38 "var x = 0.00000000000000000000000123",
39 "var x = -0.00000000000000000000000123",
40 "var x = 9007199254740991",
41 "var x = 0",
42 "var x = 0.0",
43 "var x = 0.000000000000000000000000000000000000000000000000000000000000000000000000000000",
44 "var x = -0",
45 "var x = 123.0000000000000000000000",
46 "var x = 019.5",
47 "var x = 0195",
48 "var x = 0e5",
49
6f036462
TL
50 { code: "var x = 12_34_56", parserOptions: { ecmaVersion: 2021 } },
51 { code: "var x = 12_3.4_56", parserOptions: { ecmaVersion: 2021 } },
52 { code: "var x = -12_3.4_56", parserOptions: { ecmaVersion: 2021 } },
53 { code: "var x = -12_34_56", parserOptions: { ecmaVersion: 2021 } },
54 { code: "var x = 12_3e3_4", parserOptions: { ecmaVersion: 2021 } },
55 { code: "var x = 123.0e3_4", parserOptions: { ecmaVersion: 2021 } },
56 { code: "var x = 12_3e-3_4", parserOptions: { ecmaVersion: 2021 } },
57 { code: "var x = 12_3.0e-3_4", parserOptions: { ecmaVersion: 2021 } },
58 { code: "var x = -1_23e-3_4", parserOptions: { ecmaVersion: 2021 } },
59 { code: "var x = -1_23.8e-3_4", parserOptions: { ecmaVersion: 2021 } },
60 { code: "var x = 1_230000000_00000000_00000_000", parserOptions: { ecmaVersion: 2021 } },
61 { code: "var x = -1_230000000_00000000_00000_000", parserOptions: { ecmaVersion: 2021 } },
62 { code: "var x = 0.0_00_000000000_000000000_00123", parserOptions: { ecmaVersion: 2021 } },
63 { code: "var x = -0.0_00_000000000_000000000_00123", parserOptions: { ecmaVersion: 2021 } },
64 { code: "var x = 0e5_3", parserOptions: { ecmaVersion: 2021 } },
ebb53d86
TL
65
66 { code: "var x = 0b11111111111111111111111111111111111111111111111111111", parserOptions: { ecmaVersion: 6 } },
6f036462
TL
67 { code: "var x = 0b111_111_111_111_1111_11111_111_11111_1111111111_11111111_111_111", parserOptions: { ecmaVersion: 2021 } },
68
ebb53d86 69 { code: "var x = 0B11111111111111111111111111111111111111111111111111111", parserOptions: { ecmaVersion: 6 } },
6f036462 70 { code: "var x = 0B111_111_111_111_1111_11111_111_11111_1111111111_11111111_111_111", parserOptions: { ecmaVersion: 2021 } },
ebb53d86
TL
71
72 { code: "var x = 0o377777777777777777", parserOptions: { ecmaVersion: 6 } },
6f036462 73 { code: "var x = 0o3_77_777_777_777_777_777", parserOptions: { ecmaVersion: 2021 } },
ebb53d86 74 { code: "var x = 0O377777777777777777", parserOptions: { ecmaVersion: 6 } },
ebb53d86 75
6f036462 76 "var x = 0377777777777777777",
ebb53d86
TL
77 "var x = 0x1FFFFFFFFFFFFF",
78 "var x = 0X1FFFFFFFFFFFFF",
79 "var x = true",
80 "var x = 'abc'",
81 "var x = ''",
82 "var x = null",
83 "var x = undefined",
84 "var x = {}",
85 "var x = ['a', 'b']",
86 "var x = new Date()",
6f036462 87 "var x = '9007199254740993'",
ebb53d86 88
6f036462
TL
89 { code: "var x = 0x1FFF_FFFF_FFF_FFF", parserOptions: { ecmaVersion: 2021 } },
90 { code: "var x = 0X1_FFF_FFFF_FFF_FFF", parserOptions: { ecmaVersion: 2021 } }
ebb53d86
TL
91 ],
92 invalid: [
93 {
94 code: "var x = 9007199254740993",
95 errors: [{ messageId: "noLossOfPrecision" }]
96 },
97 {
98 code: "var x = 9007199254740.993e3",
99 errors: [{ messageId: "noLossOfPrecision" }]
100 },
101 {
102 code: "var x = 9.007199254740993e15",
103 errors: [{ messageId: "noLossOfPrecision" }]
104 },
105 {
106 code: "var x = -9007199254740993",
107 errors: [{ messageId: "noLossOfPrecision" }]
108 },
109 {
110 code: "var x = 900719.9254740994",
111 errors: [{ messageId: "noLossOfPrecision" }]
112 },
113 {
114 code: "var x = -900719.9254740994",
115 errors: [{ messageId: "noLossOfPrecision" }]
116 },
6f036462
TL
117 {
118 code: "var x = 900719925474099_3",
119 parserOptions: { ecmaVersion: 2021 },
120 errors: [{ messageId: "noLossOfPrecision" }]
121 },
122 {
123 code: "var x = 90_0719925_4740.9_93e3",
124 parserOptions: { ecmaVersion: 2021 },
125 errors: [{ messageId: "noLossOfPrecision" }]
126 },
127 {
128 code: "var x = 9.0_0719925_474099_3e15",
129 parserOptions: { ecmaVersion: 2021 },
130 errors: [{ messageId: "noLossOfPrecision" }]
131 },
132 {
133 code: "var x = -9_00719_9254_740993",
134 parserOptions: { ecmaVersion: 2021 },
135 errors: [{ messageId: "noLossOfPrecision" }]
136 },
137 {
138 code: "var x = 900_719.92_54740_994",
139 parserOptions: { ecmaVersion: 2021 },
140 errors: [{ messageId: "noLossOfPrecision" }]
141 },
142 {
143 code: "var x = -900_719.92_5474_0994",
144 parserOptions: { ecmaVersion: 2021 },
145 errors: [{ messageId: "noLossOfPrecision" }]
146 },
ebb53d86
TL
147 {
148 code: "var x = 5123000000000000000000000000001",
149 errors: [{ messageId: "noLossOfPrecision" }]
150 },
151 {
152 code: "var x = -5123000000000000000000000000001",
153 errors: [{ messageId: "noLossOfPrecision" }]
154 },
155 {
156 code: "var x = 1230000000000000000000000.0",
157 errors: [{ messageId: "noLossOfPrecision" }]
158 },
159 {
160 code: "var x = 1.0000000000000000000000123",
161 errors: [{ messageId: "noLossOfPrecision" }]
162 },
163 {
164 code: "var x = 17498005798264095394980017816940970922825355447145699491406164851279623993595007385788105416184430592",
165 errors: [{ messageId: "noLossOfPrecision" }]
166 },
167 {
168 code: "var x = 2e999",
169 errors: [{ messageId: "noLossOfPrecision" }]
170 },
171 {
172 code: "var x = .1230000000000000000000000",
173 errors: [{ messageId: "noLossOfPrecision" }]
174 },
175 {
176 code: "var x = 0b100000000000000000000000000000000000000000000000000001",
177 parserOptions: { ecmaVersion: 6 },
178 errors: [{ messageId: "noLossOfPrecision" }]
179 },
180 {
181 code: "var x = 0B100000000000000000000000000000000000000000000000000001",
182 parserOptions: { ecmaVersion: 6 },
183 errors: [{ messageId: "noLossOfPrecision" }]
184 },
185 {
186 code: "var x = 0o400000000000000001",
187 parserOptions: { ecmaVersion: 6 },
188 errors: [{ messageId: "noLossOfPrecision" }]
189 },
190 {
191 code: "var x = 0O400000000000000001",
192 parserOptions: { ecmaVersion: 6 },
193 errors: [{ messageId: "noLossOfPrecision" }]
194 },
195 {
196 code: "var x = 0400000000000000001",
197 errors: [{ messageId: "noLossOfPrecision" }]
198 },
199 {
200 code: "var x = 0x20000000000001",
201 errors: [{ messageId: "noLossOfPrecision" }]
202 },
203 {
204 code: "var x = 0X20000000000001",
205 errors: [{ messageId: "noLossOfPrecision" }]
6f036462
TL
206 },
207 {
208 code: "var x = 5123_00000000000000000000000000_1",
209 parserOptions: { ecmaVersion: 2021 },
210 errors: [{ messageId: "noLossOfPrecision" }]
211 },
212 {
213 code: "var x = -5_12300000000000000000000_0000001",
214 parserOptions: { ecmaVersion: 2021 },
215 errors: [{ messageId: "noLossOfPrecision" }]
216 },
217 {
218 code: "var x = 123_00000000000000000000_00.0_0",
219 parserOptions: { ecmaVersion: 2021 },
220 errors: [{ messageId: "noLossOfPrecision" }]
221 },
222 {
223 code: "var x = 1.0_00000000000000000_0000123",
224 parserOptions: { ecmaVersion: 2021 },
225 errors: [{ messageId: "noLossOfPrecision" }]
226 },
227 {
228 code: "var x = 174_980057982_640953949800178169_409709228253554471456994_914061648512796239935950073857881054_1618443059_2",
229 parserOptions: { ecmaVersion: 2021 },
230 errors: [{ messageId: "noLossOfPrecision" }]
231 },
232 {
233 code: "var x = 2e9_99",
234 parserOptions: { ecmaVersion: 2021 },
235 errors: [{ messageId: "noLossOfPrecision" }]
236 },
237 {
238 code: "var x = .1_23000000000000_00000_0000_0",
239 parserOptions: { ecmaVersion: 2021 },
240 errors: [{ messageId: "noLossOfPrecision" }]
241 },
242 {
243 code: "var x = 0b1_0000000000000000000000000000000000000000000000000000_1",
244 parserOptions: { ecmaVersion: 2021 },
245 errors: [{ messageId: "noLossOfPrecision" }]
246 },
247 {
248 code: "var x = 0B10000000000_0000000000000000000000000000_000000000000001",
249 parserOptions: { ecmaVersion: 2021 },
250 errors: [{ messageId: "noLossOfPrecision" }]
251 },
252 {
253 code: "var x = 0o4_00000000000000_001",
254 parserOptions: { ecmaVersion: 2021 },
255 errors: [{ messageId: "noLossOfPrecision" }]
256 },
257 {
258 code: "var x = 0O4_0000000000000000_1",
259 parserOptions: { ecmaVersion: 2021 },
260 errors: [{ messageId: "noLossOfPrecision" }]
261 },
262 {
263 code: "var x = 0x2_0000000000001",
264 parserOptions: { ecmaVersion: 2021 },
265 errors: [{ messageId: "noLossOfPrecision" }]
266 },
267 {
268 code: "var x = 0X200000_0000000_1",
269 parserOptions: { ecmaVersion: 2021 },
270 errors: [{ messageId: "noLossOfPrecision" }]
ebb53d86 271 }
ebb53d86
TL
272 ]
273});