browser/devtools/debugger/test/browser_dbg_parser-06.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:5ca31711ceba
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 /**
5 * Check that some potentially problematic identifier nodes have the
6 * right location information attached.
7 */
8
9 function test() {
10 let { Parser, ParserHelpers, SyntaxTreeVisitor } =
11 Cu.import("resource:///modules/devtools/Parser.jsm", {});
12
13 function verify(source, predicate, [sline, scol], [eline, ecol]) {
14 let ast = Parser.reflectionAPI.parse(source);
15 let node = SyntaxTreeVisitor.filter(ast, predicate).pop();
16 let loc = ParserHelpers.getNodeLocation(node);
17
18 is(loc.start.toSource(), { line: sline, column: scol }.toSource(),
19 "The start location was correct for the identifier in: '" + source + "'.");
20 is(loc.end.toSource(), { line: eline, column: ecol }.toSource(),
21 "The end location was correct for the identifier in: '" + source + "'.");
22 }
23
24 // FunctionDeclarations and FunctionExpressions.
25
26 // The location is unavailable for the identifier node "foo".
27 verify("function foo(){}", e => e.name == "foo", [1, 9], [1, 12]);
28 verify("\nfunction\nfoo\n(\n)\n{\n}\n", e => e.name == "foo", [3, 0], [3, 3]);
29
30 verify("({bar:function foo(){}})", e => e.name == "foo", [1, 15], [1, 18]);
31 verify("(\n{\nbar\n:\nfunction\nfoo\n(\n)\n{\n}\n}\n)", e => e.name == "foo", [6, 0], [6, 3]);
32
33 // Just to be sure, check the identifier node "bar" as well.
34 verify("({bar:function foo(){}})", e => e.name == "bar", [1, 2], [1, 5]);
35 verify("(\n{\nbar\n:\nfunction\nfoo\n(\n)\n{\n}\n}\n)", e => e.name == "bar", [3, 0], [3, 3]);
36
37 // MemberExpressions.
38
39 // The location is unavailable for the identifier node "bar".
40 verify("foo.bar", e => e.name == "bar", [1, 4], [1, 7]);
41 verify("\nfoo\n.\nbar\n", e => e.name == "bar", [4, 0], [4, 3]);
42
43 // Just to be sure, check the identifier node "foo" as well.
44 verify("foo.bar", e => e.name == "foo", [1, 0], [1, 3]);
45 verify("\nfoo\n.\nbar\n", e => e.name == "foo", [2, 0], [2, 3]);
46
47 // VariableDeclarator
48
49 // The location is incorrect for the identifier node "foo".
50 verify("let foo = bar", e => e.name == "foo", [1, 4], [1, 7]);
51 verify("\nlet\nfoo\n=\nbar\n", e => e.name == "foo", [3, 0], [3, 3]);
52
53 // Just to be sure, check the identifier node "bar" as well.
54 verify("let foo = bar", e => e.name == "bar", [1, 10], [1, 13]);
55 verify("\nlet\nfoo\n=\nbar\n", e => e.name == "bar", [5, 0], [5, 3]);
56
57 // Just to be sure, check AssignmentExpreesions as well.
58 verify("foo = bar", e => e.name == "foo", [1, 0], [1, 3]);
59 verify("\nfoo\n=\nbar\n", e => e.name == "foo", [2, 0], [2, 3]);
60 verify("foo = bar", e => e.name == "bar", [1, 6], [1, 9]);
61 verify("\nfoo\n=\nbar\n", e => e.name == "bar", [4, 0], [4, 3]);
62
63 // LabeledStatement and ContinueStatement, because it's 1968 again
64
65 verify("foo: bar", e => e.name == "foo", [1, 0], [1, 3]);
66 verify("\nfoo\n:\nbar\n", e => e.name == "foo", [2, 0], [2, 3]);
67
68 verify("foo: for(;;) continue foo", e => e.name == "foo", [1, 22], [1, 25]);
69 verify("\nfoo\n:\nfor(\n;\n;\n)\ncontinue\nfoo\n", e => e.name == "foo", [9, 0], [9, 3]);
70
71 finish();
72 }

mercurial