|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 /** |
|
5 * Test that creating an evaluation string for certain nodes works properly. |
|
6 */ |
|
7 |
|
8 function test() { |
|
9 let { Parser, ParserHelpers, SyntaxTreeVisitor } = |
|
10 Cu.import("resource:///modules/devtools/Parser.jsm", {}); |
|
11 |
|
12 function verify(source, predicate, string) { |
|
13 let ast = Parser.reflectionAPI.parse(source); |
|
14 let node = SyntaxTreeVisitor.filter(ast, predicate).pop(); |
|
15 let info = ParserHelpers.getIdentifierEvalString(node); |
|
16 is(info, string, "The identifier evaluation string is correct."); |
|
17 } |
|
18 |
|
19 // Indentifier or Literal |
|
20 |
|
21 verify("foo", e => e.type == "Identifier", "foo"); |
|
22 verify("undefined", e => e.type == "Identifier", "undefined"); |
|
23 verify("null", e => e.type == "Literal", "null"); |
|
24 verify("42", e => e.type == "Literal", "42"); |
|
25 verify("true", e => e.type == "Literal", "true"); |
|
26 verify("\"nasu\"", e => e.type == "Literal", "\"nasu\""); |
|
27 |
|
28 // MemberExpression or ThisExpression |
|
29 |
|
30 verify("this", e => e.type == "ThisExpression", "this"); |
|
31 verify("foo.bar", e => e.name == "foo", "foo"); |
|
32 verify("foo.bar", e => e.name == "bar", "foo.bar"); |
|
33 |
|
34 // MemberExpression + ThisExpression |
|
35 |
|
36 verify("this.foo.bar", e => e.type == "ThisExpression", "this"); |
|
37 verify("this.foo.bar", e => e.name == "foo", "this.foo"); |
|
38 verify("this.foo.bar", e => e.name == "bar", "this.foo.bar"); |
|
39 |
|
40 verify("foo.this.bar", e => e.name == "foo", "foo"); |
|
41 verify("foo.this.bar", e => e.name == "this", "foo.this"); |
|
42 verify("foo.this.bar", e => e.name == "bar", "foo.this.bar"); |
|
43 |
|
44 // ObjectExpression + VariableDeclarator |
|
45 |
|
46 verify("let foo={bar:baz}", e => e.name == "baz", "baz"); |
|
47 verify("let foo={bar:undefined}", e => e.name == "undefined", "undefined"); |
|
48 verify("let foo={bar:null}", e => e.type == "Literal", "null"); |
|
49 verify("let foo={bar:42}", e => e.type == "Literal", "42"); |
|
50 verify("let foo={bar:true}", e => e.type == "Literal", "true"); |
|
51 verify("let foo={bar:\"nasu\"}", e => e.type == "Literal", "\"nasu\""); |
|
52 verify("let foo={bar:this}", e => e.type == "ThisExpression", "this"); |
|
53 |
|
54 verify("let foo={bar:{nested:baz}}", e => e.name == "baz", "baz"); |
|
55 verify("let foo={bar:{nested:undefined}}", e => e.name == "undefined", "undefined"); |
|
56 verify("let foo={bar:{nested:null}}", e => e.type == "Literal", "null"); |
|
57 verify("let foo={bar:{nested:42}}", e => e.type == "Literal", "42"); |
|
58 verify("let foo={bar:{nested:true}}", e => e.type == "Literal", "true"); |
|
59 verify("let foo={bar:{nested:\"nasu\"}}", e => e.type == "Literal", "\"nasu\""); |
|
60 verify("let foo={bar:{nested:this}}", e => e.type == "ThisExpression", "this"); |
|
61 |
|
62 verify("let foo={bar:baz}", e => e.name == "bar", "foo.bar"); |
|
63 verify("let foo={bar:baz}", e => e.name == "foo", "foo"); |
|
64 |
|
65 verify("let foo={bar:{nested:baz}}", e => e.name == "nested", "foo.bar.nested"); |
|
66 verify("let foo={bar:{nested:baz}}", e => e.name == "bar", "foo.bar"); |
|
67 verify("let foo={bar:{nested:baz}}", e => e.name == "foo", "foo"); |
|
68 |
|
69 // ObjectExpression + MemberExpression |
|
70 |
|
71 verify("parent.foo={bar:baz}", e => e.name == "bar", "parent.foo.bar"); |
|
72 verify("parent.foo={bar:baz}", e => e.name == "foo", "parent.foo"); |
|
73 verify("parent.foo={bar:baz}", e => e.name == "parent", "parent"); |
|
74 |
|
75 verify("parent.foo={bar:{nested:baz}}", e => e.name == "nested", "parent.foo.bar.nested"); |
|
76 verify("parent.foo={bar:{nested:baz}}", e => e.name == "bar", "parent.foo.bar"); |
|
77 verify("parent.foo={bar:{nested:baz}}", e => e.name == "foo", "parent.foo"); |
|
78 verify("parent.foo={bar:{nested:baz}}", e => e.name == "parent", "parent"); |
|
79 |
|
80 verify("this.foo={bar:{nested:baz}}", e => e.name == "nested", "this.foo.bar.nested"); |
|
81 verify("this.foo={bar:{nested:baz}}", e => e.name == "bar", "this.foo.bar"); |
|
82 verify("this.foo={bar:{nested:baz}}", e => e.name == "foo", "this.foo"); |
|
83 verify("this.foo={bar:{nested:baz}}", e => e.type == "ThisExpression", "this"); |
|
84 |
|
85 verify("this.parent.foo={bar:{nested:baz}}", e => e.name == "nested", "this.parent.foo.bar.nested"); |
|
86 verify("this.parent.foo={bar:{nested:baz}}", e => e.name == "bar", "this.parent.foo.bar"); |
|
87 verify("this.parent.foo={bar:{nested:baz}}", e => e.name == "foo", "this.parent.foo"); |
|
88 verify("this.parent.foo={bar:{nested:baz}}", e => e.name == "parent", "this.parent"); |
|
89 verify("this.parent.foo={bar:{nested:baz}}", e => e.type == "ThisExpression", "this"); |
|
90 |
|
91 verify("parent.this.foo={bar:{nested:baz}}", e => e.name == "nested", "parent.this.foo.bar.nested"); |
|
92 verify("parent.this.foo={bar:{nested:baz}}", e => e.name == "bar", "parent.this.foo.bar"); |
|
93 verify("parent.this.foo={bar:{nested:baz}}", e => e.name == "foo", "parent.this.foo"); |
|
94 verify("parent.this.foo={bar:{nested:baz}}", e => e.name == "this", "parent.this"); |
|
95 verify("parent.this.foo={bar:{nested:baz}}", e => e.name == "parent", "parent"); |
|
96 |
|
97 // FunctionExpression |
|
98 |
|
99 verify("function foo(){}", e => e.name == "foo", "foo"); |
|
100 verify("var foo=function(){}", e => e.name == "foo", "foo"); |
|
101 verify("var foo=function bar(){}", e => e.name == "bar", "bar"); |
|
102 |
|
103 // New/CallExpression |
|
104 |
|
105 verify("foo()", e => e.name == "foo", "foo"); |
|
106 verify("new foo()", e => e.name == "foo", "foo"); |
|
107 |
|
108 verify("foo(bar)", e => e.name == "bar", "bar"); |
|
109 verify("foo(bar, baz)", e => e.name == "baz", "baz"); |
|
110 verify("foo(undefined)", e => e.name == "undefined", "undefined"); |
|
111 verify("foo(null)", e => e.type == "Literal", "null"); |
|
112 verify("foo(42)", e => e.type == "Literal", "42"); |
|
113 verify("foo(true)", e => e.type == "Literal", "true"); |
|
114 verify("foo(\"nasu\")", e => e.type == "Literal", "\"nasu\""); |
|
115 verify("foo(this)", e => e.type == "ThisExpression", "this"); |
|
116 |
|
117 // New/CallExpression + ObjectExpression + MemberExpression |
|
118 |
|
119 verify("fun(this.parent.foo={bar:{nested:baz}})", e => e.name == "nested", "this.parent.foo.bar.nested"); |
|
120 verify("fun(this.parent.foo={bar:{nested:baz}})", e => e.name == "bar", "this.parent.foo.bar"); |
|
121 verify("fun(this.parent.foo={bar:{nested:baz}})", e => e.name == "foo", "this.parent.foo"); |
|
122 verify("fun(this.parent.foo={bar:{nested:baz}})", e => e.name == "parent", "this.parent"); |
|
123 verify("fun(this.parent.foo={bar:{nested:baz}})", e => e.type == "ThisExpression", "this"); |
|
124 verify("fun(this.parent.foo={bar:{nested:baz}})", e => e.name == "fun", "fun"); |
|
125 |
|
126 finish(); |
|
127 } |