| 1 |
// Warning should be shown for unreachable statement after return (bug 1151931). |
| 2 |
|
| 3 |
function testWarn(code, lineNumber, columnNumber) { |
| 4 |
enableLastWarning(); |
| 5 |
eval(code); |
| 6 |
var warning = getLastWarning(); |
| 7 |
assertEq(warning !== null, true, "warning should be caught for " + code); |
| 8 |
assertEq(warning.name, "Warning"); |
| 9 |
assertEq(warning.lineNumber, lineNumber); |
| 10 |
assertEq(warning.columnNumber, columnNumber); |
| 11 |
|
| 12 |
clearLastWarning(); |
| 13 |
Reflect.parse(code); |
| 14 |
warning = getLastWarning(); |
| 15 |
assertEq(warning !== null, true, "warning should be caught for " + code); |
| 16 |
assertEq(warning.name, "Warning"); |
| 17 |
// Warning generated by Reflect.parse has line/column number for Reflect.parse |
| 18 |
// itself, not parsed code. |
| 19 |
disableLastWarning(); |
| 20 |
} |
| 21 |
|
| 22 |
function testPass(code) { |
| 23 |
enableLastWarning(); |
| 24 |
eval(code); |
| 25 |
var warning = getLastWarning(); |
| 26 |
assertEq(warning, null, "warning should not be caught for " + code); |
| 27 |
|
| 28 |
clearLastWarning(); |
| 29 |
Reflect.parse(code); |
| 30 |
warning = getLastWarning(); |
| 31 |
assertEq(warning, null, "warning should not be caught for " + code); |
| 32 |
disableLastWarning(); |
| 33 |
} |
| 34 |
|
| 35 |
testPass(` |
| 36 |
function f() { |
| 37 |
return ( |
| 38 |
1 + 2 |
| 39 |
); |
| 40 |
} |
| 41 |
`); |
| 42 |
|
| 43 |
// unary expression |
| 44 |
testWarn(` |
| 45 |
function f() { |
| 46 |
var i = 0; |
| 47 |
return |
| 48 |
++i; |
| 49 |
} |
| 50 |
`, 5, 5); |
| 51 |
testWarn(` |
| 52 |
function f() { |
| 53 |
var i = 0; |
| 54 |
return |
| 55 |
--i; |
| 56 |
} |
| 57 |
`, 5, 5); |
| 58 |
|
| 59 |
// array |
| 60 |
testWarn(` |
| 61 |
function f() { |
| 62 |
return |
| 63 |
[1, 2, 3]; |
| 64 |
} |
| 65 |
`, 4, 5); |
| 66 |
|
| 67 |
// block (object) |
| 68 |
testWarn(` |
| 69 |
function f() { |
| 70 |
return |
| 71 |
{x: 10}; |
| 72 |
} |
| 73 |
`, 4, 5); |
| 74 |
testWarn(` |
| 75 |
function f() { |
| 76 |
return |
| 77 |
{ |
| 78 |
method() |
| 79 |
{ |
| 80 |
f(); |
| 81 |
} |
| 82 |
}; |
| 83 |
} |
| 84 |
`, 4, 3); |
| 85 |
|
| 86 |
// expression in paren |
| 87 |
testWarn(` |
| 88 |
function f() { |
| 89 |
return |
| 90 |
(1 + 2); |
| 91 |
} |
| 92 |
`, 4, 5); |
| 93 |
|
| 94 |
// name |
| 95 |
testWarn(` |
| 96 |
function f() { |
| 97 |
return |
| 98 |
f; |
| 99 |
} |
| 100 |
`, 4, 5); |
| 101 |
|
| 102 |
// binary expression |
| 103 |
testWarn(` |
| 104 |
function f() { |
| 105 |
return |
| 106 |
1 + 2; |
| 107 |
} |
| 108 |
`, 4, 5); |
| 109 |
testWarn(` |
| 110 |
function f() { |
| 111 |
return |
| 112 |
.1 + .2; |
| 113 |
} |
| 114 |
`, 4, 5); |
| 115 |
|
| 116 |
// string |
| 117 |
testWarn(` |
| 118 |
function f() { |
| 119 |
return |
| 120 |
"foo"; |
| 121 |
} |
| 122 |
`, 4, 5); |
| 123 |
testWarn(` |
| 124 |
function f() { |
| 125 |
return |
| 126 |
"use struct"; |
| 127 |
} |
| 128 |
`, 4, 5); |
| 129 |
testWarn(` |
| 130 |
function f() { |
| 131 |
return |
| 132 |
'foo'; |
| 133 |
} |
| 134 |
`, 4, 5); |
| 135 |
|
| 136 |
// template string |
| 137 |
testWarn(` |
| 138 |
function f() { |
| 139 |
return |
| 140 |
\`foo\${1 + 2}\`; |
| 141 |
} |
| 142 |
`, 4, 5); |
| 143 |
testWarn(` |
| 144 |
function f() { |
| 145 |
return |
| 146 |
\`foo\`; |
| 147 |
} |
| 148 |
`, 4, 5); |
| 149 |
|
| 150 |
// RegExp |
| 151 |
testWarn(` |
| 152 |
function f() { |
| 153 |
return |
| 154 |
/foo/; |
| 155 |
} |
| 156 |
`, 4, 5); |
| 157 |
|
| 158 |
// boolean |
| 159 |
testWarn(` |
| 160 |
function f() { |
| 161 |
return |
| 162 |
true; |
| 163 |
} |
| 164 |
`, 4, 5); |
| 165 |
testWarn(` |
| 166 |
function f() { |
| 167 |
return |
| 168 |
false; |
| 169 |
} |
| 170 |
`, 4, 5); |
| 171 |
|
| 172 |
// null |
| 173 |
testWarn(` |
| 174 |
function f() { |
| 175 |
return |
| 176 |
null; |
| 177 |
} |
| 178 |
`, 4, 5); |
| 179 |
|
| 180 |
// this |
| 181 |
testWarn(` |
| 182 |
function f() { |
| 183 |
return |
| 184 |
this; |
| 185 |
} |
| 186 |
`, 4, 5); |
| 187 |
|
| 188 |
// new |
| 189 |
testWarn(` |
| 190 |
function f() { |
| 191 |
return |
| 192 |
new Array(); |
| 193 |
} |
| 194 |
`, 4, 5); |
| 195 |
|
| 196 |
// delete |
| 197 |
testWarn(` |
| 198 |
function f() { |
| 199 |
var a = {x: 10}; |
| 200 |
return |
| 201 |
delete a.x; |
| 202 |
} |
| 203 |
`, 5, 5); |
| 204 |
|
| 205 |
// yield |
| 206 |
testWarn(` |
| 207 |
function* f() { |
| 208 |
return |
| 209 |
yield 1; |
| 210 |
} |
| 211 |
`, 4, 5); |
| 212 |
|
| 213 |
// class |
| 214 |
testWarn(` |
| 215 |
function f() { |
| 216 |
return |
| 217 |
class A { constructor() {} }; |
| 218 |
} |
| 219 |
`, 4, 5); |
| 220 |
|
| 221 |
// unary expression |
| 222 |
testWarn(` |
| 223 |
function f() { |
| 224 |
return |
| 225 |
+1; |
| 226 |
} |
| 227 |
`, 4, 5); |
| 228 |
testWarn(` |
| 229 |
function f() { |
| 230 |
return |
| 231 |
-1; |
| 232 |
} |
| 233 |
`, 4, 5); |
| 234 |
testWarn(` |
| 235 |
function f() { |
| 236 |
return |
| 237 |
!1; |
| 238 |
} |
| 239 |
`, 4, 5); |
| 240 |
testWarn(` |
| 241 |
function f() { |
| 242 |
return |
| 243 |
~1; |
| 244 |
} |
| 245 |
`, 4, 5); |
| 246 |
|
| 247 |
// eof |
| 248 |
testPass(` |
| 249 |
var f = new Function("return\\n"); |
| 250 |
`); |
| 251 |
|
| 252 |
// empty statement |
| 253 |
testPass(` |
| 254 |
function f() { |
| 255 |
return; |
| 256 |
; // empty statement |
| 257 |
} |
| 258 |
`); |
| 259 |
|
| 260 |
// end of block |
| 261 |
testPass(` |
| 262 |
function f() { |
| 263 |
{ |
| 264 |
return |
| 265 |
} |
| 266 |
} |
| 267 |
`); |
| 268 |
|
| 269 |
// function (hosted) |
| 270 |
testPass(` |
| 271 |
function f() { |
| 272 |
g(); |
| 273 |
return |
| 274 |
function g() {} |
| 275 |
} |
| 276 |
`); |
| 277 |
|
| 278 |
// if |
| 279 |
testWarn(` |
| 280 |
function f() { |
| 281 |
return |
| 282 |
if (true) |
| 283 |
1 + 2; |
| 284 |
} |
| 285 |
`, 4, 3); |
| 286 |
|
| 287 |
// else |
| 288 |
testPass(` |
| 289 |
function f() { |
| 290 |
if (true) |
| 291 |
return |
| 292 |
else |
| 293 |
1 + 2; |
| 294 |
} |
| 295 |
`); |
| 296 |
|
| 297 |
// switch |
| 298 |
testWarn(` |
| 299 |
function f() { |
| 300 |
return |
| 301 |
switch (1) { |
| 302 |
case 1: |
| 303 |
break; |
| 304 |
} |
| 305 |
} |
| 306 |
`, 4, 3); |
| 307 |
|
| 308 |
// return in switch |
| 309 |
testWarn(` |
| 310 |
function f() { |
| 311 |
switch (1) { |
| 312 |
case 1: |
| 313 |
return; |
| 314 |
1 + 2; |
| 315 |
break; |
| 316 |
} |
| 317 |
} |
| 318 |
`, 6, 7); |
| 319 |
|
| 320 |
// break in switch |
| 321 |
testPass(` |
| 322 |
function f() { |
| 323 |
switch (1) { |
| 324 |
case 1: |
| 325 |
return; |
| 326 |
break; |
| 327 |
} |
| 328 |
} |
| 329 |
`); |
| 330 |
|
| 331 |
// case |
| 332 |
testPass(` |
| 333 |
function f() { |
| 334 |
switch (1) { |
| 335 |
case 0: |
| 336 |
return |
| 337 |
case 1: |
| 338 |
break; |
| 339 |
} |
| 340 |
} |
| 341 |
`); |
| 342 |
|
| 343 |
// default |
| 344 |
testPass(` |
| 345 |
function f() { |
| 346 |
switch (1) { |
| 347 |
case 0: |
| 348 |
return |
| 349 |
default: |
| 350 |
break; |
| 351 |
} |
| 352 |
} |
| 353 |
`); |
| 354 |
|
| 355 |
// while |
| 356 |
testWarn(` |
| 357 |
function f() { |
| 358 |
return |
| 359 |
while (false) |
| 360 |
1 + 2; |
| 361 |
} |
| 362 |
`, 4, 3); |
| 363 |
testPass(` |
| 364 |
function f() { |
| 365 |
do |
| 366 |
return |
| 367 |
while (false); |
| 368 |
} |
| 369 |
`); |
| 370 |
|
| 371 |
// do |
| 372 |
testWarn(` |
| 373 |
function f() { |
| 374 |
return |
| 375 |
do { |
| 376 |
1 + 2; |
| 377 |
} while (false); |
| 378 |
} |
| 379 |
`, 4, 3); |
| 380 |
|
| 381 |
// for |
| 382 |
testWarn(` |
| 383 |
function f() { |
| 384 |
return |
| 385 |
for (;;) { |
| 386 |
break; |
| 387 |
} |
| 388 |
} |
| 389 |
`, 4, 3); |
| 390 |
|
| 391 |
// break in for |
| 392 |
testPass(` |
| 393 |
function f() { |
| 394 |
for (;;) { |
| 395 |
return |
| 396 |
break; |
| 397 |
} |
| 398 |
} |
| 399 |
`, 5, 5); |
| 400 |
|
| 401 |
// continue |
| 402 |
testWarn(` |
| 403 |
function f() { |
| 404 |
for (;;) { |
| 405 |
return |
| 406 |
continue; |
| 407 |
} |
| 408 |
} |
| 409 |
`, 5, 5); |
| 410 |
|
| 411 |
// var (hosted) |
| 412 |
testPass(` |
| 413 |
function f() { |
| 414 |
return |
| 415 |
var a = 1; |
| 416 |
} |
| 417 |
`); |
| 418 |
|
| 419 |
// const |
| 420 |
testWarn(` |
| 421 |
function f() { |
| 422 |
return |
| 423 |
const a = 1; |
| 424 |
} |
| 425 |
`, 4, 3); |
| 426 |
|
| 427 |
// with |
| 428 |
testWarn(` |
| 429 |
function f() { |
| 430 |
return |
| 431 |
with ({}) { |
| 432 |
1; |
| 433 |
} |
| 434 |
} |
| 435 |
`, 4, 3); |
| 436 |
|
| 437 |
// return |
| 438 |
testWarn(` |
| 439 |
function f() { |
| 440 |
return |
| 441 |
return; |
| 442 |
} |
| 443 |
`, 4, 3); |
| 444 |
|
| 445 |
// try |
| 446 |
testWarn(` |
| 447 |
function f() { |
| 448 |
return |
| 449 |
try { |
| 450 |
} catch (e) { |
| 451 |
} |
| 452 |
} |
| 453 |
`, 4, 3); |
| 454 |
|
| 455 |
// throw |
| 456 |
testPass(` |
| 457 |
function f() { |
| 458 |
return |
| 459 |
throw 1; |
| 460 |
} |
| 461 |
`); |
| 462 |
|
| 463 |
// debugger |
| 464 |
testWarn(` |
| 465 |
function f() { |
| 466 |
return |
| 467 |
debugger; |
| 468 |
} |
| 469 |
`, 4, 3); |
| 470 |
|
| 471 |
// let |
| 472 |
testWarn(` |
| 473 |
function f() { |
| 474 |
return |
| 475 |
let a = 1; |
| 476 |
} |
| 477 |
`, 4, 3); |
| 478 |
|
| 479 |
// skip hoisted |
| 480 |
|
| 481 |
testWarn(` |
| 482 |
function f() { |
| 483 |
return |
| 484 |
var a = 0; |
| 485 |
(1 + 2); |
| 486 |
} |
| 487 |
`, 5, 3); |
| 488 |
|
| 489 |
testWarn(` |
| 490 |
function f() { |
| 491 |
return |
| 492 |
function f() {} |
| 493 |
var a = 0; |
| 494 |
(1 + 2); |
| 495 |
} |
| 496 |
`, 6, 3); |