summaryrefslogtreecommitdiffstats
path: root/js/src/tests/ecma_6/Syntax/statement-versus-statementlistitem.js
blob: d0565353ddd3a57be0a6f589bdea359aaa854a2f (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
141
142
143
144
145
146
147
148
// |reftest| skip-if(!xulRuntime.shell) -- needs evaluate, parseModule
/*
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/licenses/publicdomain/
 */

//-----------------------------------------------------------------------------
var BUGNUMBER = 1288459;
var summary =
  "Properly implement the spec's distinctions between StatementListItem and " +
  "Statement grammar productions and their uses";

print(BUGNUMBER + ": " + summary);

/**************
 * BEGIN TEST *
 **************/

var counter = 0;

// Test all the different contexts that expect a Statement.

function contextAcceptsStatement(pat)
{
  var goodCode =
`function f${counter++}()
{
  ${pat.replace("@@@", "let \n {} \n ;")}
}
`;

  evaluate(goodCode);

  var badCode =
`function f${counter++}()
{
  ${pat.replace("@@@", "let {} \n ;")}
}
`;

  try
  {
    evaluate(badCode);
    throw new Error("didn't throw");
  }
  catch (e)
  {
    assertEq(e instanceof SyntaxError, true,
             "didn't get a syntax error, instead got: " + e);
  }
}

contextAcceptsStatement("if (0) @@@");
contextAcceptsStatement("if (0) ; else @@@");
contextAcceptsStatement("if (0) ; else if (0) @@@");
contextAcceptsStatement("if (0) ; else if (0) ; else @@@");
// Can't test do-while loops this way because the Statement isn't in trailing
// position, so 'let' followed by newline *must* be followed by 'while'.
contextAcceptsStatement("while (1) @@@");
contextAcceptsStatement("for (1; 1; 0) @@@");
contextAcceptsStatement("for (var x; 1; 0) @@@");
contextAcceptsStatement("for (let x; 1; 0) @@@");
contextAcceptsStatement("for (const x = 1; 1; 0) @@@");
contextAcceptsStatement("for (x in 0) @@@");
contextAcceptsStatement("for (var x in 0) @@@");
contextAcceptsStatement("for (let x in 0) @@@");
contextAcceptsStatement("for (const x in 0) @@@");
contextAcceptsStatement("for (x of []) @@@");
contextAcceptsStatement("for (var x of []) @@@");
contextAcceptsStatement("for (let x of []) @@@");
contextAcceptsStatement("for (const x of []) @@@");
contextAcceptsStatement("with (1) @@@");
contextAcceptsStatement("foo: @@@");

// Test all the different contexts that expect a StatementListItem.

function contextAcceptsStatementListItem(pat)
{
  var goodCode =
`function f${counter++}()
{
  ${pat.replace("@@@", "let \n [] = [] ;")}
}
`;

  evaluate(goodCode);

  var moarGoodCode = pat.replace("@@@", "let \n [] = [] ;");
  evaluate(moarGoodCode);

  var goodModuleCode = pat.replace("@@@", "let \n [] = [] ;");
  parseModule(goodModuleCode);

  var badCode =
`function f${counter++}()
{
  ${pat.replace("@@@", "let \n let = 3 ;")}
}
`;

  try
  {
    evaluate(badCode);
    throw new Error("didn't throw");
  }
  catch (e)
  {
    assertEq(e instanceof SyntaxError, true,
             "didn't get a syntax error, instead got: " + e);
  }
}

contextAcceptsStatementListItem("{ @@@ }");
contextAcceptsStatementListItem("switch (1) { case 1: @@@ }");
contextAcceptsStatementListItem("switch (1) { default: @@@ }");
contextAcceptsStatementListItem("switch (1) { case 0: case 1: @@@ }");
contextAcceptsStatementListItem("switch (1) { case 0: break; case 1: @@@ }");
contextAcceptsStatementListItem("switch (1) { default: @@@ case 2: }");
contextAcceptsStatementListItem("try { @@@ } catch (e) { }");
contextAcceptsStatementListItem("try { @@@ } finally { }");
contextAcceptsStatementListItem("try { @@@ } catch (e) { } finally { }");
contextAcceptsStatementListItem("try { } catch (e) { @@@ }");
contextAcceptsStatementListItem("try { } finally { @@@ }");
contextAcceptsStatementListItem("try { } catch (e) { @@@ } finally { }");
contextAcceptsStatementListItem("try { } catch (e) { } finally { @@@ }");
contextAcceptsStatementListItem("_ => { @@@ }");
contextAcceptsStatementListItem("function q() { @@@ }");
contextAcceptsStatementListItem("function* q() { @@@ }");
contextAcceptsStatementListItem("(function() { @@@ })");
contextAcceptsStatementListItem("(function*() { @@@ })");
contextAcceptsStatementListItem("({ *q() { @@@ } })");
contextAcceptsStatementListItem("({ q() { @@@ } })");
contextAcceptsStatementListItem("({ get q() { @@@ } })");
contextAcceptsStatementListItem("({ set q(v) { @@@ } })");
contextAcceptsStatementListItem("(class { f() { @@@ } })");
contextAcceptsStatementListItem("(class { static f() { @@@ } })");
contextAcceptsStatementListItem("(class { static *f() { @@@ } })");
contextAcceptsStatementListItem("(class { static get f() { @@@ } })");
contextAcceptsStatementListItem("(class { static set f(v) { @@@ } })");
contextAcceptsStatementListItem("(class { static() { @@@ } })");
contextAcceptsStatementListItem("@@@");

/******************************************************************************/

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

print("Tests complete");