• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15function CheckSyntaxError (str)
16{
17  try {
18    eval (str);
19    assert (false);
20  } catch (e) {
21    assert (e instanceof SyntaxError);
22  }
23
24  /* force the pre-scanner */
25  try {
26    eval ('switch (1) { default: ' + str + '}');
27    assert (false);
28  } catch (e) {
29    assert (e instanceof SyntaxError);
30  }
31}
32
33CheckSyntaxError ('function x (a, b, ...c, d) {}');
34CheckSyntaxError ('function x (... c = 5) {}');
35CheckSyntaxError ('function x (...) {}');
36CheckSyntaxError ('function x (a, a, ...a) {}');
37CheckSyntaxError ('"use strict" function x (...arguments) {}');
38CheckSyntaxError ('var o = { set e (...args) { } }');
39
40rest_params = ['hello', true, 7, {}, [], function () {}];
41
42function f (x, y, ...a) {
43  for (var i = 0; i < a.length; i++) {
44    assert (a[i] == rest_params[i]);
45  }
46  return (x + y) * a.length;
47}
48
49assert (f (1, 2, rest_params[0], rest_params[1], rest_params[2]) === 9);
50assert (f.length === 2);
51
52function g (...a) {
53  return a.reduce (function (accumulator, currentValue) { return accumulator + currentValue });
54}
55
56assert (g (1, 2, 3, 4) === 10);
57
58function h (...arguments) {
59  return arguments.length;
60}
61
62assert (h (1, 2, 3, 4) === 4);
63
64function f2 (a = 1, b = 1, c = 1, ...d) {
65  assert (JSON.stringify (d) === '[]');
66  return a + b + c;
67}
68
69assert (f2 () === 3);
70assert (f2 (2) === 4);
71assert (f2 (2, 3) === 6);
72assert (f2 (2, 3, 4) === 9);
73
74function g2 (a = 5, b = a + 1, ...c) {
75  return a + b + c.length;
76}
77
78assert (g2 () === 11);
79assert (g2 (1) === 3);
80assert (g2 (1, 2) === 3);
81assert (g2 (1, 2, 3) === 4);
82
83function args(a, ...b)
84{
85  assert(a === 1);
86  assert(arguments[0] === 1);
87
88  a = 5;
89
90  assert(a === 5);
91  assert(arguments[0] === 1);
92
93  assert(arguments[1] === 2);
94  assert(b[0] === 2)
95}
96args(1, 2);
97