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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
/**
* Test that inferring anonymous function information is done correctly.
*/
function test() {
let { Parser, ParserHelpers, SyntaxTreeVisitor } =
Cu.import("resource://devtools/shared/Parser.jsm", {});
function verify(source, predicate, details) {
let { name, chain } = details;
let [[sline, scol], [eline, ecol]] = details.loc;
let ast = Parser.reflectionAPI.parse(source);
let node = SyntaxTreeVisitor.filter(ast, predicate).pop();
let info = ParserHelpers.inferFunctionExpressionInfo(node);
is(info.name, name,
"The function expression assignment property name is correct.");
is(chain ? info.chain.toSource() : info.chain, chain ? chain.toSource() : chain,
"The function expression assignment property chain is correct.");
is(info.loc.start.toSource(), { line: sline, column: scol }.toSource(),
"The start location was correct for the identifier in: '" + source + "'.");
is(info.loc.end.toSource(), { line: eline, column: ecol }.toSource(),
"The end location was correct for the identifier in: '" + source + "'.");
}
// VariableDeclarator
verify("var foo=function(){}", e => e.type == "FunctionExpression", {
name: "foo",
chain: null,
loc: [[1, 4], [1, 7]]
});
verify("\nvar\nfoo\n=\nfunction\n(\n)\n{\n}\n", e => e.type == "FunctionExpression", {
name: "foo",
chain: null,
loc: [[3, 0], [3, 3]]
});
// AssignmentExpression
verify("foo=function(){}", e => e.type == "FunctionExpression",
{ name: "foo", chain: [], loc: [[1, 0], [1, 3]] });
verify("\nfoo\n=\nfunction\n(\n)\n{\n}\n", e => e.type == "FunctionExpression",
{ name: "foo", chain: [], loc: [[2, 0], [2, 3]] });
verify("foo.bar=function(){}", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["foo"], loc: [[1, 0], [1, 7]] });
verify("\nfoo.bar\n=\nfunction\n(\n)\n{\n}\n", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["foo"], loc: [[2, 0], [2, 7]] });
verify("this.foo=function(){}", e => e.type == "FunctionExpression",
{ name: "foo", chain: ["this"], loc: [[1, 0], [1, 8]] });
verify("\nthis.foo\n=\nfunction\n(\n)\n{\n}\n", e => e.type == "FunctionExpression",
{ name: "foo", chain: ["this"], loc: [[2, 0], [2, 8]] });
verify("this.foo.bar=function(){}", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["this", "foo"], loc: [[1, 0], [1, 12]] });
verify("\nthis.foo.bar\n=\nfunction\n(\n)\n{\n}\n", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["this", "foo"], loc: [[2, 0], [2, 12]] });
verify("foo.this.bar=function(){}", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["foo", "this"], loc: [[1, 0], [1, 12]] });
verify("\nfoo.this.bar\n=\nfunction\n(\n)\n{\n}\n", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["foo", "this"], loc: [[2, 0], [2, 12]] });
// ObjectExpression
verify("({foo:function(){}})", e => e.type == "FunctionExpression",
{ name: "foo", chain: [], loc: [[1, 2], [1, 5]] });
verify("(\n{\nfoo\n:\nfunction\n(\n)\n{\n}\n}\n)", e => e.type == "FunctionExpression",
{ name: "foo", chain: [], loc: [[3, 0], [3, 3]] });
verify("({foo:{bar:function(){}}})", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["foo"], loc: [[1, 7], [1, 10]] });
verify("(\n{\nfoo\n:\n{\nbar\n:\nfunction\n(\n)\n{\n}\n}\n}\n)", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["foo"], loc: [[6, 0], [6, 3]] });
// AssignmentExpression + ObjectExpression
verify("foo={bar:function(){}}", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["foo"], loc: [[1, 5], [1, 8]] });
verify("\nfoo\n=\n{\nbar\n:\nfunction\n(\n)\n{\n}\n}\n", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["foo"], loc: [[5, 0], [5, 3]] });
verify("foo={bar:{baz:function(){}}}", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["foo", "bar"], loc: [[1, 10], [1, 13]] });
verify("\nfoo\n=\n{\nbar\n:\n{\nbaz\n:\nfunction\n(\n)\n{\n}\n}\n}\n", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["foo", "bar"], loc: [[8, 0], [8, 3]] });
verify("nested.foo={bar:function(){}}", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["nested", "foo"], loc: [[1, 12], [1, 15]] });
verify("\nnested.foo\n=\n{\nbar\n:\nfunction\n(\n)\n{\n}\n}\n", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["nested", "foo"], loc: [[5, 0], [5, 3]] });
verify("nested.foo={bar:{baz:function(){}}}", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["nested", "foo", "bar"], loc: [[1, 17], [1, 20]] });
verify("\nnested.foo\n=\n{\nbar\n:\n{\nbaz\n:\nfunction\n(\n)\n{\n}\n}\n}\n", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["nested", "foo", "bar"], loc: [[8, 0], [8, 3]] });
verify("this.foo={bar:function(){}}", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["this", "foo"], loc: [[1, 10], [1, 13]] });
verify("\nthis.foo\n=\n{\nbar\n:\nfunction\n(\n)\n{\n}\n}\n", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["this", "foo"], loc: [[5, 0], [5, 3]] });
verify("this.foo={bar:{baz:function(){}}}", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["this", "foo", "bar"], loc: [[1, 15], [1, 18]] });
verify("\nthis.foo\n=\n{\nbar\n:\n{\nbaz\n:\nfunction\n(\n)\n{\n}\n}\n}\n", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["this", "foo", "bar"], loc: [[8, 0], [8, 3]] });
verify("this.nested.foo={bar:function(){}}", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["this", "nested", "foo"], loc: [[1, 17], [1, 20]] });
verify("\nthis.nested.foo\n=\n{\nbar\n:\nfunction\n(\n)\n{\n}\n}\n", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["this", "nested", "foo"], loc: [[5, 0], [5, 3]] });
verify("this.nested.foo={bar:{baz:function(){}}}", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["this", "nested", "foo", "bar"], loc: [[1, 22], [1, 25]] });
verify("\nthis.nested.foo\n=\n{\nbar\n:\n{\nbaz\n:\nfunction\n(\n)\n{\n}\n}\n}\n", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["this", "nested", "foo", "bar"], loc: [[8, 0], [8, 3]] });
verify("nested.this.foo={bar:function(){}}", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["nested", "this", "foo"], loc: [[1, 17], [1, 20]] });
verify("\nnested.this.foo\n=\n{\nbar\n:\nfunction\n(\n)\n{\n}\n}\n", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["nested", "this", "foo"], loc: [[5, 0], [5, 3]] });
verify("nested.this.foo={bar:{baz:function(){}}}", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["nested", "this", "foo", "bar"], loc: [[1, 22], [1, 25]] });
verify("\nnested.this.foo\n=\n{\nbar\n:\n{\nbaz\n:\nfunction\n(\n)\n{\n}\n}\n}\n", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["nested", "this", "foo", "bar"], loc: [[8, 0], [8, 3]] });
// VariableDeclarator + AssignmentExpression + ObjectExpression
verify("let foo={bar:function(){}}", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["foo"], loc: [[1, 9], [1, 12]] });
verify("\nlet\nfoo\n=\n{\nbar\n:\nfunction\n(\n)\n{\n}\n}\n", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["foo"], loc: [[6, 0], [6, 3]] });
verify("let foo={bar:{baz:function(){}}}", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["foo", "bar"], loc: [[1, 14], [1, 17]] });
verify("\nlet\nfoo\n=\n{\nbar\n:\n{\nbaz\n:\nfunction\n(\n)\n{\n}\n}\n}\n", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["foo", "bar"], loc: [[9, 0], [9, 3]] });
// New/CallExpression + AssignmentExpression + ObjectExpression
verify("foo({bar:function(){}})", e => e.type == "FunctionExpression",
{ name: "bar", chain: [], loc: [[1, 5], [1, 8]] });
verify("\nfoo\n(\n{\nbar\n:\nfunction\n(\n)\n{\n}\n}\n)\n", e => e.type == "FunctionExpression",
{ name: "bar", chain: [], loc: [[5, 0], [5, 3]] });
verify("foo({bar:{baz:function(){}}})", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["bar"], loc: [[1, 10], [1, 13]] });
verify("\nfoo\n(\n{\nbar\n:\n{\nbaz\n:\nfunction\n(\n)\n{\n}\n}\n}\n)\n", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["bar"], loc: [[8, 0], [8, 3]] });
verify("nested.foo({bar:function(){}})", e => e.type == "FunctionExpression",
{ name: "bar", chain: [], loc: [[1, 12], [1, 15]] });
verify("\nnested.foo\n(\n{\nbar\n:\nfunction\n(\n)\n{\n}\n}\n)\n", e => e.type == "FunctionExpression",
{ name: "bar", chain: [], loc: [[5, 0], [5, 3]] });
verify("nested.foo({bar:{baz:function(){}}})", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["bar"], loc: [[1, 17], [1, 20]] });
verify("\nnested.foo\n(\n{\nbar\n:\n{\nbaz\n:\nfunction\n(\n)\n{\n}\n}\n}\n)\n", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["bar"], loc: [[8, 0], [8, 3]] });
verify("this.foo({bar:function(){}})", e => e.type == "FunctionExpression",
{ name: "bar", chain: [], loc: [[1, 10], [1, 13]] });
verify("\nthis.foo\n(\n{\nbar\n:\nfunction\n(\n)\n{\n}\n}\n)\n", e => e.type == "FunctionExpression",
{ name: "bar", chain: [], loc: [[5, 0], [5, 3]] });
verify("this.foo({bar:{baz:function(){}}})", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["bar"], loc: [[1, 15], [1, 18]] });
verify("\nthis.foo\n(\n{\nbar\n:\n{\nbaz\n:\nfunction\n(\n)\n{\n}\n}\n}\n)\n", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["bar"], loc: [[8, 0], [8, 3]] });
verify("this.nested.foo({bar:function(){}})", e => e.type == "FunctionExpression",
{ name: "bar", chain: [], loc: [[1, 17], [1, 20]] });
verify("\nthis.nested.foo\n(\n{\nbar\n:\nfunction\n(\n)\n{\n}\n}\n)\n", e => e.type == "FunctionExpression",
{ name: "bar", chain: [], loc: [[5, 0], [5, 3]] });
verify("this.nested.foo({bar:{baz:function(){}}})", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["bar"], loc: [[1, 22], [1, 25]] });
verify("\nthis.nested.foo\n(\n{\nbar\n:\n{\nbaz\n:\nfunction\n(\n)\n{\n}\n}\n}\n)\n", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["bar"], loc: [[8, 0], [8, 3]] });
verify("nested.this.foo({bar:function(){}})", e => e.type == "FunctionExpression",
{ name: "bar", chain: [], loc: [[1, 17], [1, 20]] });
verify("\nnested.this.foo\n(\n{\nbar\n:\nfunction\n(\n)\n{\n}\n}\n)\n", e => e.type == "FunctionExpression",
{ name: "bar", chain: [], loc: [[5, 0], [5, 3]] });
verify("nested.this.foo({bar:{baz:function(){}}})", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["bar"], loc: [[1, 22], [1, 25]] });
verify("\nnested.this.foo\n(\n{\nbar\n:\n{\nbaz\n:\nfunction\n(\n)\n{\n}\n}\n}\n)\n", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["bar"], loc: [[8, 0], [8, 3]] });
// New/CallExpression + VariableDeclarator + AssignmentExpression + ObjectExpression
verify("let target=foo({bar:function(){}})", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["target"], loc: [[1, 16], [1, 19]] });
verify("\nlet\ntarget=\nfoo\n(\n{\nbar\n:\nfunction\n(\n)\n{\n}\n}\n)\n", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["target"], loc: [[7, 0], [7, 3]] });
verify("let target=foo({bar:{baz:function(){}}})", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["target", "bar"], loc: [[1, 21], [1, 24]] });
verify("\nlet\ntarget=\nfoo\n(\n{\nbar\n:\n{\nbaz\n:\nfunction\n(\n)\n{\n}\n}\n}\n)\n", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["target", "bar"], loc: [[10, 0], [10, 3]] });
verify("let target=nested.foo({bar:function(){}})", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["target"], loc: [[1, 23], [1, 26]] });
verify("\nlet\ntarget=\nnested.foo\n(\n{\nbar\n:\nfunction\n(\n)\n{\n}\n}\n)\n", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["target"], loc: [[7, 0], [7, 3]] });
verify("let target=nested.foo({bar:{baz:function(){}}})", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["target", "bar"], loc: [[1, 28], [1, 31]] });
verify("\nlet\ntarget=\nnested.foo\n(\n{\nbar\n:\n{\nbaz\n:\nfunction\n(\n)\n{\n}\n}\n}\n)\n", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["target", "bar"], loc: [[10, 0], [10, 3]] });
verify("let target=this.foo({bar:function(){}})", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["target"], loc: [[1, 21], [1, 24]] });
verify("\nlet\ntarget=\nthis.foo\n(\n{\nbar\n:\nfunction\n(\n)\n{\n}\n}\n)\n", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["target"], loc: [[7, 0], [7, 3]] });
verify("let target=this.foo({bar:{baz:function(){}}})", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["target", "bar"], loc: [[1, 26], [1, 29]] });
verify("\nlet\ntarget=\nthis.foo\n(\n{\nbar\n:\n{\nbaz\n:\nfunction\n(\n)\n{\n}\n}\n}\n)\n", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["target", "bar"], loc: [[10, 0], [10, 3]] });
verify("let target=this.nested.foo({bar:function(){}})", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["target"], loc: [[1, 28], [1, 31]] });
verify("\nlet\ntarget=\nthis.nested.foo\n(\n{\nbar\n:\nfunction\n(\n)\n{\n}\n}\n)\n", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["target"], loc: [[7, 0], [7, 3]] });
verify("let target=this.nested.foo({bar:{baz:function(){}}})", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["target", "bar"], loc: [[1, 33], [1, 36]] });
verify("\nlet\ntarget=\nthis.nested.foo\n(\n{\nbar\n:\n{\nbaz\n:\nfunction\n(\n)\n{\n}\n}\n}\n)\n", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["target", "bar"], loc: [[10, 0], [10, 3]] });
verify("let target=nested.this.foo({bar:function(){}})", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["target"], loc: [[1, 28], [1, 31]] });
verify("\nlet\ntarget=\nnested.this.foo\n(\n{\nbar\n:\nfunction\n(\n)\n{\n}\n}\n)\n", e => e.type == "FunctionExpression",
{ name: "bar", chain: ["target"], loc: [[7, 0], [7, 3]] });
verify("let target=nested.this.foo({bar:{baz:function(){}}})", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["target", "bar"], loc: [[1, 33], [1, 36]] });
verify("\nlet\ntarget=\nnested.this.foo\n(\n{\nbar\n:\n{\nbaz\n:\nfunction\n(\n)\n{\n}\n}\n}\n)\n", e => e.type == "FunctionExpression",
{ name: "baz", chain: ["target", "bar"], loc: [[10, 0], [10, 3]] });
finish();
}
|