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
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
/**
* Test that creating an evaluation string for certain nodes works properly.
*/
function test() {
let { Parser, ParserHelpers, SyntaxTreeVisitor } =
Cu.import("resource:///modules/devtools/Parser.jsm", {});
function verify(source, predicate, string) {
let ast = Parser.reflectionAPI.parse(source);
let node = SyntaxTreeVisitor.filter(ast, predicate).pop();
let info = ParserHelpers.getIdentifierEvalString(node);
is(info, string, "The identifier evaluation string is correct.");
}
// Indentifier or Literal
verify("foo", e => e.type == "Identifier", "foo");
verify("undefined", e => e.type == "Identifier", "undefined");
verify("null", e => e.type == "Literal", "null");
verify("42", e => e.type == "Literal", "42");
verify("true", e => e.type == "Literal", "true");
verify("\"nasu\"", e => e.type == "Literal", "\"nasu\"");
// MemberExpression or ThisExpression
verify("this", e => e.type == "ThisExpression", "this");
verify("foo.bar", e => e.name == "foo", "foo");
verify("foo.bar", e => e.name == "bar", "foo.bar");
// MemberExpression + ThisExpression
verify("this.foo.bar", e => e.type == "ThisExpression", "this");
verify("this.foo.bar", e => e.name == "foo", "this.foo");
verify("this.foo.bar", e => e.name == "bar", "this.foo.bar");
verify("foo.this.bar", e => e.name == "foo", "foo");
verify("foo.this.bar", e => e.name == "this", "foo.this");
verify("foo.this.bar", e => e.name == "bar", "foo.this.bar");
// ObjectExpression + VariableDeclarator
verify("let foo={bar:baz}", e => e.name == "baz", "baz");
verify("let foo={bar:undefined}", e => e.name == "undefined", "undefined");
verify("let foo={bar:null}", e => e.type == "Literal", "null");
verify("let foo={bar:42}", e => e.type == "Literal", "42");
verify("let foo={bar:true}", e => e.type == "Literal", "true");
verify("let foo={bar:\"nasu\"}", e => e.type == "Literal", "\"nasu\"");
verify("let foo={bar:this}", e => e.type == "ThisExpression", "this");
verify("let foo={bar:{nested:baz}}", e => e.name == "baz", "baz");
verify("let foo={bar:{nested:undefined}}", e => e.name == "undefined", "undefined");
verify("let foo={bar:{nested:null}}", e => e.type == "Literal", "null");
verify("let foo={bar:{nested:42}}", e => e.type == "Literal", "42");
verify("let foo={bar:{nested:true}}", e => e.type == "Literal", "true");
verify("let foo={bar:{nested:\"nasu\"}}", e => e.type == "Literal", "\"nasu\"");
verify("let foo={bar:{nested:this}}", e => e.type == "ThisExpression", "this");
verify("let foo={bar:baz}", e => e.name == "bar", "foo.bar");
verify("let foo={bar:baz}", e => e.name == "foo", "foo");
verify("let foo={bar:{nested:baz}}", e => e.name == "nested", "foo.bar.nested");
verify("let foo={bar:{nested:baz}}", e => e.name == "bar", "foo.bar");
verify("let foo={bar:{nested:baz}}", e => e.name == "foo", "foo");
// ObjectExpression + MemberExpression
verify("parent.foo={bar:baz}", e => e.name == "bar", "parent.foo.bar");
verify("parent.foo={bar:baz}", e => e.name == "foo", "parent.foo");
verify("parent.foo={bar:baz}", e => e.name == "parent", "parent");
verify("parent.foo={bar:{nested:baz}}", e => e.name == "nested", "parent.foo.bar.nested");
verify("parent.foo={bar:{nested:baz}}", e => e.name == "bar", "parent.foo.bar");
verify("parent.foo={bar:{nested:baz}}", e => e.name == "foo", "parent.foo");
verify("parent.foo={bar:{nested:baz}}", e => e.name == "parent", "parent");
verify("this.foo={bar:{nested:baz}}", e => e.name == "nested", "this.foo.bar.nested");
verify("this.foo={bar:{nested:baz}}", e => e.name == "bar", "this.foo.bar");
verify("this.foo={bar:{nested:baz}}", e => e.name == "foo", "this.foo");
verify("this.foo={bar:{nested:baz}}", e => e.type == "ThisExpression", "this");
verify("this.parent.foo={bar:{nested:baz}}", e => e.name == "nested", "this.parent.foo.bar.nested");
verify("this.parent.foo={bar:{nested:baz}}", e => e.name == "bar", "this.parent.foo.bar");
verify("this.parent.foo={bar:{nested:baz}}", e => e.name == "foo", "this.parent.foo");
verify("this.parent.foo={bar:{nested:baz}}", e => e.name == "parent", "this.parent");
verify("this.parent.foo={bar:{nested:baz}}", e => e.type == "ThisExpression", "this");
verify("parent.this.foo={bar:{nested:baz}}", e => e.name == "nested", "parent.this.foo.bar.nested");
verify("parent.this.foo={bar:{nested:baz}}", e => e.name == "bar", "parent.this.foo.bar");
verify("parent.this.foo={bar:{nested:baz}}", e => e.name == "foo", "parent.this.foo");
verify("parent.this.foo={bar:{nested:baz}}", e => e.name == "this", "parent.this");
verify("parent.this.foo={bar:{nested:baz}}", e => e.name == "parent", "parent");
// FunctionExpression
verify("function foo(){}", e => e.name == "foo", "foo");
verify("var foo=function(){}", e => e.name == "foo", "foo");
verify("var foo=function bar(){}", e => e.name == "bar", "bar");
// New/CallExpression
verify("foo()", e => e.name == "foo", "foo");
verify("new foo()", e => e.name == "foo", "foo");
verify("foo(bar)", e => e.name == "bar", "bar");
verify("foo(bar, baz)", e => e.name == "baz", "baz");
verify("foo(undefined)", e => e.name == "undefined", "undefined");
verify("foo(null)", e => e.type == "Literal", "null");
verify("foo(42)", e => e.type == "Literal", "42");
verify("foo(true)", e => e.type == "Literal", "true");
verify("foo(\"nasu\")", e => e.type == "Literal", "\"nasu\"");
verify("foo(this)", e => e.type == "ThisExpression", "this");
// New/CallExpression + ObjectExpression + MemberExpression
verify("fun(this.parent.foo={bar:{nested:baz}})", e => e.name == "nested", "this.parent.foo.bar.nested");
verify("fun(this.parent.foo={bar:{nested:baz}})", e => e.name == "bar", "this.parent.foo.bar");
verify("fun(this.parent.foo={bar:{nested:baz}})", e => e.name == "foo", "this.parent.foo");
verify("fun(this.parent.foo={bar:{nested:baz}})", e => e.name == "parent", "this.parent");
verify("fun(this.parent.foo={bar:{nested:baz}})", e => e.type == "ThisExpression", "this");
verify("fun(this.parent.foo={bar:{nested:baz}})", e => e.name == "fun", "fun");
finish();
}
|