Debian Codegraph @

parser/comments_test.go

raw symbols SCIP
1 package parser
2
3 import (
4 "fmt"
5 "reflect"
6 "testing"
7
8 "github.com/robertkrimen/otto/ast"
9 )
10
11 func checkComments(actual []*ast.Comment, expected []string, position ast.CommentPosition) error {
12 var comments []*ast.Comment
13 for _, c := range actual {
14 if c.Position == position {
15 comments = append(comments, c)
16 }
17 }
18
19 if len(comments) != len(expected) {
20 return fmt.Errorf("the number of comments is not correct. %v != %v", len(comments), len(expected))
21 }
22
23 for i, v := range comments {
24 if v.Text != expected[i] {
25 return fmt.Errorf("comments do not match: %q != %q", v.Text, expected[i])
26 }
27 if v.Position != position {
28 return fmt.Errorf("comment positions do not match: %d != %d", position, v.Position)
29 }
30 }
31
32 return nil
33 }
34
35 func displayComments(m ast.CommentMap) {
36 fmt.Printf("Displaying comments:\n") //nolint:forbidigo
37 for n, comments := range m {
38 fmt.Printf("%v %v:\n", reflect.TypeOf(n), n) //nolint:forbidigo
39 for i, comment := range comments {
40 fmt.Printf(" [%v] %v @ %v\n", i, comment.Text, comment.Position) //nolint:forbidigo
41 }
42 }
43 }
44
45 func TestParser_comments(t *testing.T) {
46 tt(t, func() {
47 test := func(source string, chk interface{}) (*parser, *ast.Program) {
48 parser, program, err := testParseWithMode(source, StoreComments)
49 is(firstErr(err), chk)
50
51 // Check unresolved comments
52
53 return parser, program
54 }
55
56 var err error
57 var p *parser
58 var program *ast.Program
59
60 p, program = test("q=2;// Hej\nv = 0", nil)
61 is(len(program.Body), 2)
62 err = checkComments((p.comments.CommentMap)[program.Body[1]], []string{" Hej"}, ast.LEADING)
63 is(err, nil)
64
65 // Assignment
66 p, program = test("i = /*test=*/ 2", nil)
67 is(len(program.Body), 1)
68 is(checkComments((p.comments.CommentMap)[program.Body[0]], []string{}, ast.LEADING), nil)
69 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.AssignExpression).Right], []string{"test="}, ast.LEADING), nil)
70 is(p.comments.CommentMap.Size(), 1)
71
72 // Conditional, before consequent
73 p, program = test("i ? /*test?*/ 2 : 3", nil)
74 is(len(program.Body), 1)
75 is(checkComments((p.comments.CommentMap)[program.Body[0]], []string{}, ast.LEADING), nil)
76 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.ConditionalExpression).Consequent], []string{"test?"}, ast.LEADING), nil)
77 is(p.comments.CommentMap.Size(), 1)
78
79 // Conditional, after consequent
80 p, program = test("i ? 2 /*test?*/ : 3", nil)
81 is(len(program.Body), 1)
82 is(checkComments((p.comments.CommentMap)[program.Body[0]], []string{}, ast.LEADING), nil)
83 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.ConditionalExpression).Consequent], []string{"test?"}, ast.TRAILING), nil)
84 is(p.comments.CommentMap.Size(), 1)
85
86 // Conditional, before alternate
87 p, program = test("i ? 2 : /*test:*/ 3", nil)
88 is(len(program.Body), 1)
89 is(checkComments((p.comments.CommentMap)[program.Body[0]], []string{}, ast.LEADING), nil)
90 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.ConditionalExpression).Alternate], []string{"test:"}, ast.LEADING), nil)
91 is(p.comments.CommentMap.Size(), 1)
92
93 // Logical OR
94 p, program = test("i || /*test||*/ 2", nil)
95 is(len(program.Body), 1)
96 is(checkComments((p.comments.CommentMap)[program.Body[0]], []string{}, ast.LEADING), nil)
97 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.BinaryExpression).Right], []string{"test||"}, ast.LEADING), nil)
98 is(p.comments.CommentMap.Size(), 1)
99
100 // Logical AND
101 p, program = test("i && /*test&&*/ 2", nil)
102 is(len(program.Body), 1)
103 is(checkComments((p.comments.CommentMap)[program.Body[0]], []string{}, ast.LEADING), nil)
104 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.BinaryExpression).Right], []string{"test&&"}, ast.LEADING), nil)
105 is(p.comments.CommentMap.Size(), 1)
106
107 // Bitwise OR
108 p, program = test("i | /*test|*/ 2", nil)
109 is(len(program.Body), 1)
110 is(checkComments((p.comments.CommentMap)[program.Body[0]], []string{}, ast.LEADING), nil)
111 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.BinaryExpression).Right], []string{"test|"}, ast.LEADING), nil)
112 is(p.comments.CommentMap.Size(), 1)
113
114 // Exclusive OR
115 p, program = test("i ^ /*test^*/ 2", nil)
116 is(len(program.Body), 1)
117 is(checkComments((p.comments.CommentMap)[program.Body[0]], []string{}, ast.LEADING), nil)
118 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.BinaryExpression).Right], []string{"test^"}, ast.LEADING), nil)
119 is(p.comments.CommentMap.Size(), 1)
120
121 // Bitwise AND
122 p, program = test("i & /*test&*/ 2", nil)
123 is(len(program.Body), 1)
124 is(checkComments((p.comments.CommentMap)[program.Body[0]], []string{}, ast.LEADING), nil)
125 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.BinaryExpression).Right], []string{"test&"}, ast.LEADING), nil)
126 is(p.comments.CommentMap.Size(), 1)
127
128 // Equality
129 p, program = test("i == /*test==*/ 2", nil)
130 is(len(program.Body), 1)
131 is(checkComments((p.comments.CommentMap)[program.Body[0]], []string{}, ast.LEADING), nil)
132 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.BinaryExpression).Right], []string{"test=="}, ast.LEADING), nil)
133 is(p.comments.CommentMap.Size(), 1)
134
135 // Relational, <
136 p, program = test("i < /*test<*/ 2", nil)
137 is(len(program.Body), 1)
138 is(checkComments((p.comments.CommentMap)[program.Body[0]], []string{}, ast.LEADING), nil)
139 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.BinaryExpression).Right], []string{"test<"}, ast.LEADING), nil)
140 is(p.comments.CommentMap.Size(), 1)
141
142 // Relational, instanceof
143 p, program = test("i instanceof /*testinstanceof*/ thing", nil)
144 is(len(program.Body), 1)
145 is(checkComments((p.comments.CommentMap)[program.Body[0]], []string{}, ast.LEADING), nil)
146 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.BinaryExpression).Right], []string{"testinstanceof"}, ast.LEADING), nil)
147 is(p.comments.CommentMap.Size(), 1)
148
149 // Shift left
150 p, program = test("i << /*test<<*/ 2", nil)
151 is(len(program.Body), 1)
152 is(checkComments((p.comments.CommentMap)[program.Body[0]], []string{}, ast.LEADING), nil)
153 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.BinaryExpression).Right], []string{"test<<"}, ast.LEADING), nil)
154 is(p.comments.CommentMap.Size(), 1)
155
156 // +
157 p, program = test("i + /*test+*/ 2", nil)
158 is(len(program.Body), 1)
159 is(checkComments((p.comments.CommentMap)[program.Body[0]], []string{}, ast.LEADING), nil)
160 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.BinaryExpression).Right], []string{"test+"}, ast.LEADING), nil)
161 is(p.comments.CommentMap.Size(), 1)
162
163 // *
164 p, program = test("i * /*test**/ 2", nil)
165 is(len(program.Body), 1)
166 is(checkComments((p.comments.CommentMap)[program.Body[0]], []string{}, ast.LEADING), nil)
167 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.BinaryExpression).Right], []string{"test*"}, ast.LEADING), nil)
168 is(p.comments.CommentMap.Size(), 1)
169
170 // Unary prefix, ++
171 p, program = test("++/*test++*/i", nil)
172 is(len(program.Body), 1)
173 is(checkComments((p.comments.CommentMap)[program.Body[0]], []string{}, ast.LEADING), nil)
174 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.UnaryExpression).Operand], []string{"test++"}, ast.LEADING), nil)
175 is(p.comments.CommentMap.Size(), 1)
176
177 // Unary prefix, delete
178 p, program = test("delete /*testdelete*/ i", nil)
179 is(len(program.Body), 1)
180 is(checkComments((p.comments.CommentMap)[program.Body[0]], []string{}, ast.LEADING), nil)
181 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.UnaryExpression).Operand], []string{"testdelete"}, ast.LEADING), nil)
182 is(p.comments.CommentMap.Size(), 1)
183
184 // Unary postfix, ++
185 p, program = test("i/*test++*/++", nil)
186 is(len(program.Body), 1)
187 is(checkComments((p.comments.CommentMap)[program.Body[0]], []string{}, ast.LEADING), nil)
188 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.UnaryExpression).Operand], []string{"test++"}, ast.TRAILING), nil)
189 is(p.comments.CommentMap.Size(), 1)
190
191 // + pt 2
192 p, program = test("i /*test+*/ + 2", nil)
193 is(len(program.Body), 1)
194 is(checkComments((p.comments.CommentMap)[program.Body[0]], []string{}, ast.LEADING), nil)
195 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.BinaryExpression).Left], []string{"test+"}, ast.TRAILING), nil)
196 is(p.comments.CommentMap.Size(), 1)
197
198 // Multiple comments for a single node
199 p, program = test("i /*test+*/ /*test+2*/ + 2", nil)
200 is(len(program.Body), 1)
201 is(checkComments((p.comments.CommentMap)[program.Body[0]], []string{}, ast.LEADING), nil)
202 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.BinaryExpression).Left], []string{"test+", "test+2"}, ast.TRAILING), nil)
203 is(p.comments.CommentMap.Size(), 2)
204
205 // Multiple comments for multiple nodes
206 p, program = test("i /*test1*/ + 2 /*test2*/ + a /*test3*/ * x /*test4*/", nil)
207 is(len(program.Body), 1)
208 is(p.comments.CommentMap.Size(), 4)
209
210 // Leading comment
211 p, program = test("/*leadingtest*/i + 2", nil)
212 is(len(program.Body), 1)
213
214 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement)], []string{"leadingtest"}, ast.LEADING), nil)
215 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.BinaryExpression).Left], []string{}, ast.TRAILING), nil)
216 is(p.comments.CommentMap.Size(), 1)
217
218 // Leading comment, with semicolon
219 p, program = test("/*leadingtest;*/;i + 2", nil)
220 is(len(program.Body), 2)
221 is(checkComments((p.comments.CommentMap)[program.Body[1]], []string{"leadingtest;"}, ast.LEADING), nil)
222 is(p.comments.CommentMap.Size(), 1)
223
224 // Arrays
225 p, program = test("[1, 2 /*test2*/, 3]", nil)
226 is(len(program.Body), 1)
227 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.ArrayLiteral).Value[1]], []string{"test2"}, ast.TRAILING), nil)
228 is(p.comments.CommentMap.Size(), 1)
229
230 // Function calls
231 p, program = test("fun(a,b) //test", nil)
232 is(len(program.Body), 1)
233 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.CallExpression)], []string{"test"}, ast.TRAILING), nil)
234 is(p.comments.CommentMap.Size(), 1)
235
236 // Function calls, pt 2
237 p, program = test("fun(a/*test1*/,b)", nil)
238 is(len(program.Body), 1)
239 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.CallExpression).ArgumentList[0]], []string{"test1"}, ast.TRAILING), nil)
240 is(p.comments.CommentMap.Size(), 1)
241
242 // Function calls, pt 3
243 p, program = test("fun(/*test1*/a,b)", nil)
244 is(len(program.Body), 1)
245 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.CallExpression).ArgumentList[0]], []string{"test1"}, ast.LEADING), nil)
246 is(p.comments.CommentMap.Size(), 1)
247
248 // Arrays pt 2
249 p, program = test(`["abc".substr(0,1)/*testa*/,
250 "abc.substr(0,2)"];`, nil)
251 is(len(program.Body), 1)
252 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.ArrayLiteral).Value[0]], []string{"testa"}, ast.TRAILING), nil)
253 is(p.comments.CommentMap.Size(), 1)
254
255 // Arrays pt 3
256 p, program = test(`[a, //test
257 b];`, nil)
258 is(len(program.Body), 1)
259 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.ArrayLiteral).Value[1]], []string{"test"}, ast.LEADING), nil)
260 is(p.comments.CommentMap.Size(), 1)
261
262 // Arrays pt 4
263 p, program = test(`[a, //test
264 b, c];`, nil)
265 is(len(program.Body), 1)
266 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.ArrayLiteral).Value[1]], []string{"test"}, ast.LEADING), nil)
267 is(p.comments.CommentMap.Size(), 1)
268
269 // Arrays pt 5
270 p, program = test(`
271 [
272 "a1", // "a"
273 "a2", // "ab"
274 ];
275 `, nil)
276 is(p.comments.CommentMap.Size(), 2)
277 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.ArrayLiteral).Value[1]], []string{" \"a\""}, ast.LEADING), nil)
278 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.ArrayLiteral)], []string{" \"ab\""}, ast.FINAL), nil)
279
280 // Arrays pt 6
281 p, program = test(`[a, /*test*/ b, c];`, nil)
282 is(len(program.Body), 1)
283 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.ArrayLiteral).Value[1]], []string{"test"}, ast.LEADING), nil)
284 is(p.comments.CommentMap.Size(), 1)
285
286 // Arrays pt 7 - Empty node
287 p, program = test(`[a,,/*test2*/,];`, nil)
288 is(len(program.Body), 1)
289 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.ArrayLiteral).Value[2]], []string{"test2"}, ast.TRAILING), nil)
290 is(p.comments.CommentMap.Size(), 1)
291
292 // Arrays pt 8 - Trailing node
293 p, program = test(`[a,,,/*test2*/];`, nil)
294 is(len(program.Body), 1)
295 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.ArrayLiteral)], []string{"test2"}, ast.FINAL), nil)
296 is(p.comments.CommentMap.Size(), 1)
297
298 // Arrays pt 9 - Leading node
299 p, program = test(`[/*test2*/a,,,];`, nil)
300 is(len(program.Body), 1)
301 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.ArrayLiteral).Value[0]], []string{"test2"}, ast.LEADING), nil)
302 is(p.comments.CommentMap.Size(), 1)
303
304 // Object literal
305 p, program = test("obj = {a: 1, b: 2 /*test2*/, c: 3}", nil)
306 is(len(program.Body), 1)
307 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.AssignExpression).Right.(*ast.ObjectLiteral).Value[1].Value], []string{"test2"}, ast.TRAILING), nil)
308 is(p.comments.CommentMap.Size(), 1)
309
310 // Object literal, pt 2
311 p, program = test("obj = {/*test2*/a: 1, b: 2, c: 3}", nil)
312 is(len(program.Body), 1)
313 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.AssignExpression).Right.(*ast.ObjectLiteral).Value[0].Value], []string{"test2"}, ast.KEY), nil)
314 is(p.comments.CommentMap.Size(), 1)
315
316 // Object literal, pt 3
317 p, program = test("obj = {x/*test2*/: 1, y: 2, z: 3}", nil)
318 is(len(program.Body), 1)
319 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.AssignExpression).Right.(*ast.ObjectLiteral).Value[0].Value], []string{"test2"}, ast.COLON), nil)
320 is(p.comments.CommentMap.Size(), 1)
321
322 // Object literal, pt 4
323 p, program = test("obj = {x: /*test2*/1, y: 2, z: 3}", nil)
324 is(len(program.Body), 1)
325 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.AssignExpression).Right.(*ast.ObjectLiteral).Value[0].Value], []string{"test2"}, ast.LEADING), nil)
326 is(p.comments.CommentMap.Size(), 1)
327
328 // Object literal, pt 5
329 p, program = test("obj = {x: 1/*test2*/, y: 2, z: 3}", nil)
330 is(len(program.Body), 1)
331 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.AssignExpression).Right.(*ast.ObjectLiteral).Value[0].Value], []string{"test2"}, ast.TRAILING), nil)
332 is(p.comments.CommentMap.Size(), 1)
333
334 // Object literal, pt 6
335 p, program = test("obj = {x: 1, y: 2, z: 3/*test2*/}", nil)
336 is(len(program.Body), 1)
337 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.AssignExpression).Right.(*ast.ObjectLiteral).Value[2].Value], []string{"test2"}, ast.TRAILING), nil)
338 is(p.comments.CommentMap.Size(), 1)
339
340 // Object literal, pt 7 - trailing comment
341 p, program = test("obj = {x: 1, y: 2, z: 3,/*test2*/}", nil)
342 is(p.comments.CommentMap.Size(), 1)
343 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.AssignExpression).Right.(*ast.ObjectLiteral)], []string{"test2"}, ast.FINAL), nil)
344
345 // Line breaks
346 p, program = test(`
347 t1 = "BLA DE VLA"
348 /*Test*/
349 t2 = "Nothing happens."
350 `, nil)
351 is(p.comments.CommentMap.Size(), 1)
352 is(checkComments((p.comments.CommentMap)[program.Body[1]], []string{"Test"}, ast.LEADING), nil)
353
354 // Line breaks pt 2
355 p, program = test(`
356 t1 = "BLA DE VLA" /*Test*/
357 t2 = "Nothing happens."
358 `, nil)
359 is(p.comments.CommentMap.Size(), 1)
360 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.AssignExpression).Right.(*ast.StringLiteral)], []string{"Test"}, ast.TRAILING), nil)
361 is(checkComments((p.comments.CommentMap)[program.Body[1].(*ast.ExpressionStatement)], []string{}, ast.LEADING), nil)
362
363 // Line breaks pt 3
364 p, program = test(`
365 t1 = "BLA DE VLA" /*Test*/ /*Test2*/
366 t2 = "Nothing happens."
367 `, nil)
368 is(p.comments.CommentMap.Size(), 2)
369 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.AssignExpression).Right.(*ast.StringLiteral)], []string{"Test", "Test2"}, ast.TRAILING), nil)
370 is(checkComments((p.comments.CommentMap)[program.Body[1].(*ast.ExpressionStatement)], []string{}, ast.LEADING), nil)
371
372 // Line breaks pt 4
373 p, program = test(`
374 t1 = "BLA DE VLA" /*Test*/
375 /*Test2*/
376 t2 = "Nothing happens."
377 `, nil)
378 is(p.comments.CommentMap.Size(), 2)
379 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.AssignExpression).Right.(*ast.StringLiteral)], []string{"Test"}, ast.TRAILING), nil)
380 is(checkComments((p.comments.CommentMap)[program.Body[1]], []string{"Test2"}, ast.LEADING), nil)
381
382 // Line breaks pt 5
383 p, program = test(`
384 t1 = "BLA DE VLA";
385 /*Test*/
386 t2 = "Nothing happens."
387 `, nil)
388 is(p.comments.CommentMap.Size(), 1)
389 is(checkComments((p.comments.CommentMap)[program.Body[1]], []string{"Test"}, ast.LEADING), nil)
390
391 // Line breaks pt 6
392 p, program = test(`
393 t1 = "BLA DE VLA"; /*Test*/
394 /*Test2*/
395 t2 = "Nothing happens."
396 `, nil)
397 is(p.comments.CommentMap.Size(), 2)
398 is(checkComments((p.comments.CommentMap)[program.Body[1]], []string{"Test", "Test2"}, ast.LEADING), nil)
399
400 // Misc
401 p, _ = test(`
402 var x = Object.create({y: {
403 },
404 // a
405 });
406 `, nil)
407 is(p.comments.CommentMap.Size(), 1)
408
409 // Misc 2
410 p, _ = test(`
411 var x = Object.create({y: {
412 },
413 // a
414 // b
415 a: 2});
416 `, nil)
417 is(p.comments.CommentMap.Size(), 2)
418
419 // Statement blocks
420 p, program = test(`
421 (function() {
422 // Baseline setup
423 })
424 `, nil)
425 is(p.comments.CommentMap.Size(), 1)
426 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body], []string{" Baseline setup"}, ast.FINAL), nil)
427
428 // Switches
429 p, program = test(`
430 switch (switcha) {
431 // switch comment
432 case "switchb":
433 a
434 }
435 `, nil)
436 is(p.comments.CommentMap.Size(), 1)
437 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.SwitchStatement).Body[0]], []string{" switch comment"}, ast.LEADING), nil)
438
439 // Switches pt 2
440 p, program = test(`
441 switch (switcha) {
442 case /*switch comment*/ "switchb":
443 a
444 }
445 `, nil)
446 is(p.comments.CommentMap.Size(), 1)
447 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.SwitchStatement).Body[0].Test], []string{"switch comment"}, ast.LEADING), nil)
448
449 // Switches pt 3
450 p, program = test(`
451 switch (switcha) {
452 case "switchb" /*switch comment*/:
453 a
454 }
455 `, nil)
456 is(p.comments.CommentMap.Size(), 1)
457 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.SwitchStatement).Body[0].Test], []string{"switch comment"}, ast.TRAILING), nil)
458
459 // Switches pt 4
460 p, program = test(`
461 switch (switcha) {
462 case "switchb": /*switch comment*/
463 a
464 }
465 `, nil)
466 is(p.comments.CommentMap.Size(), 1)
467 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.SwitchStatement).Body[0].Consequent[0]], []string{"switch comment"}, ast.LEADING), nil)
468
469 // Switches pt 5 - default
470 p, program = test(`
471 switch (switcha) {
472 default: /*switch comment*/
473 a
474 }
475 `, nil)
476 is(p.comments.CommentMap.Size(), 1)
477 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.SwitchStatement).Body[0].Consequent[0]], []string{"switch comment"}, ast.LEADING), nil)
478
479 // Switches pt 6
480 p, program = test(`
481 switch (switcha) {
482 case "switchb":
483 /*switch comment*/a
484 }
485 `, nil)
486 is(p.comments.CommentMap.Size(), 1)
487 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.SwitchStatement).Body[0].Consequent[0]], []string{"switch comment"}, ast.LEADING), nil)
488
489 // Switches pt 7
490 p, program = test(`
491 switch (switcha) {
492 case "switchb": /*switch comment*/ {
493 a
494 }
495 }
496 `, nil)
497 is(p.comments.CommentMap.Size(), 1)
498 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.SwitchStatement).Body[0].Consequent[0]], []string{"switch comment"}, ast.LEADING), nil)
499
500 // Switches pt 8
501 p, program = test(`
502 switch (switcha) {
503 case "switchb": {
504 a
505 }/*switch comment*/
506 }
507 `, nil)
508 is(p.comments.CommentMap.Size(), 1)
509 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.SwitchStatement).Body[0].Consequent[0]], []string{"switch comment"}, ast.TRAILING), nil)
510
511 // Switches pt 9
512 p, program = test(`
513 switch (switcha) {
514 case "switchb": /*switch comment*/ {
515 a
516 }
517 }
518 `, nil)
519 is(p.comments.CommentMap.Size(), 1)
520 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.SwitchStatement).Body[0].Consequent[0]], []string{"switch comment"}, ast.LEADING), nil)
521
522 // Switches pt 10
523 p, program = test(`
524 switch (switcha) {
525 case "switchb": {
526 /*switch comment*/a
527 }
528 }
529 `, nil)
530 is(p.comments.CommentMap.Size(), 1)
531 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.SwitchStatement).Body[0].Consequent[0].(*ast.BlockStatement).List[0]], []string{"switch comment"}, ast.LEADING), nil)
532
533 // For loops
534 p, program = test(`
535 for(/*comment*/i = 0 ; i < 1 ; i++) {
536 a
537 }
538 `, nil)
539 is(p.comments.CommentMap.Size(), 1)
540 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ForStatement).Initializer.(*ast.SequenceExpression).Sequence[0].(*ast.AssignExpression).Left], []string{"comment"}, ast.LEADING), nil)
541
542 // For loops pt 2
543 p, program = test(`
544 for(i/*comment*/ = 0 ; i < 1 ; i++) {
545 a
546 }
547 `, nil)
548 is(p.comments.CommentMap.Size(), 1)
549 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ForStatement).Initializer.(*ast.SequenceExpression).Sequence[0].(*ast.AssignExpression).Left], []string{"comment"}, ast.TRAILING), nil)
550
551 // For loops pt 3
552 p, program = test(`
553 for(i = 0 ; /*comment*/i < 1 ; i++) {
554 a
555 }
556 `, nil)
557 is(p.comments.CommentMap.Size(), 1)
558 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ForStatement).Test.(*ast.BinaryExpression).Left], []string{"comment"}, ast.LEADING), nil)
559
560 // For loops pt 4
561 p, program = test(`
562 for(i = 0 ;i /*comment*/ < 1 ; i++) {
563 a
564 }
565 `, nil)
566 is(p.comments.CommentMap.Size(), 1)
567 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ForStatement).Test.(*ast.BinaryExpression).Left], []string{"comment"}, ast.TRAILING), nil)
568
569 // For loops pt 5
570 p, program = test(`
571 for(i = 0 ;i < 1 /*comment*/ ; i++) {
572 a
573 }
574 `, nil)
575 is(p.comments.CommentMap.Size(), 1)
576 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ForStatement).Test.(*ast.BinaryExpression).Right], []string{"comment"}, ast.TRAILING), nil)
577
578 // For loops pt 6
579 p, program = test(`
580 for(i = 0 ;i < 1 ; /*comment*/ i++) {
581 a
582 }
583 `, nil)
584 is(p.comments.CommentMap.Size(), 1)
585 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ForStatement).Update.(*ast.UnaryExpression).Operand], []string{"comment"}, ast.LEADING), nil)
586
587 // For loops pt 7
588 p, program = test(`
589 for(i = 0 ;i < 1 ; i++) /*comment*/ {
590 a
591 }
592 `, nil)
593 is(p.comments.CommentMap.Size(), 1)
594 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ForStatement).Body], []string{"comment"}, ast.LEADING), nil)
595
596 // For loops pt 8
597 p, program = test(`
598 for(i = 0 ;i < 1 ; i++) {
599 a
600 }/*comment*/
601 `, nil)
602 is(p.comments.CommentMap.Size(), 1)
603 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ForStatement).Body], []string{"comment"}, ast.TRAILING), nil)
604
605 // For loops pt 9
606 p, program = test(`
607 for(i = 0 ;i < 1 ; /*comment*/i++) {
608 a
609 }
610 `, nil)
611 is(p.comments.CommentMap.Size(), 1)
612 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ForStatement).Update.(*ast.UnaryExpression).Operand], []string{"comment"}, ast.LEADING), nil)
613
614 // For loops pt 10
615 p, program = test(`
616 for(i = 0 ;i < 1 ; i/*comment*/++) {
617 a
618 }
619 `, nil)
620 is(p.comments.CommentMap.Size(), 1)
621 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ForStatement).Update.(*ast.UnaryExpression).Operand.(*ast.Identifier)], []string{"comment"}, ast.TRAILING), nil)
622
623 // For loops pt 11
624 p, program = test(`
625 for(i = 0 ;i < 1 ; i++/*comment*/) {
626 a
627 }
628 `, nil)
629 is(p.comments.CommentMap.Size(), 1)
630 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ForStatement).Update.(*ast.UnaryExpression)], []string{"comment"}, ast.TRAILING), nil)
631
632 // ForIn
633 p, program = test(`
634 for(/*comment*/var i = 0 in obj) {
635 a
636 }
637 `, nil)
638 is(p.comments.CommentMap.Size(), 1)
639 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ForInStatement).Into], []string{"comment"}, ast.LEADING), nil)
640
641 // ForIn pt 2
642 p, program = test(`
643 for(var i = 0 /*comment*/in obj) {
644 a
645 }
646 `, nil)
647 is(p.comments.CommentMap.Size(), 1)
648 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ForInStatement).Into.(*ast.VariableExpression).Initializer], []string{"comment"}, ast.TRAILING), nil)
649
650 // ForIn pt 3
651 p, program = test(`
652 for(var i = 0 in /*comment*/ obj) {
653 a
654 }
655 `, nil)
656 is(p.comments.CommentMap.Size(), 1)
657 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ForInStatement).Source], []string{"comment"}, ast.LEADING), nil)
658
659 // ForIn pt 4
660 p, program = test(`
661 for(var i = 0 in obj/*comment*/) {
662 a
663 }
664 `, nil)
665 is(p.comments.CommentMap.Size(), 1)
666 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ForInStatement).Source], []string{"comment"}, ast.TRAILING), nil)
667
668 // ForIn pt 5
669 p, program = test(`
670 for(var i = 0 in obj) /*comment*/ {
671 a
672 }
673 `, nil)
674 is(p.comments.CommentMap.Size(), 1)
675 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ForInStatement).Body], []string{"comment"}, ast.LEADING), nil)
676
677 // ForIn pt 6
678 p, program = test(`
679 for(var i = 0 in obj) {
680 a
681 }/*comment*/
682 `, nil)
683 is(p.comments.CommentMap.Size(), 1)
684 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ForInStatement).Body], []string{"comment"}, ast.TRAILING), nil)
685
686 // ForIn pt 7
687 p, program = test(`
688 for(var i = 0 in obj) {
689 a
690 }
691 // comment
692 `, nil)
693 is(p.comments.CommentMap.Size(), 1)
694 is(checkComments((p.comments.CommentMap)[program], []string{" comment"}, ast.TRAILING), nil)
695
696 // ForIn pt 8
697 p, program = test(`
698 for(var i = 0 in obj) {
699 a
700 }
701 // comment
702 c
703 `, nil)
704 is(p.comments.CommentMap.Size(), 1)
705 is(checkComments((p.comments.CommentMap)[program.Body[1]], []string{" comment"}, ast.LEADING), nil)
706
707 // Block
708 p, program = test(`
709 /*comment*/{
710 a
711 }
712 `, nil)
713 is(p.comments.CommentMap.Size(), 1)
714 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.BlockStatement)], []string{"comment"}, ast.LEADING), nil)
715
716 // Block pt 2
717 p, program = test(`
718 {
719 a
720 }/*comment*/
721 `, nil)
722 is(p.comments.CommentMap.Size(), 1)
723 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.BlockStatement)], []string{"comment"}, ast.TRAILING), nil)
724
725 // If then else
726 p, program = test(`
727 /*comment*/
728 if(a) {
729 b
730 } else {
731 c
732 }
733 `, nil)
734 is(p.comments.CommentMap.Size(), 1)
735 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.IfStatement)], []string{"comment"}, ast.LEADING), nil)
736
737 // If then else pt 2
738 p, program = test(`
739 if/*comment*/(a) {
740 b
741 } else {
742 c
743 }
744 `, nil)
745 is(p.comments.CommentMap.Size(), 1)
746 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.IfStatement)], []string{"comment"}, ast.IF), nil)
747
748 // If then else pt 3
749 p, program = test(`
750 if(/*comment*/a) {
751 b
752 } else {
753 c
754 }
755 `, nil)
756 is(p.comments.CommentMap.Size(), 1)
757 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.IfStatement).Test], []string{"comment"}, ast.LEADING), nil)
758
759 // If then else pt 4
760 p, program = test(`
761 if(a/*comment*/) {
762 b
763 } else {
764 c
765 }
766 `, nil)
767 is(p.comments.CommentMap.Size(), 1)
768 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.IfStatement).Test], []string{"comment"}, ast.TRAILING), nil)
769
770 // If then else pt 4
771 p, program = test(`
772 if(a)/*comment*/ {
773 b
774 } else {
775 c
776 }
777 `, nil)
778 is(p.comments.CommentMap.Size(), 1)
779 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.IfStatement).Consequent], []string{"comment"}, ast.LEADING), nil)
780
781 // If then else pt 5
782 p, program = test(`
783 if(a) {
784 b
785 } /*comment*/else {
786 c
787 }
788 `, nil)
789 is(p.comments.CommentMap.Size(), 1)
790 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.IfStatement).Consequent], []string{"comment"}, ast.TRAILING), nil)
791
792 // If then else pt 6
793 p, program = test(`
794 if(a) {
795 b
796 } else/*comment*/ {
797 c
798 }
799 `, nil)
800 is(p.comments.CommentMap.Size(), 1)
801 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.IfStatement).Alternate], []string{"comment"}, ast.LEADING), nil)
802
803 // If then else pt 7
804 p, program = test(`
805 if(a) {
806 b
807 } else {
808 c
809 }/*comment*/
810 `, nil)
811 is(p.comments.CommentMap.Size(), 1)
812 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.IfStatement).Alternate], []string{"comment"}, ast.TRAILING), nil)
813
814 // If then else pt 8
815 p, _ = test(`
816 if
817 /*comment*/
818 (a) {
819 b
820 } else {
821 c
822 }
823 `, nil)
824 is(p.comments.CommentMap.Size(), 1)
825
826 // If then else pt 9
827 p, _ = test(`
828 if
829 (a)
830 /*comment*/{
831 b
832 } else {
833 c
834 }
835 `, nil)
836 is(p.comments.CommentMap.Size(), 1)
837
838 // If then else pt 10
839 p, _ = test(`
840 if(a){
841 b
842 }
843 /*comment*/
844 else {
845 c
846 }
847 `, nil)
848 is(p.comments.CommentMap.Size(), 1)
849
850 // Do while
851 p, program = test(`
852 /*comment*/do {
853 a
854 } while(b)
855 `, nil)
856 is(p.comments.CommentMap.Size(), 1)
857 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.DoWhileStatement)], []string{"comment"}, ast.LEADING), nil)
858
859 // Do while pt 2
860 p, program = test(`
861 do /*comment*/ {
862 a
863 } while(b)
864 `, nil)
865 is(p.comments.CommentMap.Size(), 1)
866 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.DoWhileStatement)], []string{"comment"}, ast.DO), nil)
867
868 // Do while pt 3
869 p, program = test(`
870 do {
871 a
872 } /*comment*/ while(b)
873 `, nil)
874 is(p.comments.CommentMap.Size(), 1)
875 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.DoWhileStatement).Body], []string{"comment"}, ast.TRAILING), nil)
876
877 // Do while pt 4
878 p, program = test(`
879 do {
880 a
881 } while/*comment*/(b)
882 `, nil)
883 is(p.comments.CommentMap.Size(), 1)
884 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.DoWhileStatement)], []string{"comment"}, ast.WHILE), nil)
885
886 // Do while pt 5
887 p, program = test(`
888 do {
889 a
890 } while(b)/*comment*/
891 `, nil)
892 is(p.comments.CommentMap.Size(), 1)
893 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.DoWhileStatement)], []string{"comment"}, ast.TRAILING), nil)
894
895 // While
896 p, program = test(`
897 /*comment*/while(a) {
898 b
899 }
900 `, nil)
901 is(p.comments.CommentMap.Size(), 1)
902 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.WhileStatement)], []string{"comment"}, ast.LEADING), nil)
903
904 // While pt 2
905 p, program = test(`
906 while/*comment*/(a) {
907 b
908 }
909 `, nil)
910 is(p.comments.CommentMap.Size(), 1)
911 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.WhileStatement)], []string{"comment"}, ast.WHILE), nil)
912
913 // While pt 3
914 p, program = test(`
915 while(/*comment*/a) {
916 b
917 }
918 `, nil)
919 is(p.comments.CommentMap.Size(), 1)
920 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.WhileStatement).Test], []string{"comment"}, ast.LEADING), nil)
921
922 // While pt 4
923 p, program = test(`
924 while(a/*comment*/) {
925 b
926 }
927 `, nil)
928 is(p.comments.CommentMap.Size(), 1)
929 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.WhileStatement).Test], []string{"comment"}, ast.TRAILING), nil)
930
931 // While pt 5
932 p, program = test(`
933 while(a) /*comment*/ {
934 c
935 }
936 `, nil)
937 is(p.comments.CommentMap.Size(), 1)
938 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.WhileStatement).Body], []string{"comment"}, ast.LEADING), nil)
939
940 // While pt 6
941 p, program = test(`
942 while(a) {
943 c
944 }/*comment*/
945 `, nil)
946 is(p.comments.CommentMap.Size(), 1)
947 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.WhileStatement).Body], []string{"comment"}, ast.TRAILING), nil)
948
949 // While pt 7
950 p, program = test(`
951 while(a) {
952 c/*comment*/
953 }
954 `, nil)
955 is(p.comments.CommentMap.Size(), 1)
956 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.WhileStatement).Body.(*ast.BlockStatement).List[0].(*ast.ExpressionStatement).Expression.(*ast.Identifier)], []string{"comment"}, ast.TRAILING), nil)
957
958 // While pt 7
959 p, program = test(`
960 while(a) {
961 /*comment*/
962 }
963 `, nil)
964 is(p.comments.CommentMap.Size(), 1)
965 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.WhileStatement).Body.(*ast.BlockStatement)], []string{"comment"}, ast.FINAL), nil)
966
967 // While pt 8
968 p, _ = test(`
969 while
970 /*comment*/(a) {
971
972 }
973 `, nil)
974 is(p.comments.CommentMap.Size(), 1)
975
976 // While pt 9
977 p, _ = test(`
978 while
979 (a)
980 /*comment*/{
981
982 }
983 `, nil)
984 is(p.comments.CommentMap.Size(), 1)
985
986 // Break
987 p, program = test(`
988 while(a) {
989 break/*comment*/;
990 }
991 `, nil)
992 is(p.comments.CommentMap.Size(), 1)
993 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.WhileStatement).Body.(*ast.BlockStatement).List[0].(*ast.BranchStatement)], []string{"comment"}, ast.TRAILING), nil)
994
995 // Break pt 2
996 p, program = test(`
997 while(a) {
998 next/*comment*/:
999 break next;
1000 }
1001 `, nil)
1002 is(p.comments.CommentMap.Size(), 1)
1003 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.WhileStatement).Body.(*ast.BlockStatement).List[0].(*ast.LabelledStatement).Label], []string{"comment"}, ast.TRAILING), nil)
1004
1005 // Break pt 3
1006 p, program = test(`
1007 while(a) {
1008 next:/*comment*/
1009 break next;
1010 }
1011 `, nil)
1012 is(p.comments.CommentMap.Size(), 1)
1013 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.WhileStatement).Body.(*ast.BlockStatement).List[0].(*ast.LabelledStatement)], []string{"comment"}, ast.LEADING), nil)
1014
1015 // Break pt 4
1016 p, program = test(`
1017 while(a) {
1018 next:
1019 break /*comment*/next;
1020 }
1021 `, nil)
1022 is(p.comments.CommentMap.Size(), 1)
1023 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.WhileStatement).Body.(*ast.BlockStatement).List[0].(*ast.LabelledStatement).Statement.(*ast.BranchStatement).Label], []string{"comment"}, ast.LEADING), nil)
1024
1025 // Break pt 5
1026 p, program = test(`
1027 while(a) {
1028 next:
1029 break next/*comment*/;
1030 }
1031 `, nil)
1032 is(p.comments.CommentMap.Size(), 1)
1033 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.WhileStatement).Body.(*ast.BlockStatement).List[0].(*ast.LabelledStatement).Statement.(*ast.BranchStatement).Label], []string{"comment"}, ast.TRAILING), nil)
1034
1035 // Debugger
1036 p, program = test(`
1037 debugger // comment
1038 `, nil)
1039 is(p.comments.CommentMap.Size(), 1)
1040 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.DebuggerStatement)], []string{" comment"}, ast.TRAILING), nil)
1041
1042 // Debugger pt 2
1043 p, program = test(`
1044 debugger; // comment
1045 `, nil)
1046 is(p.comments.CommentMap.Size(), 1)
1047 is(checkComments((p.comments.CommentMap)[program], []string{" comment"}, ast.TRAILING), nil)
1048
1049 // Debugger pt 3
1050 p, program = test(`
1051 debugger;
1052 // comment
1053 `, nil)
1054 is(p.comments.CommentMap.Size(), 1)
1055 is(checkComments((p.comments.CommentMap)[program], []string{" comment"}, ast.TRAILING), nil)
1056
1057 // With
1058 p, program = test(`
1059 /*comment*/with(a) {
1060 }
1061 `, nil)
1062 is(p.comments.CommentMap.Size(), 1)
1063 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.WithStatement)], []string{"comment"}, ast.LEADING), nil)
1064
1065 // With pt 2
1066 p, program = test(`
1067 with/*comment*/(a) {
1068 }
1069 `, nil)
1070 is(p.comments.CommentMap.Size(), 1)
1071 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.WithStatement)], []string{"comment"}, ast.WITH), nil)
1072
1073 // With pt 3
1074 p, program = test(`
1075 with(/*comment*/a) {
1076 }
1077 `, nil)
1078 is(p.comments.CommentMap.Size(), 1)
1079 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.WithStatement).Object], []string{"comment"}, ast.LEADING), nil)
1080
1081 // With pt 4
1082 p, program = test(`
1083 with(a/*comment*/) {
1084 }
1085 `, nil)
1086 is(p.comments.CommentMap.Size(), 1)
1087 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.WithStatement).Object], []string{"comment"}, ast.TRAILING), nil)
1088
1089 // With pt 5
1090 p, program = test(`
1091 with(a) /*comment*/ {
1092 }
1093 `, nil)
1094 is(p.comments.CommentMap.Size(), 1)
1095 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.WithStatement).Body], []string{"comment"}, ast.LEADING), nil)
1096
1097 // With pt 6
1098 p, program = test(`
1099 with(a) {
1100 }/*comment*/
1101 `, nil)
1102 is(p.comments.CommentMap.Size(), 1)
1103 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.WithStatement).Body], []string{"comment"}, ast.TRAILING), nil)
1104
1105 // With pt 7
1106 p, _ = test(`
1107 with
1108 /*comment*/(a) {
1109 }
1110 `, nil)
1111 is(p.comments.CommentMap.Size(), 1)
1112
1113 // With pt 8
1114 p, _ = test(`
1115 with
1116 (a)
1117 /*comment*/{
1118 }
1119 `, nil)
1120 is(p.comments.CommentMap.Size(), 1)
1121
1122 // Var
1123 p, program = test(`
1124 /*comment*/var a
1125 `, nil)
1126 is(p.comments.CommentMap.Size(), 1)
1127 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.VariableStatement)], []string{"comment"}, ast.LEADING), nil)
1128
1129 // Var pt 2
1130 p, program = test(`
1131 var/*comment*/ a
1132 `, nil)
1133 is(p.comments.CommentMap.Size(), 1)
1134 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.VariableStatement).List[0]], []string{"comment"}, ast.LEADING), nil)
1135
1136 // Var pt 3
1137 p, program = test(`
1138 var a/*comment*/
1139 `, nil)
1140 is(p.comments.CommentMap.Size(), 1)
1141 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.VariableStatement).List[0]], []string{"comment"}, ast.TRAILING), nil)
1142
1143 // Var pt 4
1144 p, program = test(`
1145 var a/*comment*/, b
1146 `, nil)
1147 is(p.comments.CommentMap.Size(), 1)
1148 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.VariableStatement).List[0].(*ast.VariableExpression)], []string{"comment"}, ast.TRAILING), nil)
1149
1150 // Var pt 5
1151 p, program = test(`
1152 var a, /*comment*/b
1153 `, nil)
1154 is(p.comments.CommentMap.Size(), 1)
1155 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.VariableStatement).List[1].(*ast.VariableExpression)], []string{"comment"}, ast.LEADING), nil)
1156
1157 // Var pt 6
1158 p, program = test(`
1159 var a, b/*comment*/
1160 `, nil)
1161 is(p.comments.CommentMap.Size(), 1)
1162 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.VariableStatement).List[1]], []string{"comment"}, ast.TRAILING), nil)
1163
1164 // Var pt 7
1165 p, program = test(`
1166 var a, b;
1167 /*comment*/
1168 `, nil)
1169 is(p.comments.CommentMap.Size(), 1)
1170 is(checkComments((p.comments.CommentMap)[program], []string{"comment"}, ast.TRAILING), nil)
1171
1172 // Return
1173 p, _ = test(`
1174 function f() {
1175 /*comment*/return o
1176 }
1177 `, nil)
1178 is(p.comments.CommentMap.Size(), 1)
1179
1180 // Try catch
1181 p, program = test(`
1182 /*comment*/try {
1183 a
1184 } catch(b) {
1185 c
1186 }
1187 `, nil)
1188 is(p.comments.CommentMap.Size(), 1)
1189 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.TryStatement)], []string{"comment"}, ast.LEADING), nil)
1190
1191 // Try catch pt 2
1192 p, program = test(`
1193 try/*comment*/ {
1194 a
1195 } catch(b) {
1196 c
1197 } finally {
1198 }
1199 `, nil)
1200 is(p.comments.CommentMap.Size(), 1)
1201 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.TryStatement).Body], []string{"comment"}, ast.LEADING), nil)
1202
1203 // Try catch pt 3
1204 p, program = test(`
1205 try {
1206 a
1207 }/*comment*/ catch(b) {
1208 c
1209 } finally {
1210 }
1211 `, nil)
1212 is(p.comments.CommentMap.Size(), 1)
1213 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.TryStatement).Body], []string{"comment"}, ast.TRAILING), nil)
1214
1215 // Try catch pt 4
1216 p, program = test(`
1217 try {
1218 a
1219 } catch(/*comment*/b) {
1220 c
1221 } finally {
1222 }
1223 `, nil)
1224 is(p.comments.CommentMap.Size(), 1)
1225 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.TryStatement).Catch.Parameter], []string{"comment"}, ast.LEADING), nil)
1226
1227 // Try catch pt 5
1228 p, program = test(`
1229 try {
1230 a
1231 } catch(b/*comment*/) {
1232 c
1233 } finally {
1234 }
1235 `, nil)
1236 is(p.comments.CommentMap.Size(), 1)
1237 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.TryStatement).Catch.Parameter], []string{"comment"}, ast.TRAILING), nil)
1238
1239 // Try catch pt 6
1240 p, program = test(`
1241 try {
1242 a
1243 } catch(b) /*comment*/{
1244 c
1245 } finally {
1246 }
1247 `, nil)
1248 is(p.comments.CommentMap.Size(), 1)
1249 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.TryStatement).Catch.Body], []string{"comment"}, ast.LEADING), nil)
1250
1251 // Try catch pt 7
1252 p, program = test(`
1253 try {
1254 a
1255 } catch(b){
1256 c
1257 } /*comment*/ finally {
1258 }
1259 `, nil)
1260 is(p.comments.CommentMap.Size(), 1)
1261 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.TryStatement).Catch.Body], []string{"comment"}, ast.TRAILING), nil)
1262
1263 // Try catch pt 8
1264 p, program = test(`
1265 try {
1266 a
1267 } catch(b){
1268 c
1269 } finally /*comment*/ {
1270 }
1271 `, nil)
1272 is(p.comments.CommentMap.Size(), 1)
1273 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.TryStatement).Finally], []string{"comment"}, ast.LEADING), nil)
1274
1275 // Try catch pt 9
1276 p, program = test(`
1277 try {
1278 a
1279 } catch(b){
1280 c
1281 } finally {
1282 }/*comment*/
1283 `, nil)
1284 is(p.comments.CommentMap.Size(), 1)
1285 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.TryStatement).Finally], []string{"comment"}, ast.TRAILING), nil)
1286
1287 // Try catch pt 11
1288 p, program = test(`
1289 try {
1290 a
1291 }
1292 /*comment*/
1293 catch(b){
1294 c
1295 } finally {
1296 d
1297 }
1298 `, nil)
1299 is(p.comments.CommentMap.Size(), 1)
1300 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.TryStatement).Body], []string{"comment"}, ast.TRAILING), nil)
1301
1302 // Throw
1303 p, program = test(`
1304 throw a/*comment*/
1305 `, nil)
1306 is(p.comments.CommentMap.Size(), 1)
1307 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ThrowStatement).Argument], []string{"comment"}, ast.TRAILING), nil)
1308
1309 // Throw pt 2
1310 p, program = test(`
1311 /*comment*/throw a
1312 `, nil)
1313 is(p.comments.CommentMap.Size(), 1)
1314 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ThrowStatement)], []string{"comment"}, ast.LEADING), nil)
1315
1316 // Throw pt 3
1317 p, program = test(`
1318 throw /*comment*/a
1319 `, nil)
1320 is(p.comments.CommentMap.Size(), 1)
1321 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ThrowStatement).Argument], []string{"comment"}, ast.LEADING), nil)
1322
1323 // Try catch pt 10
1324 p, program = test(`
1325 try {
1326 a
1327 } catch(b){
1328 c
1329 }
1330 /*comment*/finally {
1331 }
1332 `, nil)
1333 is(p.comments.CommentMap.Size(), 1)
1334 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.TryStatement).Catch.Body], []string{"comment"}, ast.TRAILING), nil)
1335
1336 // Try catch pt 11
1337 p, program = test(`
1338 try {
1339 a
1340 } catch(b){
1341 c
1342 }
1343 finally
1344 /*comment*/
1345 {
1346 d
1347 }
1348 `, nil)
1349 is(p.comments.CommentMap.Size(), 1)
1350 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.TryStatement).Finally], []string{"comment"}, ast.LEADING), nil)
1351
1352 // Switch / comment
1353 p, _ = test(`
1354 var volvo = 1
1355 //comment
1356 switch(abra) {
1357 }
1358 `, nil)
1359 is(p.comments.CommentMap.Size(), 1)
1360
1361 // Switch / comment
1362 p, _ = test(`
1363 f("string",{
1364 key: "val"
1365 //comment
1366 });
1367 `, nil)
1368 is(p.comments.CommentMap.Size(), 1)
1369
1370 // Switch / comment
1371 p, program = test(`
1372 function f() {
1373 /*comment*/if(true){a++}
1374 }
1375 `, nil)
1376 is(p.comments.CommentMap.Size(), 1)
1377 n := program.Body[0].(*ast.FunctionStatement).Function.Body.(*ast.BlockStatement).List[0]
1378 is(checkComments((p.comments.CommentMap)[n], []string{"comment"}, ast.LEADING), nil)
1379
1380 // Function in function
1381 p, program = test(`
1382 function f() {
1383 /*comment*/function f2() {
1384 }
1385 }
1386 `, nil)
1387 is(p.comments.CommentMap.Size(), 1)
1388 n = program.Body[0].(*ast.FunctionStatement).Function.Body.(*ast.BlockStatement).List[0]
1389 is(checkComments((p.comments.CommentMap)[n], []string{"comment"}, ast.LEADING), nil)
1390
1391 p, program = test(`
1392 a + /*comment1*/
1393 /*comment2*/
1394 b/*comment3*/;
1395 /*comment4*/c
1396 `, nil)
1397 is(p.comments.CommentMap.Size(), 4)
1398 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.BinaryExpression).Right], []string{"comment1", "comment2"}, ast.LEADING), nil)
1399 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.BinaryExpression).Right], []string{"comment3"}, ast.TRAILING), nil)
1400 is(checkComments((p.comments.CommentMap)[program.Body[1]], []string{"comment4"}, ast.LEADING), nil)
1401
1402 p, program = test(`
1403 a + /*comment1*/
1404 /*comment2*/
1405 b/*comment3*/
1406 /*comment4*/c
1407 `, nil)
1408 is(p.comments.CommentMap.Size(), 4)
1409 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.BinaryExpression).Right], []string{"comment1", "comment2"}, ast.LEADING), nil)
1410 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.BinaryExpression).Right], []string{"comment3"}, ast.TRAILING), nil)
1411 is(checkComments((p.comments.CommentMap)[program.Body[1]], []string{"comment4"}, ast.LEADING), nil)
1412
1413 // New
1414 p, program = test(`
1415 a = /*comment1*/new /*comment2*/ obj/*comment3*/()
1416 `, nil)
1417 is(p.comments.CommentMap.Size(), 3)
1418 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.AssignExpression).Right], []string{"comment1"}, ast.LEADING), nil)
1419 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.AssignExpression).Right.(*ast.NewExpression).Callee], []string{"comment2"}, ast.LEADING), nil)
1420 is(checkComments((p.comments.CommentMap)[program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.AssignExpression).Right.(*ast.NewExpression).Callee], []string{"comment3"}, ast.TRAILING), nil)
1421 })
1422 }
1423
1424 func TestParser_comments2(t *testing.T) {
1425 tt(t, func() {
1426 test := func(source string, chk interface{}) (*parser, *ast.Program) {
1427 parser, program, err := testParseWithMode(source, StoreComments)
1428 is(firstErr(err), chk)
1429
1430 // Check unresolved comments
1431 is(len(parser.comments.Comments), 0)
1432 return parser, program
1433 }
1434
1435 parser, program := test(`
1436 a = /*comment1*/new /*comment2*/ obj/*comment3*/()
1437 `, nil)
1438 n := program.Body[0]
1439 fmt.Printf("FOUND NODE: %v, number of comments: %v\n", reflect.TypeOf(n), len(parser.comments.CommentMap[n])) //nolint:forbidigo
1440 displayComments(parser.comments.CommentMap)
1441 })
1442 }