@@ -1319,24 +1319,94 @@ public void testDefaultUndefinedParameters() {
1319
1319
}
1320
1320
1321
1321
public void testRestParameter () {
1322
- test (
1323
- "function f(...zero) {}" ,
1324
- "function f(zero) { zero = [].slice.call(arguments, 0); }" );
1325
- test (
1326
- "function f(zero, ...one) {}" ,
1327
- "function f(zero, one) { one = [].slice.call(arguments, 1); }" );
1328
- test (
1329
- "function f(zero, one, ...two) {}" ,
1330
- "function f(zero, one, two) { two = [].slice.call(arguments, 2); }" );
1322
+ test ("function f(...zero) { return zero; }" ,
1323
+ LINE_JOINER .join (
1324
+ "function f(zero) {" ,
1325
+ " var $jscomp$restParams = [];" ,
1326
+ " for (var $jscomp$restIndex = 0; $jscomp$restIndex < arguments.length;" ,
1327
+ " ++$jscomp$restIndex) {" ,
1328
+ " $jscomp$restParams[$jscomp$restIndex - 0] = arguments[$jscomp$restIndex];" ,
1329
+ " }" ,
1330
+ " {" ,
1331
+ " var zero$0 = $jscomp$restParams;" ,
1332
+ " return zero$0;" ,
1333
+ " }" ,
1334
+ "}" ));
1335
+
1336
+ test ("function f(zero, ...one) {}" ,
1337
+ LINE_JOINER .join (
1338
+ "function f(zero, one) {" ,
1339
+ " var $jscomp$restParams = [];" ,
1340
+ " for (var $jscomp$restIndex = 1; $jscomp$restIndex < arguments.length;" ,
1341
+ " ++$jscomp$restIndex) {" ,
1342
+ " $jscomp$restParams[$jscomp$restIndex - 1] = arguments[$jscomp$restIndex];" ,
1343
+ " }" ,
1344
+ " {" ,
1345
+ " var one$0 = $jscomp$restParams;" ,
1346
+ " }" ,
1347
+ "}" ));
1348
+
1349
+ test ("function f(zero, one, ...two) {}" ,
1350
+ LINE_JOINER .join (
1351
+ "function f(zero, one, two) {" ,
1352
+ " var $jscomp$restParams = [];" ,
1353
+ " for (var $jscomp$restIndex = 2; $jscomp$restIndex < arguments.length;" ,
1354
+ " ++$jscomp$restIndex) {" ,
1355
+ " $jscomp$restParams[$jscomp$restIndex - 2] = arguments[$jscomp$restIndex];" ,
1356
+ " }" ,
1357
+ " {" ,
1358
+ " var two$0 = $jscomp$restParams;" ,
1359
+ " }" ,
1360
+ "}" ));
1361
+
1362
+ // Make sure we get type checking on the rest parameters
1363
+ test ("/** @param {...number} zero */ function f(...zero) {}" ,
1364
+ LINE_JOINER .join (
1365
+ "/** @param {...number} zero */ function f(zero) {" ,
1366
+ " var $jscomp$restParams = [];" ,
1367
+ " for (var $jscomp$restIndex = 0; $jscomp$restIndex < arguments.length;" ,
1368
+ " ++$jscomp$restIndex) {" ,
1369
+ " $jscomp$restParams[$jscomp$restIndex - 0] = arguments[$jscomp$restIndex];" ,
1370
+ " }" ,
1371
+ " {" ,
1372
+ " var /** !Array<number> */ zero$0 = $jscomp$restParams;" ,
1373
+ " }" ,
1374
+ "}" ));
1375
+
1376
+ // Inline type
1377
+ test ("function f(/** ...number */ ...zero) {}" ,
1378
+ LINE_JOINER .join (
1379
+ "function f(/** ...number */ zero) {" ,
1380
+ " var $jscomp$restParams = [];" ,
1381
+ " for (var $jscomp$restIndex = 0; $jscomp$restIndex < arguments.length;" ,
1382
+ " ++$jscomp$restIndex) {" ,
1383
+ " $jscomp$restParams[$jscomp$restIndex - 0] = arguments[$jscomp$restIndex];" ,
1384
+ " }" ,
1385
+ " {" ,
1386
+ " var /** !Array<number> */ zero$0 = $jscomp$restParams;" ,
1387
+ " }" ,
1388
+ "}" ));
1389
+
1390
+ // Warn on /** number */
1391
+ testWarning ("function f(/** number */ ...zero) {}" ,
1392
+ Es6ToEs3Converter .BAD_REST_PARAMETER_ANNOTATION );
1393
+ testWarning ("/** @param {number} zero */ function f(...zero) {}" ,
1394
+ Es6ToEs3Converter .BAD_REST_PARAMETER_ANNOTATION );
1331
1395
}
1332
1396
1333
1397
public void testDefaultAndRestParameters () {
1334
- test (
1335
- "function f(zero, one = 1, ...two) {}" ,
1398
+ test ("function f(zero, one = 1, ...two) {}" ,
1336
1399
LINE_JOINER .join (
1337
1400
"function f(zero, one, two) {" ,
1338
1401
" one = (one === undefined) ? 1 : one;" ,
1339
- " two = [].slice.call(arguments, 2);" ,
1402
+ " var $jscomp$restParams = [];" ,
1403
+ " for (var $jscomp$restIndex = 2; $jscomp$restIndex < arguments.length;" ,
1404
+ " ++$jscomp$restIndex) {" ,
1405
+ " $jscomp$restParams[$jscomp$restIndex - 2] = arguments[$jscomp$restIndex];" ,
1406
+ " }" ,
1407
+ " {" ,
1408
+ " var two$0 = $jscomp$restParams;" ,
1409
+ " }" ,
1340
1410
"}" ));
1341
1411
}
1342
1412
0 commit comments