|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 /** |
|
5 * Test that inferring anonymous function information is done correctly |
|
6 * from arrow expressions. |
|
7 */ |
|
8 |
|
9 function test() { |
|
10 let { Parser, ParserHelpers, SyntaxTreeVisitor } = |
|
11 Cu.import("resource:///modules/devtools/Parser.jsm", {}); |
|
12 |
|
13 function verify(source, predicate, details) { |
|
14 let { name, chain } = details; |
|
15 let [[sline, scol], [eline, ecol]] = details.loc; |
|
16 let ast = Parser.reflectionAPI.parse(source); |
|
17 let node = SyntaxTreeVisitor.filter(ast, predicate).pop(); |
|
18 let info = ParserHelpers.inferFunctionExpressionInfo(node); |
|
19 |
|
20 is(info.name, name, |
|
21 "The function expression assignment property name is correct."); |
|
22 is(chain ? info.chain.toSource() : info.chain, chain ? chain.toSource() : chain, |
|
23 "The function expression assignment property chain is correct."); |
|
24 is(info.loc.start.toSource(), { line: sline, column: scol }.toSource(), |
|
25 "The start location was correct for the identifier in: '" + source + "'."); |
|
26 is(info.loc.end.toSource(), { line: eline, column: ecol }.toSource(), |
|
27 "The end location was correct for the identifier in: '" + source + "'."); |
|
28 } |
|
29 |
|
30 // VariableDeclarator |
|
31 |
|
32 verify("var foo=()=>{}", e => e.type == "ArrowExpression", { |
|
33 name: "foo", |
|
34 chain: null, |
|
35 loc: [[1, 4], [1, 7]] |
|
36 }); |
|
37 verify("\nvar\nfoo\n=\n(\n)\n=>\n{\n}\n", e => e.type == "ArrowExpression", { |
|
38 name: "foo", |
|
39 chain: null, |
|
40 loc: [[3, 0], [3, 3]] |
|
41 }); |
|
42 |
|
43 // AssignmentExpression |
|
44 |
|
45 verify("foo=()=>{}", e => e.type == "ArrowExpression", |
|
46 { name: "foo", chain: [], loc: [[1, 0], [1, 3]] }); |
|
47 |
|
48 verify("\nfoo\n=\n(\n)\n=>\n{\n}\n", e => e.type == "ArrowExpression", |
|
49 { name: "foo", chain: [], loc: [[2, 0], [2, 3]] }); |
|
50 |
|
51 verify("foo.bar=()=>{}", e => e.type == "ArrowExpression", |
|
52 { name: "bar", chain: ["foo"], loc: [[1, 0], [1, 7]] }); |
|
53 |
|
54 verify("\nfoo.bar\n=\n(\n)\n=>\n{\n}\n", e => e.type == "ArrowExpression", |
|
55 { name: "bar", chain: ["foo"], loc: [[2, 0], [2, 7]] }); |
|
56 |
|
57 verify("this.foo=()=>{}", e => e.type == "ArrowExpression", |
|
58 { name: "foo", chain: ["this"], loc: [[1, 0], [1, 8]] }); |
|
59 |
|
60 verify("\nthis.foo\n=\n(\n)\n=>\n{\n}\n", e => e.type == "ArrowExpression", |
|
61 { name: "foo", chain: ["this"], loc: [[2, 0], [2, 8]] }); |
|
62 |
|
63 verify("this.foo.bar=()=>{}", e => e.type == "ArrowExpression", |
|
64 { name: "bar", chain: ["this", "foo"], loc: [[1, 0], [1, 12]] }); |
|
65 |
|
66 verify("\nthis.foo.bar\n=\n(\n)\n=>\n{\n}\n", e => e.type == "ArrowExpression", |
|
67 { name: "bar", chain: ["this", "foo"], loc: [[2, 0], [2, 12]] }); |
|
68 |
|
69 verify("foo.this.bar=()=>{}", e => e.type == "ArrowExpression", |
|
70 { name: "bar", chain: ["foo", "this"], loc: [[1, 0], [1, 12]] }); |
|
71 |
|
72 verify("\nfoo.this.bar\n=\n(\n)\n=>\n{\n}\n", e => e.type == "ArrowExpression", |
|
73 { name: "bar", chain: ["foo", "this"], loc: [[2, 0], [2, 12]] }); |
|
74 |
|
75 // ObjectExpression |
|
76 |
|
77 verify("({foo:()=>{}})", e => e.type == "ArrowExpression", |
|
78 { name: "foo", chain: [], loc: [[1, 2], [1, 5]] }); |
|
79 |
|
80 verify("(\n{\nfoo\n:\n(\n)\n=>\n{\n}\n}\n)", e => e.type == "ArrowExpression", |
|
81 { name: "foo", chain: [], loc: [[3, 0], [3, 3]] }); |
|
82 |
|
83 verify("({foo:{bar:()=>{}}})", e => e.type == "ArrowExpression", |
|
84 { name: "bar", chain: ["foo"], loc: [[1, 7], [1, 10]] }); |
|
85 |
|
86 verify("(\n{\nfoo\n:\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n}\n)", e => e.type == "ArrowExpression", |
|
87 { name: "bar", chain: ["foo"], loc: [[6, 0], [6, 3]] }); |
|
88 |
|
89 // AssignmentExpression + ObjectExpression |
|
90 |
|
91 verify("foo={bar:()=>{}}", e => e.type == "ArrowExpression", |
|
92 { name: "bar", chain: ["foo"], loc: [[1, 5], [1, 8]] }); |
|
93 |
|
94 verify("\nfoo\n=\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n", e => e.type == "ArrowExpression", |
|
95 { name: "bar", chain: ["foo"], loc: [[5, 0], [5, 3]] }); |
|
96 |
|
97 verify("foo={bar:{baz:()=>{}}}", e => e.type == "ArrowExpression", |
|
98 { name: "baz", chain: ["foo", "bar"], loc: [[1, 10], [1, 13]] }); |
|
99 |
|
100 verify("\nfoo\n=\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n", e => e.type == "ArrowExpression", |
|
101 { name: "baz", chain: ["foo", "bar"], loc: [[8, 0], [8, 3]] }); |
|
102 |
|
103 verify("nested.foo={bar:()=>{}}", e => e.type == "ArrowExpression", |
|
104 { name: "bar", chain: ["nested", "foo"], loc: [[1, 12], [1, 15]] }); |
|
105 |
|
106 verify("\nnested.foo\n=\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n", e => e.type == "ArrowExpression", |
|
107 { name: "bar", chain: ["nested", "foo"], loc: [[5, 0], [5, 3]] }); |
|
108 |
|
109 verify("nested.foo={bar:{baz:()=>{}}}", e => e.type == "ArrowExpression", |
|
110 { name: "baz", chain: ["nested", "foo", "bar"], loc: [[1, 17], [1, 20]] }); |
|
111 |
|
112 verify("\nnested.foo\n=\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n", e => e.type == "ArrowExpression", |
|
113 { name: "baz", chain: ["nested", "foo", "bar"], loc: [[8, 0], [8, 3]] }); |
|
114 |
|
115 verify("this.foo={bar:()=>{}}", e => e.type == "ArrowExpression", |
|
116 { name: "bar", chain: ["this", "foo"], loc: [[1, 10], [1, 13]] }); |
|
117 |
|
118 verify("\nthis.foo\n=\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n", e => e.type == "ArrowExpression", |
|
119 { name: "bar", chain: ["this", "foo"], loc: [[5, 0], [5, 3]] }); |
|
120 |
|
121 verify("this.foo={bar:{baz:()=>{}}}", e => e.type == "ArrowExpression", |
|
122 { name: "baz", chain: ["this", "foo", "bar"], loc: [[1, 15], [1, 18]] }); |
|
123 |
|
124 verify("\nthis.foo\n=\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n", e => e.type == "ArrowExpression", |
|
125 { name: "baz", chain: ["this", "foo", "bar"], loc: [[8, 0], [8, 3]] }); |
|
126 |
|
127 verify("this.nested.foo={bar:()=>{}}", e => e.type == "ArrowExpression", |
|
128 { name: "bar", chain: ["this", "nested", "foo"], loc: [[1, 17], [1, 20]] }); |
|
129 |
|
130 verify("\nthis.nested.foo\n=\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n", e => e.type == "ArrowExpression", |
|
131 { name: "bar", chain: ["this", "nested", "foo"], loc: [[5, 0], [5, 3]] }); |
|
132 |
|
133 verify("this.nested.foo={bar:{baz:()=>{}}}", e => e.type == "ArrowExpression", |
|
134 { name: "baz", chain: ["this", "nested", "foo", "bar"], loc: [[1, 22], [1, 25]] }); |
|
135 |
|
136 verify("\nthis.nested.foo\n=\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n", e => e.type == "ArrowExpression", |
|
137 { name: "baz", chain: ["this", "nested", "foo", "bar"], loc: [[8, 0], [8, 3]] }); |
|
138 |
|
139 verify("nested.this.foo={bar:()=>{}}", e => e.type == "ArrowExpression", |
|
140 { name: "bar", chain: ["nested", "this", "foo"], loc: [[1, 17], [1, 20]] }); |
|
141 |
|
142 verify("\nnested.this.foo\n=\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n", e => e.type == "ArrowExpression", |
|
143 { name: "bar", chain: ["nested", "this", "foo"], loc: [[5, 0], [5, 3]] }); |
|
144 |
|
145 verify("nested.this.foo={bar:{baz:()=>{}}}", e => e.type == "ArrowExpression", |
|
146 { name: "baz", chain: ["nested", "this", "foo", "bar"], loc: [[1, 22], [1, 25]] }); |
|
147 |
|
148 verify("\nnested.this.foo\n=\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n", e => e.type == "ArrowExpression", |
|
149 { name: "baz", chain: ["nested", "this", "foo", "bar"], loc: [[8, 0], [8, 3]] }); |
|
150 |
|
151 // VariableDeclarator + AssignmentExpression + ObjectExpression |
|
152 |
|
153 verify("let foo={bar:()=>{}}", e => e.type == "ArrowExpression", |
|
154 { name: "bar", chain: ["foo"], loc: [[1, 9], [1, 12]] }); |
|
155 |
|
156 verify("\nlet\nfoo\n=\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n", e => e.type == "ArrowExpression", |
|
157 { name: "bar", chain: ["foo"], loc: [[6, 0], [6, 3]] }); |
|
158 |
|
159 verify("let foo={bar:{baz:()=>{}}}", e => e.type == "ArrowExpression", |
|
160 { name: "baz", chain: ["foo", "bar"], loc: [[1, 14], [1, 17]] }); |
|
161 |
|
162 verify("\nlet\nfoo\n=\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n", e => e.type == "ArrowExpression", |
|
163 { name: "baz", chain: ["foo", "bar"], loc: [[9, 0], [9, 3]] }); |
|
164 |
|
165 // New/CallExpression + AssignmentExpression + ObjectExpression |
|
166 |
|
167 verify("foo({bar:()=>{}})", e => e.type == "ArrowExpression", |
|
168 { name: "bar", chain: [], loc: [[1, 5], [1, 8]] }); |
|
169 |
|
170 verify("\nfoo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression", |
|
171 { name: "bar", chain: [], loc: [[5, 0], [5, 3]] }); |
|
172 |
|
173 verify("foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression", |
|
174 { name: "baz", chain: ["bar"], loc: [[1, 10], [1, 13]] }); |
|
175 |
|
176 verify("\nfoo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression", |
|
177 { name: "baz", chain: ["bar"], loc: [[8, 0], [8, 3]] }); |
|
178 |
|
179 verify("nested.foo({bar:()=>{}})", e => e.type == "ArrowExpression", |
|
180 { name: "bar", chain: [], loc: [[1, 12], [1, 15]] }); |
|
181 |
|
182 verify("\nnested.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression", |
|
183 { name: "bar", chain: [], loc: [[5, 0], [5, 3]] }); |
|
184 |
|
185 verify("nested.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression", |
|
186 { name: "baz", chain: ["bar"], loc: [[1, 17], [1, 20]] }); |
|
187 |
|
188 verify("\nnested.foo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression", |
|
189 { name: "baz", chain: ["bar"], loc: [[8, 0], [8, 3]] }); |
|
190 |
|
191 verify("this.foo({bar:()=>{}})", e => e.type == "ArrowExpression", |
|
192 { name: "bar", chain: [], loc: [[1, 10], [1, 13]] }); |
|
193 |
|
194 verify("\nthis.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression", |
|
195 { name: "bar", chain: [], loc: [[5, 0], [5, 3]] }); |
|
196 |
|
197 verify("this.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression", |
|
198 { name: "baz", chain: ["bar"], loc: [[1, 15], [1, 18]] }); |
|
199 |
|
200 verify("\nthis.foo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression", |
|
201 { name: "baz", chain: ["bar"], loc: [[8, 0], [8, 3]] }); |
|
202 |
|
203 verify("this.nested.foo({bar:()=>{}})", e => e.type == "ArrowExpression", |
|
204 { name: "bar", chain: [], loc: [[1, 17], [1, 20]] }); |
|
205 |
|
206 verify("\nthis.nested.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression", |
|
207 { name: "bar", chain: [], loc: [[5, 0], [5, 3]] }); |
|
208 |
|
209 verify("this.nested.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression", |
|
210 { name: "baz", chain: ["bar"], loc: [[1, 22], [1, 25]] }); |
|
211 |
|
212 verify("\nthis.nested.foo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression", |
|
213 { name: "baz", chain: ["bar"], loc: [[8, 0], [8, 3]] }); |
|
214 |
|
215 verify("nested.this.foo({bar:()=>{}})", e => e.type == "ArrowExpression", |
|
216 { name: "bar", chain: [], loc: [[1, 17], [1, 20]] }); |
|
217 |
|
218 verify("\nnested.this.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression", |
|
219 { name: "bar", chain: [], loc: [[5, 0], [5, 3]] }); |
|
220 |
|
221 verify("nested.this.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression", |
|
222 { name: "baz", chain: ["bar"], loc: [[1, 22], [1, 25]] }); |
|
223 |
|
224 verify("\nnested.this.foo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression", |
|
225 { name: "baz", chain: ["bar"], loc: [[8, 0], [8, 3]] }); |
|
226 |
|
227 // New/CallExpression + VariableDeclarator + AssignmentExpression + ObjectExpression |
|
228 |
|
229 verify("let target=foo({bar:()=>{}})", e => e.type == "ArrowExpression", |
|
230 { name: "bar", chain: ["target"], loc: [[1, 16], [1, 19]] }); |
|
231 |
|
232 verify("\nlet\ntarget=\nfoo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression", |
|
233 { name: "bar", chain: ["target"], loc: [[7, 0], [7, 3]] }); |
|
234 |
|
235 verify("let target=foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression", |
|
236 { name: "baz", chain: ["target", "bar"], loc: [[1, 21], [1, 24]] }); |
|
237 |
|
238 verify("\nlet\ntarget=\nfoo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression", |
|
239 { name: "baz", chain: ["target", "bar"], loc: [[10, 0], [10, 3]] }); |
|
240 |
|
241 verify("let target=nested.foo({bar:()=>{}})", e => e.type == "ArrowExpression", |
|
242 { name: "bar", chain: ["target"], loc: [[1, 23], [1, 26]] }); |
|
243 |
|
244 verify("\nlet\ntarget=\nnested.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression", |
|
245 { name: "bar", chain: ["target"], loc: [[7, 0], [7, 3]] }); |
|
246 |
|
247 verify("let target=nested.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression", |
|
248 { name: "baz", chain: ["target", "bar"], loc: [[1, 28], [1, 31]] }); |
|
249 |
|
250 verify("\nlet\ntarget=\nnested.foo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression", |
|
251 { name: "baz", chain: ["target", "bar"], loc: [[10, 0], [10, 3]] }); |
|
252 |
|
253 verify("let target=this.foo({bar:()=>{}})", e => e.type == "ArrowExpression", |
|
254 { name: "bar", chain: ["target"], loc: [[1, 21], [1, 24]] }); |
|
255 |
|
256 verify("\nlet\ntarget=\nthis.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression", |
|
257 { name: "bar", chain: ["target"], loc: [[7, 0], [7, 3]] }); |
|
258 |
|
259 verify("let target=this.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression", |
|
260 { name: "baz", chain: ["target", "bar"], loc: [[1, 26], [1, 29]] }); |
|
261 |
|
262 verify("\nlet\ntarget=\nthis.foo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression", |
|
263 { name: "baz", chain: ["target", "bar"], loc: [[10, 0], [10, 3]] }); |
|
264 |
|
265 verify("let target=this.nested.foo({bar:()=>{}})", e => e.type == "ArrowExpression", |
|
266 { name: "bar", chain: ["target"], loc: [[1, 28], [1, 31]] }); |
|
267 |
|
268 verify("\nlet\ntarget=\nthis.nested.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression", |
|
269 { name: "bar", chain: ["target"], loc: [[7, 0], [7, 3]] }); |
|
270 |
|
271 verify("let target=this.nested.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression", |
|
272 { name: "baz", chain: ["target", "bar"], loc: [[1, 33], [1, 36]] }); |
|
273 |
|
274 verify("\nlet\ntarget=\nthis.nested.foo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression", |
|
275 { name: "baz", chain: ["target", "bar"], loc: [[10, 0], [10, 3]] }); |
|
276 |
|
277 verify("let target=nested.this.foo({bar:()=>{}})", e => e.type == "ArrowExpression", |
|
278 { name: "bar", chain: ["target"], loc: [[1, 28], [1, 31]] }); |
|
279 |
|
280 verify("\nlet\ntarget=\nnested.this.foo\n(\n{\nbar\n:\n(\n)\n=>\n{\n}\n}\n)\n", e => e.type == "ArrowExpression", |
|
281 { name: "bar", chain: ["target"], loc: [[7, 0], [7, 3]] }); |
|
282 |
|
283 verify("let target=nested.this.foo({bar:{baz:()=>{}}})", e => e.type == "ArrowExpression", |
|
284 { name: "baz", chain: ["target", "bar"], loc: [[1, 33], [1, 36]] }); |
|
285 |
|
286 verify("\nlet\ntarget=\nnested.this.foo\n(\n{\nbar\n:\n{\nbaz\n:\n(\n)\n=>\n{\n}\n}\n}\n)\n", e => e.type == "ArrowExpression", |
|
287 { name: "baz", chain: ["target", "bar"], loc: [[10, 0], [10, 3]] }); |
|
288 |
|
289 finish(); |
|
290 } |