summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/syntax/non-simple-with-strict-directive.js
blob: a0e5366879c34cb362e6d4c332ff4889c54eaa1e (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
128
129
130
131
132
133
134
135
136
137
138
139
140
/* 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/. */

const testCases = [
    // Array destructuring.
    "[]",
    "[a]",
    "x, [a]",
    "[a], x",

    // Array destructuring with defaults.
    "[a = 0]",
    "x, [a = 0]",
    "[a = 0], x",

    // Array destructuring with rest binding identifier.
    "[...a]",
    "x, [...a]",
    "[...a], x",

    // Array destructuring with rest binding pattern.
    "[...[a]]",
    "x, [...[a]]",
    "[...[a]], x",

    // Object destructuring.
    "{}",
    "{p: o}",
    "x, {p: o}",
    "{p: o}, x",

    // Object destructuring with defaults.
    "{p: o = 0}",
    "x, {p: o = 0}",
    "{p: o = 0}, x",

    // Object destructuring with shorthand identifier form.
    "{o}",
    "x, {o}",
    "{o}, x",

    // Object destructuring with CoverInitName.
    "{o = 0}",
    "x, {o = 0}",
    "{o = 0}, x",

    // Default parameter.
    "d = 0",
    "x, d = 0",
    "d = 0, x",

    // Rest parameter.
    "...rest",
    "x, ...rest",

    // Rest parameter with array destructuring.
    "...[]",
    "...[a]",
    "x, ...[]",
    "x, ...[a]",

    // Rest parameter with object destructuring.
    "...{}",
    "...{p: o}",
    "x, ...{}",
    "x, ...{p: o}",

    // All non-simple cases combined.
    "x, d = 123, [a], {p: 0}, ...rest",
];

const GeneratorFunction = function*(){}.constructor;

const functionDefinitions = [
    // FunctionDeclaration
    parameters => `function f(${parameters}) { "use strict"; }`,

    // FunctionExpression
    parameters => `void function(${parameters}) { "use strict"; };`,
    parameters => `void function f(${parameters}) { "use strict"; };`,

    // Function constructor
    parameters => `Function('${parameters}', '"use strict";')`,

    // GeneratorDeclaration
    parameters => `function* g(${parameters}) { "use strict"; }`,

    // GeneratorExpression
    parameters => `void function*(${parameters}) { "use strict"; };`,
    parameters => `void function* g(${parameters}) { "use strict"; };`,

    // GeneratorFunction constructor
    parameters => `GeneratorFunction('${parameters}', '"use strict";')`,

    // MethodDefinition
    parameters => `({ m(${parameters}) { "use strict"; } });`,
    parameters => `(class { m(${parameters}) { "use strict"; } });`,
    parameters => `class C { m(${parameters}) { "use strict"; } }`,

    // MethodDefinition (constructors)
    parameters => `(class { constructor(${parameters}) { "use strict"; } });`,
    parameters => `class C { constructor(${parameters}) { "use strict"; } }`,

    // MethodDefinition (getter)
    parameters => `({ get m(${parameters}) { "use strict"; } });`,
    parameters => `(class { get m(${parameters}) { "use strict"; } });`,
    parameters => `class C { get m(${parameters}) { "use strict"; } }`,

    // MethodDefinition (setter)
    parameters => `({ set m(${parameters}) { "use strict"; } });`,
    parameters => `(class { set m(${parameters}) { "use strict"; } });`,
    parameters => `class C { set m(${parameters}) { "use strict"; } }`,

    // GeneratorMethod
    parameters => `({ *m(${parameters}) { "use strict"; } });`,
    parameters => `(class { *m(${parameters}) { "use strict"; } });`,
    parameters => `class C { *m(${parameters}) { "use strict"; } }`,

    // ArrowFunction
    parameters => `(${parameters}) => { "use strict"; };`,
];

for (let nonSimpleParameters of testCases) {
    for (let def of functionDefinitions) {
        // Non-strict script code.
        assertThrowsInstanceOf(() => eval(`
            ${def(nonSimpleParameters)}
        `), SyntaxError, def(nonSimpleParameters));

        // Strict script code.
        assertThrowsInstanceOf(() => eval(`
            "use strict";
            ${def(nonSimpleParameters)}
        `), SyntaxError, `"use strict"; ${def(nonSimpleParameters)}`);
    }
}

if (typeof reportCompare === 'function')
    reportCompare(0, 0);