summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/object/destructuring-shorthand-defaults.js
blob: 3ca240e0b7851ec8b88662e780963ba01a8ff651 (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Ensure that the syntax used in shorthand destructuring with defaults
// e.g. |{x=1, y=2} = {}| properly raises a syntax error within an object
// literal. As per ES6 12.2.6 Object Initializer, "NOTE 3."

const SYNTAX_ERROR_STMTS = [
    // expressions
    "({x={}={}}),",
    "({y={x={}={}={}={}={}={}={}={}}={}}),",
    "({a=1, b=2, c=3, x=({}={})}),",
    "({x=1, y={z={1}}})",
    "({x=1} = {y=1});",
    "({x: y={z=1}}={})",
    "({x=1}),",
    "({z={x=1}})=>{};",
    "({x = ({y=1}) => y})",
    "(({x=1})) => x",
    "({e=[]}==(;",
    "({x=1}[-1]);",
    "({x=y}[-9])",
    "({x=y}.x.z[-9])",
    "({x=y}`${-9}`)",
    "(new {x=y}(-9))",
    "new {x=1}",
    "new {x=1}={}",
    "typeof {x=1}",
    "typeof ({x=1})",
    "({x=y, [-9]:0})",
    "((({w = x} >(-9)",
    "++({x=1})",
    "--{x=1}",
    "!{x=1}={}",
    "delete {x=1}",
    "delete ({x=1})",
    "delete {x=1} = {}",
    "({x=1}.abc)",
    "x > (0, {a = b} );",
    // declarations
    "var x = 0 + {a=1} = {}",
    "let o = {x=1};",
    "var j = {x=1};",
    "var j = {x={y=1}}={};",
    "const z = {x=1};",
    "const z = {x={y=1}}={};",
    "const {x=1};",
    "const {x={y=33}}={};",
    "var {x=1};",
    "let {x=1};",
    "let x, y, {z=1}={}, {w=2}, {e=3};",
    // array initialization
    "[{x=1, y = ({z=2} = {})}];",
    // try/catch
    "try {throw 'a';} catch ({x={y=1}}) {}",
    // if/else
    "if ({k: 1, x={y=2}={}}) {}",
    "if (false) {} else if (true) { ({x=1}) }",
    // switch
    "switch ('c') { case 'c': ({x=1}); }",
    // for
    "for ({x=1}; 1;) {1}",
    "for ({x={y=2}}; 1;) {1}",
    "for (var x = 0; x < 2; x++) { ({x=1, y=2}) }",
    "for (let x=1;{x=1};){}",
    "for (let x=1;{x={y=2}};){}",
    "for (let x=1;1;{x=1}){}",
    "for (let x=1;1;{x={y=2}}){}",
    // while
    "while ({x=1}) {1};",
    "while ({x={y=2}}={}) {1};",
    // with
    "with ({x=1}) {};",
    "with ({x={y=3}={}}) {};",
    "with (Math) { ({x=1}) };",
    // ternary
    "true ? {x=1} : 1;",
    "false ? 1 : {x=1};",
    "{x=1} ? 2 : 3;",
    // assignment
    "({x} += {});",
    "({x = 1}) = {x: 2};",
]

for (var stmt of SYNTAX_ERROR_STMTS) {
    assertThrowsInstanceOf(() => {
        Function(stmt);
    }, SyntaxError);
}

// A few tricky but acceptable cases:
// see https://bugzilla.mozilla.org/show_bug.cgi?id=932080#c2

assertEq((({a = 0}) => a)({}), 0);
assertEq((({a = 0} = {}) => a)({}), 0);
assertEq((({a = 0} = {}) => a)({a: 1}), 1);

{
    let x, y;
    ({x=1} = {});
    assertEq(x, 1);
    ({x=1} = {x: 4});
    assertEq(x, 4);
    ({x=1, y=2} = {})
    assertEq(x, 1);
    assertEq(y, 2);
}

{
    let {x={i=1, j=2}={}}={};
    assertDeepEq(x, ({}));
    assertEq(i, 1);
    assertEq(j, 2);
}

// Default destructuring values, which are variables, should be defined
// within closures (Bug 1255167).
{
    let f = function(a){
        return (function({aa = a}){ return aa; })({});
    };
    assertEq(f(9999), 9999);
}

if (typeof reportCompare == "function")
    reportCompare(true, true);