801c0d6eb5f9eaca6902987b00590960729f827e
[WebKit-https.git] / LayoutTests / inspector / model / parse-script-syntax-tree.html
1 <!doctype html>
2 <html>
3 <head>
4 <script type="text/javascript" src="../../http/tests/inspector/resources/inspector-test.js"></script>
5 <script>
6 function test()
7 {
8     function makeNode(text, isExpression)
9     {
10         var script = new WebInspector.Script(1, new WebInspector.TextRange(0, text.length));
11         var scriptSyntaxTree = new WebInspector.ScriptSyntaxTree(text, script);
12         var syntaxTree = scriptSyntaxTree._syntaxTree;
13
14         InspectorTest.assert(scriptSyntaxTree.parsedSuccessfully, "ScriptSyntaxTree should be able to parse: \"" + text + "\"");
15         InspectorTest.assert(syntaxTree.type === WebInspector.ScriptSyntaxTree.NodeType.Program, "Should be program.");
16         InspectorTest.assert(syntaxTree.body.length === 1, "Calling makeNode should return one node in the program.");
17         if (isExpression) {
18             InspectorTest.assert(syntaxTree.body[0].type === WebInspector.ScriptSyntaxTree.NodeType.ExpressionStatement);
19             return syntaxTree.body[0].expression;
20         } else
21             return syntaxTree.body[0];
22     }
23
24     var node = null;
25
26     node = makeNode("x = 20;", true);
27     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.AssignmentExpression);
28     InspectorTest.assert(node.left);
29     InspectorTest.assert(node.left.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
30     InspectorTest.assert(node.right);
31     InspectorTest.assert(node.right.type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
32     InspectorTest.assert(node.operator === "=");
33     node = makeNode("[x, y] = foo;", true);
34     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.AssignmentExpression);
35     InspectorTest.assert(node.left);
36     InspectorTest.assert(node.left.type === WebInspector.ScriptSyntaxTree.NodeType.ArrayPattern);
37     InspectorTest.assert(node.left.elements.length === 2);
38     InspectorTest.assert(node.left.elements[0].type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
39     InspectorTest.assert(node.left.elements[0].name === "x");
40     InspectorTest.assert(node.left.elements[1].type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
41     InspectorTest.assert(node.left.elements[1].name === "y");
42     node = makeNode("x += 20;", true);
43     InspectorTest.assert(node.operator === "+=");
44     node = makeNode("x -= 20;", true);
45     InspectorTest.assert(node.operator === "-=");
46     InspectorTest.log("passed AssignmentExpression");
47
48     node = makeNode("[1, 2]", true);
49     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ArrayExpression);
50     InspectorTest.assert(Array.isArray(node.elements));
51     InspectorTest.assert(node.elements.length === 2);
52     InspectorTest.assert(node.elements[0].type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
53     InspectorTest.assert(node.elements[1].type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
54     InspectorTest.log("passed ArrayExpression");
55     
56     node = makeNode("{foo();}", false);
57     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.BlockStatement);
58     InspectorTest.assert(node.body);
59     InspectorTest.assert(node.body.length === 1);
60     InspectorTest.assert(node.body[0].expression.type === WebInspector.ScriptSyntaxTree.NodeType.CallExpression);
61     InspectorTest.log("passed BlockStatement");
62
63     node = makeNode("2 + 2", true);
64     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.BinaryExpression);
65     InspectorTest.assert(node.left);
66     InspectorTest.assert(node.right);
67     InspectorTest.assert(node.operator === "+");
68     node = makeNode("2 - 2", true);
69     InspectorTest.assert(node.operator === "-");
70     node = makeNode("2 * 2", true);
71     InspectorTest.assert(node.operator === "*");
72     node = makeNode("2 / 2", true);
73     InspectorTest.assert(node.operator === "/");
74     node = makeNode("2 % 2", true);
75     InspectorTest.assert(node.operator === "%");
76     node = makeNode("2 | 2", true);
77     InspectorTest.assert(node.operator === "|");
78     node = makeNode("2 ^ 2", true);
79     InspectorTest.assert(node.operator === "^");
80     node = makeNode("2 == 2", true);
81     InspectorTest.assert(node.operator === "==");
82     node = makeNode("2 === 2", true);
83     InspectorTest.assert(node.operator === "===");
84     InspectorTest.log("passed BinaryExpression");
85
86     node = makeNode("label:while(true) {break label;}", false);
87     InspectorTest.assert(node.body.body.body[0].type === WebInspector.ScriptSyntaxTree.NodeType.BreakStatement);
88     InspectorTest.assert(node.body.body.body[0].label.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
89     InspectorTest.assert(node.body.body.body[0].label.name === "label");
90     InspectorTest.log("passed BreakStatement");
91
92     node = makeNode("foo(20, 25)", true);
93     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.CallExpression);
94     InspectorTest.assert(node.arguments);
95     InspectorTest.assert(node.arguments.length === 2);
96     InspectorTest.assert(node.arguments[0].type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
97     InspectorTest.assert(node.arguments[1].type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
98     InspectorTest.log("passed CallExpression");
99
100     node = makeNode("try {} catch(e) {} ", false);
101     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.TryStatement);
102     InspectorTest.log("passed TryStatement");
103     InspectorTest.assert(node.handler.type === WebInspector.ScriptSyntaxTree.NodeType.CatchClause);
104     InspectorTest.log("passed CatchClause");
105
106     node = makeNode("foo ? bar : baz;", true);
107     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ConditionalExpression);
108     InspectorTest.assert(node.test);
109     InspectorTest.assert(node.consequent);
110     InspectorTest.assert(node.alternate);
111     InspectorTest.assert(node.test.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
112     InspectorTest.assert(node.consequent.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
113     InspectorTest.assert(node.alternate.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
114     InspectorTest.log("passed ConditionalExpression");
115     
116     node = makeNode("label:while(true) {continue label;}", false);
117     InspectorTest.assert(node.body.body.body[0].type === WebInspector.ScriptSyntaxTree.NodeType.ContinueStatement);
118     InspectorTest.assert(node.body.body.body[0].label.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
119     InspectorTest.assert(node.body.body.body[0].label.name === "label");
120     InspectorTest.log("passed ContinueStatement");
121
122     node = makeNode("do{}while(true);", false);
123     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.DoWhileStatement);
124     InspectorTest.assert(node.body.type === WebInspector.ScriptSyntaxTree.NodeType.BlockStatement);
125     InspectorTest.assert(node.body.body.length === 0);
126     InspectorTest.assert(node.test);
127     InspectorTest.assert(node.test.type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
128     InspectorTest.log("passed DoWhileStatement");
129
130     node = makeNode("debugger;", false);
131     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.DebuggerStatement);
132     InspectorTest.log("passed DebuggerStatement");
133
134     node = makeNode(";", false);
135     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.EmptyStatement);
136     InspectorTest.log("passed EmptyStatement");
137
138     node = makeNode("2 + 2", false);
139     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ExpressionStatement);
140     InspectorTest.assert(node.expression);
141     InspectorTest.assert(node.expression.type === WebInspector.ScriptSyntaxTree.NodeType.BinaryExpression);
142     InspectorTest.log("passed ExpressionStatement");
143
144     node = makeNode("for(x = 0;x < 20;x++){}", false);
145     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ForStatement);
146     InspectorTest.assert(node.init);
147     InspectorTest.assert(node.init.type === WebInspector.ScriptSyntaxTree.NodeType.AssignmentExpression);
148     InspectorTest.assert(node.test);
149     InspectorTest.assert(node.test.type === WebInspector.ScriptSyntaxTree.NodeType.BinaryExpression);
150     InspectorTest.assert(node.update);
151     InspectorTest.assert(node.update.type === WebInspector.ScriptSyntaxTree.NodeType.UpdateExpression);
152     InspectorTest.assert(node.body);
153     InspectorTest.assert(node.body.type === WebInspector.ScriptSyntaxTree.NodeType.BlockStatement);
154     node = makeNode("for(x = 0;x < 20;x++);", false);
155     InspectorTest.assert(node.body);
156     InspectorTest.assert(node.body.type === WebInspector.ScriptSyntaxTree.NodeType.EmptyStatement);
157     InspectorTest.log("passed ForStatement");
158
159     node = makeNode("for(var x in {}){}", false);
160     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ForInStatement);
161     InspectorTest.assert(node.left);
162     InspectorTest.assert(node.left.type === WebInspector.ScriptSyntaxTree.NodeType.VariableDeclaration);
163     InspectorTest.assert(node.right);
164     InspectorTest.assert(node.right.type === WebInspector.ScriptSyntaxTree.NodeType.ObjectExpression);
165     InspectorTest.assert(node.body);
166     InspectorTest.assert(node.body.type === WebInspector.ScriptSyntaxTree.NodeType.BlockStatement);
167     node = makeNode("for(var x in {});", false);
168     InspectorTest.assert(node.body);
169     InspectorTest.assert(node.body.type === WebInspector.ScriptSyntaxTree.NodeType.EmptyStatement);
170     InspectorTest.log("passed ForInStatement");
171
172     node = makeNode("for(var x of {});", false);
173     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ForOfStatement);
174     InspectorTest.assert(node.left);
175     InspectorTest.assert(node.left.type === WebInspector.ScriptSyntaxTree.NodeType.VariableDeclaration);
176     InspectorTest.assert(node.right);
177     InspectorTest.assert(node.right.type === WebInspector.ScriptSyntaxTree.NodeType.ObjectExpression);
178     InspectorTest.assert(node.body);
179     InspectorTest.assert(node.body.type === WebInspector.ScriptSyntaxTree.NodeType.EmptyStatement);
180     node = makeNode("for(var {x} of {});", false);
181     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ForOfStatement);
182     InspectorTest.assert(node.left);
183     InspectorTest.assert(node.left.type === WebInspector.ScriptSyntaxTree.NodeType.VariableDeclaration);
184     InspectorTest.assert(node.left.declarations[0].id.type === WebInspector.ScriptSyntaxTree.NodeType.ObjectPattern);
185     InspectorTest.assert(node.left.declarations[0].id.properties[0].value.name === "x");
186     InspectorTest.log("passed ForOfStatement");
187
188     node = makeNode("function foo(x,y){}", false);
189     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.FunctionDeclaration);
190     InspectorTest.assert(node.params);
191     InspectorTest.assert(node.params.length === 2);
192     InspectorTest.assert(node.params[0].type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
193     InspectorTest.assert(node.params[1].type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
194     InspectorTest.assert(node.body);
195     InspectorTest.assert(node.body.type === WebInspector.ScriptSyntaxTree.NodeType.BlockStatement);
196     InspectorTest.assert(!node.generator);
197     node = makeNode("x = {get foo(){return 20}}", true);
198     InspectorTest.assert(node.right.properties[0].value.type === WebInspector.ScriptSyntaxTree.NodeType.FunctionExpression);
199     node = makeNode("x = {set foo(x){return 20}}", true);
200     InspectorTest.assert(node.right.properties[0].value.type === WebInspector.ScriptSyntaxTree.NodeType.FunctionExpression);
201     InspectorTest.log("passed FunctionDeclaration");
202
203     node = makeNode("foo(function(x,y){})", true);
204     InspectorTest.assert(node.arguments[0].type === WebInspector.ScriptSyntaxTree.NodeType.FunctionExpression);
205     InspectorTest.assert(node.arguments[0].params);
206     InspectorTest.assert(node.arguments[0].params.length === 2);
207     InspectorTest.assert(node.arguments[0].params[0].type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
208     InspectorTest.assert(node.arguments[0].params[1].type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
209     InspectorTest.assert(node.arguments[0].body);
210     InspectorTest.assert(node.arguments[0].body.type === WebInspector.ScriptSyntaxTree.NodeType.BlockStatement);
211     InspectorTest.assert(!node.arguments[0].generator);
212     InspectorTest.log("passed FunctionExpression");
213
214     node = makeNode("foo;", true);
215     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
216     InspectorTest.assert(node.name);
217     InspectorTest.assert(node.name === "foo");
218     InspectorTest.log("passed Identifier");
219
220     node = makeNode("if (true) baz; else bar;", false);
221     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.IfStatement);
222     InspectorTest.assert(node.test);
223     InspectorTest.assert(node.test.type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
224     InspectorTest.assert(node.consequent);
225     InspectorTest.assert(node.consequent.type === WebInspector.ScriptSyntaxTree.NodeType.ExpressionStatement);
226     InspectorTest.assert(node.alternate);
227     InspectorTest.assert(node.alternate.type === WebInspector.ScriptSyntaxTree.NodeType.ExpressionStatement);
228     InspectorTest.log("passed IfStatement");
229
230     node = makeNode("true;", true);
231     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
232     InspectorTest.assert(node.value === true);
233     InspectorTest.assert(node.raw === "true");
234     node = makeNode("false;", true);
235     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
236     InspectorTest.assert(node.value === false);
237     InspectorTest.assert(node.raw === "false");
238     node = makeNode("20;", true);
239     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
240     InspectorTest.assert(node.value === 20);
241     InspectorTest.assert(node.raw === "20");
242     node = makeNode("'20';", true);
243     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
244     InspectorTest.assert(node.value === '20');
245     InspectorTest.assert(node.raw === "'20'");
246     node = makeNode("null;", true);
247     InspectorTest.assert(node.value === null);
248     InspectorTest.assert(node.raw === "null");
249     node = makeNode("/regex/gi;", true);
250     InspectorTest.assert(node.value instanceof RegExp);
251     InspectorTest.assert(node.value.toString() === "/regex/gi");
252     InspectorTest.assert(node.raw === "/regex/gi");
253     node = makeNode("0x10", true);
254     InspectorTest.assert(node.value === 0x10);
255     InspectorTest.assert(node.raw === "0x10");
256     node = makeNode("0777", true);
257     InspectorTest.assert(node.value === 0777);
258     InspectorTest.assert(node.raw === "0777");
259     node = makeNode("\"a\"", true);
260     InspectorTest.assert(node.value === "a");
261     InspectorTest.assert(node.raw === "\"a\"");
262     InspectorTest.log("passed Literal");
263
264     node = makeNode("label:while(true)20;", false);
265     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.LabeledStatement);
266     InspectorTest.assert(node.label);
267     InspectorTest.assert(node.label.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
268     InspectorTest.assert(node.label.name === "label");
269     InspectorTest.log("passed LabeledStatement");
270
271     node = makeNode("x && 20", true);
272     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.LogicalExpression);
273     InspectorTest.assert(node.left);
274     InspectorTest.assert(node.left.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
275     InspectorTest.assert(node.right);
276     InspectorTest.assert(node.right.type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
277     //InspectorTest.assert(node.operator === "&&");
278     node = makeNode("x || 20", true);
279     InspectorTest.assert(node.operator === "||");
280     InspectorTest.log("passed LogicalExpression");
281
282     node = makeNode("foo[20]", true);
283     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.MemberExpression);
284     InspectorTest.assert(node.computed);
285     InspectorTest.assert(node.object);
286     InspectorTest.assert(node.object.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
287     InspectorTest.assert(node.property);
288     InspectorTest.assert(node.property.type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
289     node = makeNode("foo.bar", true);
290     InspectorTest.assert(!node.computed);
291     InspectorTest.assert(node.property.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
292     InspectorTest.log("passed MemberExpression");
293
294     node = makeNode("new Foo(20)", true);
295     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.NewExpression);
296     InspectorTest.assert(node.callee);
297     InspectorTest.assert(node.callee.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
298     InspectorTest.assert(node.arguments);
299     InspectorTest.assert(node.arguments.length === 1);
300     node = makeNode("new Foo", true);
301     InspectorTest.assert(node.arguments);
302     InspectorTest.assert(node.arguments.length === 0);
303     InspectorTest.log("passed NewExpression");
304
305     node = makeNode("x = {foo:bar};", true);
306     InspectorTest.assert(node.right.type === WebInspector.ScriptSyntaxTree.NodeType.ObjectExpression);
307     InspectorTest.assert(node.right.properties);
308     InspectorTest.assert(node.right.properties.length === 1);
309     InspectorTest.log("passed ObjectExpression");
310
311     // Program is tested in makeNode
312
313     node = makeNode("x = {foo:20};", true);
314     InspectorTest.assert(node.right.properties[0].type === WebInspector.ScriptSyntaxTree.NodeType.Property);
315     InspectorTest.assert(node.right.properties[0].key);
316     InspectorTest.assert(node.right.properties[0].key.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
317     InspectorTest.assert(node.right.properties[0].value);
318     InspectorTest.assert(node.right.properties[0].value.type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
319     node = makeNode("x = {'foo':20};", true);
320     InspectorTest.assert(node.right.properties[0].key);
321     InspectorTest.assert(node.right.properties[0].key.type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
322     InspectorTest.log("passed Property");
323
324     node = makeNode("function foo(...things) { return things; }", false);    
325     InspectorTest.assert(node.params.length === 1);
326     InspectorTest.assert(node.params[0].type === WebInspector.ScriptSyntaxTree.NodeType.RestElement);
327     InspectorTest.assert(node.params[0].argument.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
328     InspectorTest.assert(node.params[0].argument.name === "things");
329     InspectorTest.log("passed RestStatement");
330
331     node = makeNode("function foo() { return 20; }", false);
332     InspectorTest.assert(node.body.body[0].type === WebInspector.ScriptSyntaxTree.NodeType.ReturnStatement);
333     InspectorTest.assert(node.body.body[0].argument.type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
334     node = makeNode("function foo() { return; }", false);
335     InspectorTest.assert(node.body.body[0].argument === null);
336     InspectorTest.log("passed ReturnStatement");
337
338     node = makeNode("(x, y)", true);
339     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.SequenceExpression);
340     InspectorTest.assert(node.expressions);
341     InspectorTest.assert(node.expressions.length === 2);
342     InspectorTest.assert(node.expressions[0].type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
343     InspectorTest.assert(node.expressions[1].type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
344     InspectorTest.log("passed SequenceExpression");
345
346     node = makeNode("switch(x){case y:break;}", false);
347     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.SwitchStatement);
348     InspectorTest.assert(node.discriminant.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
349     InspectorTest.log("passed SwitchStatement");
350     InspectorTest.assert(node.cases[0].type === WebInspector.ScriptSyntaxTree.NodeType.SwitchCase);
351     InspectorTest.assert(node.cases[0].test.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
352     InspectorTest.assert(node.cases[0].consequent);
353     InspectorTest.assert(Array.isArray(node.cases[0].consequent));
354     InspectorTest.assert(node.cases[0].consequent.length === 1);
355     InspectorTest.log("passed SwitchCase");
356
357     node = makeNode("this;", true);
358     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ThisExpression);
359     InspectorTest.log("passed ThisExpression");
360
361     node = makeNode("throw new Error;", false);
362     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ThrowStatement);
363     InspectorTest.assert(node.argument);
364     InspectorTest.assert(node.argument.type === WebInspector.ScriptSyntaxTree.NodeType.NewExpression);
365     InspectorTest.log("passed ThrowStatement");
366
367     node = makeNode("!foo;", true);
368     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.UnaryExpression);
369     InspectorTest.assert(node.argument);
370     InspectorTest.assert(node.argument.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
371     InspectorTest.assert(node.operator === "!");
372     node = makeNode("~foo;", true);
373     InspectorTest.assert(node.operator === "~");
374     node = makeNode("-foo;", true);
375     InspectorTest.assert(node.operator === "-");
376     InspectorTest.log("passed UnaryExpression");
377
378     node = makeNode("foo++;", true);
379     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.UpdateExpression);
380     InspectorTest.assert(node.argument);
381     InspectorTest.assert(node.argument.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
382     InspectorTest.assert(node.operator === "++");
383     InspectorTest.assert(node.prefix === false);
384     node = makeNode("++foo;", true);
385     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.UpdateExpression);
386     InspectorTest.assert(node.argument);
387     InspectorTest.assert(node.argument.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
388     InspectorTest.assert(node.prefix === true);
389     InspectorTest.assert(node.operator === "++");
390     node = makeNode("--foo;", true);
391     InspectorTest.assert(node.operator === "--");
392     node = makeNode("foo--;", true);
393     InspectorTest.assert(node.operator === "--");
394     InspectorTest.log("passed UpdateExpression");
395
396     node = makeNode("var foo = 20;", false);
397     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.VariableDeclaration);
398     InspectorTest.assert(node.declarations);
399     InspectorTest.assert(node.declarations.length === 1);
400     InspectorTest.log("passed VariableDeclaration");
401     InspectorTest.assert(node.declarations[0].type === WebInspector.ScriptSyntaxTree.NodeType.VariableDeclarator);
402     InspectorTest.assert(node.declarations[0].id);
403     InspectorTest.assert(node.declarations[0].id.type);
404     InspectorTest.assert(node.declarations[0].id.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
405     InspectorTest.assert(node.declarations[0].init);
406     InspectorTest.assert(node.declarations[0].init.type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
407     node = makeNode("var foo;", false);
408     InspectorTest.assert(node.declarations[0].init === null);
409     node = makeNode("var {x, y: foo} = bar;", false);
410     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.VariableDeclaration);
411     InspectorTest.assert(node.declarations.length === 1);
412     node = node.declarations[0].id;
413     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ObjectPattern);
414     InspectorTest.assert(node.properties.length === 2);
415     InspectorTest.assert(node.properties[0].type === WebInspector.ScriptSyntaxTree.NodeType.Property);
416     InspectorTest.assert(node.properties[0].key.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
417     InspectorTest.assert(node.properties[0].value.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
418     InspectorTest.assert(node.properties[0].key.name === node.properties[0].value.name && node.properties[0].key.name === "x");
419     InspectorTest.assert(node.properties[1].type === WebInspector.ScriptSyntaxTree.NodeType.Property);
420     InspectorTest.assert(node.properties[1].key.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
421     InspectorTest.assert(node.properties[1].value.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
422     InspectorTest.assert(node.properties[1].key.name === "y");
423     InspectorTest.assert(node.properties[1].value.name === "foo");
424     node = makeNode("var [x, y] = foo;", false);
425     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.VariableDeclaration);
426     InspectorTest.assert(node.declarations.length === 1);
427     node = node.declarations[0].id;
428     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ArrayPattern);
429     InspectorTest.assert(node.elements.length === 2);
430     InspectorTest.assert(node.elements[0].type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
431     InspectorTest.assert(node.elements[0].name === "x");
432     InspectorTest.assert(node.elements[1].type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
433     InspectorTest.assert(node.elements[1].name === "y");
434     InspectorTest.log("passed VariableDeclarator");
435
436     node = makeNode("while(true)boo;", false);
437     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.WhileStatement);
438     InspectorTest.assert(node.test);
439     InspectorTest.assert(node.test.type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
440     InspectorTest.assert(node.body);
441     InspectorTest.assert(node.body.type === WebInspector.ScriptSyntaxTree.NodeType.ExpressionStatement);
442     node = makeNode("while(true){}", false);
443     InspectorTest.assert(node.body);
444     InspectorTest.assert(node.body.type === WebInspector.ScriptSyntaxTree.NodeType.BlockStatement);
445     InspectorTest.log("passed WhileStatement");
446
447     node = makeNode("with(o) {}", false);
448     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.WithStatement);
449     InspectorTest.assert(node.object);
450     InspectorTest.assert(node.object.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
451     InspectorTest.assert(node.body);
452     InspectorTest.assert(node.body.type === WebInspector.ScriptSyntaxTree.NodeType.BlockStatement);
453     InspectorTest.log("passed WithStatement");
454
455     node = makeNode("function* gen() { yield 1; yield* [1,2]; }", false);
456     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.FunctionDeclaration);
457     InspectorTest.assert(node.generator);
458     InspectorTest.assert(node.body.type === WebInspector.ScriptSyntaxTree.NodeType.BlockStatement);
459     InspectorTest.assert(node.body.body.length === 2);
460     InspectorTest.assert(node.body.body[0].type === WebInspector.ScriptSyntaxTree.NodeType.ExpressionStatement);
461     InspectorTest.assert(node.body.body[0].expression.type === WebInspector.ScriptSyntaxTree.NodeType.YieldExpression);
462     InspectorTest.assert(!node.body.body[0].expression.delegate);
463     InspectorTest.assert(node.body.body[0].expression.argument.type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
464     InspectorTest.assert(node.body.body[1].type === WebInspector.ScriptSyntaxTree.NodeType.ExpressionStatement);
465     InspectorTest.assert(node.body.body[1].expression.type === WebInspector.ScriptSyntaxTree.NodeType.YieldExpression);
466     InspectorTest.assert(node.body.body[1].expression.delegate);
467     InspectorTest.assert(node.body.body[1].expression.argument.type === WebInspector.ScriptSyntaxTree.NodeType.ArrayExpression);
468     InspectorTest.log("passed YieldExpression");
469
470     node = makeNode("class C { constructor() { super(); new.target; } }", false);
471     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ClassDeclaration);
472     InspectorTest.assert(node.body.type === WebInspector.ScriptSyntaxTree.NodeType.ClassBody);
473     InspectorTest.assert(node.body.body[0].type === WebInspector.ScriptSyntaxTree.NodeType.MethodDefinition);
474     InspectorTest.assert(node.body.body[0].value.type === WebInspector.ScriptSyntaxTree.NodeType.FunctionExpression);
475     InspectorTest.assert(node.body.body[0].value.body.type === WebInspector.ScriptSyntaxTree.NodeType.BlockStatement);
476     InspectorTest.assert(node.body.body[0].value.body.body.length === 2);
477     InspectorTest.assert(node.body.body[0].value.body.body[0].type === WebInspector.ScriptSyntaxTree.NodeType.ExpressionStatement);
478     InspectorTest.assert(node.body.body[0].value.body.body[0].expression.type === WebInspector.ScriptSyntaxTree.NodeType.CallExpression);
479     InspectorTest.assert(node.body.body[0].value.body.body[0].expression.callee.type === WebInspector.ScriptSyntaxTree.NodeType.Super);
480     InspectorTest.assert(node.body.body[0].value.body.body[1].type === WebInspector.ScriptSyntaxTree.NodeType.ExpressionStatement);
481     InspectorTest.assert(node.body.body[0].value.body.body[1].expression.type === WebInspector.ScriptSyntaxTree.NodeType.MetaProperty);
482     InspectorTest.assert(node.body.body[0].value.body.body[1].expression.meta.name === "new");
483     InspectorTest.assert(node.body.body[0].value.body.body[1].expression.property.name === "target");
484     InspectorTest.log("passed ClassStatement, Super, MetaProperty");
485
486     node = makeNode("let [x=20] = [];", false);
487     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.VariableDeclaration);
488     InspectorTest.assert(node.kind === "let");
489     InspectorTest.assert(node.declarations.length === 1);
490     InspectorTest.assert(node.declarations[0].type === WebInspector.ScriptSyntaxTree.NodeType.VariableDeclarator);
491     InspectorTest.assert(node.declarations[0].id.type === WebInspector.ScriptSyntaxTree.NodeType.ArrayPattern);
492     InspectorTest.assert(node.declarations[0].id.elements.length === 1);
493     InspectorTest.assert(node.declarations[0].id.elements[0].type === WebInspector.ScriptSyntaxTree.NodeType.AssignmentPattern);
494     InspectorTest.assert(node.declarations[0].id.elements[0].left.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
495     InspectorTest.assert(node.declarations[0].id.elements[0].left.name === "x");
496     InspectorTest.assert(node.declarations[0].id.elements[0].right.type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
497     InspectorTest.assert(node.declarations[0].id.elements[0].right.value === 20);
498     InspectorTest.log("passed AssignmentPattern");
499
500     node = makeNode("(x) => x;", true);
501     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ArrowFunctionExpression);
502     InspectorTest.assert(node.params.length === 1);
503     InspectorTest.assert(node.params[0].type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
504     InspectorTest.assert(node.params[0].name === "x");
505     InspectorTest.assert(node.expression === true);
506     InspectorTest.assert(node.body.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
507
508     node = makeNode("(x = 20) => { return x; };", true);
509     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ArrowFunctionExpression);
510     InspectorTest.assert(node.params.length === 1);
511     InspectorTest.assert(node.params[0].type === WebInspector.ScriptSyntaxTree.NodeType.AssignmentPattern);
512     InspectorTest.assert(node.params[0].left.type === WebInspector.ScriptSyntaxTree.NodeType.Identifier);
513     InspectorTest.assert(node.params[0].left.name === "x");
514     InspectorTest.assert(node.params[0].right.type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
515     InspectorTest.assert(node.params[0].right.value === 20);
516     InspectorTest.assert(node.expression === false);
517     InspectorTest.assert(node.body.type === WebInspector.ScriptSyntaxTree.NodeType.BlockStatement);
518     InspectorTest.log("passed ArrowFunctionExpression");
519
520     node = makeNode("async function foo() {}", false);
521     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.FunctionDeclaration);
522     InspectorTest.assert(node.async);
523     node = makeNode("function foo() {}", false);
524     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.FunctionDeclaration);
525     InspectorTest.assert(!node.async);
526     node = makeNode("(async function() {})", true);
527     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.FunctionExpression);
528     InspectorTest.assert(node.async);
529     node = makeNode("(function() {})", true);
530     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.FunctionExpression);
531     InspectorTest.assert(!node.async);
532     node = makeNode("async () => {}", true);
533     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ArrowFunctionExpression);
534     InspectorTest.assert(node.async);
535     node = makeNode("() => {}", true);
536     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.ArrowFunctionExpression);
537     InspectorTest.assert(!node.async);
538     InspectorTest.log("passed Async Functions");
539
540     node = makeNode("async function foo() { await foo(); }", false);
541     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.FunctionDeclaration);
542     InspectorTest.assert(node.async);
543     InspectorTest.assert(node.body.body[0].expression.type === WebInspector.ScriptSyntaxTree.NodeType.AwaitExpression);
544     InspectorTest.assert(node.body.body[0].expression.argument.type === WebInspector.ScriptSyntaxTree.NodeType.CallExpression);
545     node = makeNode("async function foo() { await 1; }", false);
546     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.FunctionDeclaration);
547     InspectorTest.assert(node.async);
548     InspectorTest.assert(node.body.body[0].expression.type === WebInspector.ScriptSyntaxTree.NodeType.AwaitExpression);
549     InspectorTest.assert(node.body.body[0].expression.argument.type === WebInspector.ScriptSyntaxTree.NodeType.Literal);
550     InspectorTest.assert(node.body.body[0].expression.argument.value === 1);
551     InspectorTest.log("passed AwaitExpression");
552
553     node = makeNode("var o = {['c']() { }};", false);
554     //                        ^
555     //                 type profiling return divot.
556     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.VariableDeclaration);
557     InspectorTest.assert(node.declarations.length === 1);
558     InspectorTest.assert(node.declarations[0].type === WebInspector.ScriptSyntaxTree.NodeType.VariableDeclarator);
559     InspectorTest.assert(node.declarations[0].init.type === WebInspector.ScriptSyntaxTree.NodeType.ObjectExpression);
560     InspectorTest.assert(node.declarations[0].init.properties.length === 1);
561     InspectorTest.assert(node.declarations[0].init.properties[0].type === WebInspector.ScriptSyntaxTree.NodeType.Property);
562     InspectorTest.assert(!!node.declarations[0].init.properties[0].method);
563     InspectorTest.assert(!!node.declarations[0].init.properties[0].computed);
564     InspectorTest.assert(node.declarations[0].init.properties[0].value.type === WebInspector.ScriptSyntaxTree.NodeType.FunctionExpression);
565     InspectorTest.assert(node.declarations[0].init.properties[0].value.typeProfilingReturnDivot === 9);
566     InspectorTest.log("passed computed method on object literal");
567
568     node = makeNode("var o = { m(){ } };", false);
569     //                         ^
570     //                 type profiling return divot.
571     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.VariableDeclaration);
572     InspectorTest.assert(node.declarations.length === 1);
573     InspectorTest.assert(node.declarations[0].type === WebInspector.ScriptSyntaxTree.NodeType.VariableDeclarator);
574     InspectorTest.assert(node.declarations[0].init.type === WebInspector.ScriptSyntaxTree.NodeType.ObjectExpression);
575     InspectorTest.assert(node.declarations[0].init.properties.length === 1);
576     InspectorTest.assert(node.declarations[0].init.properties[0].type === WebInspector.ScriptSyntaxTree.NodeType.Property);
577     InspectorTest.assert(!!node.declarations[0].init.properties[0].method);
578     InspectorTest.assert(!node.declarations[0].init.properties[0].computed);
579     InspectorTest.assert(node.declarations[0].init.properties[0].value.type === WebInspector.ScriptSyntaxTree.NodeType.FunctionExpression);
580     InspectorTest.assert(node.declarations[0].init.properties[0].value.typeProfilingReturnDivot === 10);
581     InspectorTest.log("passed method on object literal");
582
583     node = makeNode("var o = {['c']: function(){ } };", false);
584     //                               ^
585     //                    type profiling return divot.
586     InspectorTest.assert(node.type === WebInspector.ScriptSyntaxTree.NodeType.VariableDeclaration);
587     InspectorTest.assert(node.declarations.length === 1);
588     InspectorTest.assert(node.declarations[0].type === WebInspector.ScriptSyntaxTree.NodeType.VariableDeclarator);
589     InspectorTest.assert(node.declarations[0].init.type === WebInspector.ScriptSyntaxTree.NodeType.ObjectExpression);
590     InspectorTest.assert(node.declarations[0].init.properties.length === 1);
591     InspectorTest.assert(node.declarations[0].init.properties[0].type === WebInspector.ScriptSyntaxTree.NodeType.Property);
592     InspectorTest.assert(!node.declarations[0].init.properties[0].method);
593     InspectorTest.assert(!!node.declarations[0].init.properties[0].computed);
594     InspectorTest.assert(node.declarations[0].init.properties[0].value.type === WebInspector.ScriptSyntaxTree.NodeType.FunctionExpression);
595     InspectorTest.assert(node.declarations[0].init.properties[0].value.typeProfilingReturnDivot === 16);
596     InspectorTest.log("passed computed method property on object literal");
597
598     InspectorTest.log("passed ALL TESTS");
599     InspectorTest.completeTest();
600 }
601 </script>
602 </head>
603 <body onload="runTest()">
604 </body>
605 </html>