comment-parser 0.2.3 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,78 +2,71 @@ var fs = require('fs');
2
2
  var expect = require('chai').expect;
3
3
  var parse = require('../index');
4
4
 
5
- describe('Single comment string parsing', function() {
5
+ describe('Comment string parsing', function() {
6
+
7
+ /**
8
+ * Source lines numeration:
9
+ *
10
+ * 0 function() {
11
+ * 1 // source with comments
12
+ * 2 }
13
+ *
14
+ */
6
15
 
7
16
  function parsed(func, opts) {
8
17
  var str = func.toString();
9
18
  return parse(str.slice(
10
19
  str.indexOf('{') + 1,
11
20
  str.lastIndexOf('}')
12
- ).trim(), opts);
21
+ ), opts);
13
22
  }
14
23
 
15
- it('should parse one-liner doc block', function() {
16
- expect(parsed(function(){
17
- /** Description */
18
- var a;
19
- })[0].description)
20
- .to.eq('Description');
21
- });
22
-
23
- it('should gracefully parse one-liner with tag', function() {
24
- var p = parsed(function(){
25
- /** @tag */
26
- var a;
27
- });
28
-
29
- expect(p[0].description)
30
- .to.eq('');
31
-
32
- expect(p[0].line)
33
- .to.eq(0);
34
-
35
- expect(p[0].tags.length)
36
- .to.eq(1);
37
-
38
- expect(p[0].tags[0].tag)
39
- .to.eq('tag');
40
-
41
- expect(p[0].tags[0].line)
42
- .to.eq(0);
43
- });
44
-
45
- it('should parse simple doc block with description', function() {
24
+ it('should parse doc block with description', function() {
46
25
  expect(parsed(function(){
47
26
  /**
48
27
  * Description
49
28
  */
50
- })[0].description)
51
- .to.eq('Description');
29
+ })[0])
30
+ .to.eql({
31
+ description : 'Description',
32
+ source : 'Description',
33
+ line : 1,
34
+ tags : []
35
+ });
52
36
  });
53
37
 
54
- it('should split the description', function() {
38
+ it('should skip surrounding empty lines while preserving line numbers', function() {
55
39
  expect(parsed(function(){
56
40
  /**
41
+ *
42
+ *
57
43
  * Description first line
58
44
  *
59
45
  * Description second line
46
+ *
60
47
  */
61
48
  var a;
62
- })[0].description)
63
- .to.eq('Description first line\n\nDescription second line');
49
+ })[0])
50
+ .eql({
51
+ description : 'Description first line\n\nDescription second line',
52
+ source : 'Description first line\n\nDescription second line',
53
+ line : 1,
54
+ tags : []
55
+ });
64
56
  });
65
57
 
66
- it('should keep description "" if omitted', function() {
58
+ it('should skip empty blocks', function() {
59
+
67
60
  expect(parsed(function(){
68
61
  /**
69
62
  *
70
63
  */
71
64
  var a;
72
- })[0].description)
73
- .to.eq('');
65
+ }).length)
66
+ .to.eq(0);
74
67
  });
75
68
 
76
- it('should parse multiple comments', function() {
69
+ it('should parse multiple doc blocks', function() {
77
70
  var p = parsed(function(){
78
71
  /**
79
72
  * Description first line
@@ -89,14 +82,37 @@ describe('Single comment string parsing', function() {
89
82
  expect(p.length)
90
83
  .to.eq(2);
91
84
 
92
- expect(p[0].description)
93
- .to.eq('Description first line');
85
+ expect(p[0])
86
+ .to.eql({
87
+ description : 'Description first line',
88
+ source : 'Description first line',
89
+ line : 1,
90
+ tags : []
91
+ });
92
+
93
+ expect(p[1])
94
+ .to.eql({
95
+ description : 'Description second line',
96
+ source : 'Description second line',
97
+ line : 6,
98
+ tags : []
99
+ });
100
+ });
94
101
 
95
- expect(p[1].description)
96
- .to.eq('Description second line');
102
+ it('should parse one line block', function() {
103
+ expect(parsed(function(){
104
+ /** Description */
105
+ var a;
106
+ })[0])
107
+ .to.eql({
108
+ description : 'Description',
109
+ source : 'Description',
110
+ line : 1,
111
+ tags : []
112
+ });
97
113
  });
98
114
 
99
- it('should return `null` for `/* */` comments', function() {
115
+ it('should skip `/* */` comments', function() {
100
116
  expect(parsed(function(){
101
117
  /*
102
118
  *
@@ -106,7 +122,7 @@ describe('Single comment string parsing', function() {
106
122
  .to.eq(0);
107
123
  });
108
124
 
109
- it('should return `null` for `/*** */` comments', function() {
125
+ it('should skip `/*** */` comments', function() {
110
126
  expect(parsed(function(){
111
127
  /*
112
128
  *
@@ -116,21 +132,68 @@ describe('Single comment string parsing', function() {
116
132
  .to.eq(0);
117
133
  });
118
134
 
135
+ it('should preserve empty lines and indentation with `opts.trim = false`', function() {
136
+ expect(parsed(function(){
137
+ /**
138
+ *
139
+ *
140
+ * Description first line
141
+ * second line
142
+ *
143
+ * third line
144
+ */
145
+ var a;
146
+ }, {
147
+ trim: false
148
+ })[0])
149
+ .eql({
150
+ description : '\n\n\n Description first line\n second line\n\n third line\n',
151
+ source : '\n\n\n Description first line\n second line\n\n third line\n',
152
+ line : 1,
153
+ tags : []
154
+ });
155
+ });
156
+
157
+ it('should parse one line block with tag', function() {
158
+ expect(parsed(function(){
159
+ /** @tag */
160
+ var a;
161
+ })[0])
162
+ .to.eql({
163
+ description : '',
164
+ line : 1,
165
+ source : '@tag',
166
+ tags : [{
167
+ tag : 'tag',
168
+ type : '',
169
+ name : '',
170
+ description : '',
171
+ line : 1,
172
+ optional : false,
173
+ source : '@tag'
174
+ }]
175
+ });
176
+ });
177
+
119
178
  it('should parse `@tag`', function() {
120
179
  expect(parsed(function(){
121
180
  /**
181
+ *
122
182
  * @my-tag
123
183
  */
124
184
  var a;
125
185
  })[0])
126
186
  .to.eql({
127
- line : 0,
187
+ line : 1,
188
+ source : '@my-tag',
128
189
  description : '',
129
190
  tags: [{
130
- line : 1,
191
+ line : 3,
131
192
  tag : 'my-tag',
193
+ source : '@my-tag',
132
194
  type : '',
133
195
  name : '',
196
+ optional : false,
134
197
  description : ''
135
198
  }]
136
199
  });
@@ -144,135 +207,218 @@ describe('Single comment string parsing', function() {
144
207
  var a;
145
208
  })[0])
146
209
  .to.eql({
147
- line : 0,
210
+ line : 1,
211
+ source : '@my-tag {my.type}',
148
212
  description : '',
149
213
  tags: [{
150
- line : 1,
214
+ line : 2,
151
215
  tag : 'my-tag',
152
216
  type : 'my.type',
153
217
  name : '',
218
+ source : '@my-tag {my.type}',
219
+ optional : false,
220
+ description : ''
221
+ }]
222
+ });
223
+ });
224
+
225
+ it('should parse tag with name only `@tag name`', function() {
226
+ expect(parsed(function(){
227
+ /**
228
+ * @my-tag name
229
+ */
230
+ })[0])
231
+ .to.eql({
232
+ line : 1,
233
+ description : '',
234
+ source : '@my-tag name',
235
+ tags: [{
236
+ line : 2,
237
+ tag : 'my-tag',
238
+ type : '',
239
+ name : 'name',
240
+ source : '@my-tag name',
241
+ optional : false,
154
242
  description : ''
155
243
  }]
156
244
  });
157
245
  });
158
246
 
159
- it('should parse `@tag {my.type} name`', function() {
247
+ it('should parse tag with type and name `@tag {my.type} name`', function() {
160
248
  expect(parsed(function(){
161
249
  /**
162
250
  * @my-tag {my.type} name
163
251
  */
164
- var a;
165
252
  })[0])
166
253
  .to.eql({
167
- line : 0,
254
+ line : 1,
255
+ source : '@my-tag {my.type} name',
168
256
  description : '',
169
257
  tags: [{
170
258
  tag : 'my-tag',
171
- line : 1,
259
+ line : 2,
172
260
  type : 'my.type',
173
261
  name : 'name',
174
- description : ''
262
+ source : '@my-tag {my.type} name',
263
+ description : '',
264
+ optional : false
175
265
  }]
176
266
  });
177
267
  });
178
268
 
179
- it('should parse `@tag name`', function() {
269
+ it('should parse tag with type, name and description `@tag {my.type} name description`', function() {
180
270
  expect(parsed(function(){
181
271
  /**
182
- * @my-tag name
272
+ * @my-tag {my.type} name description
183
273
  */
184
274
  })[0])
185
275
  .to.eql({
186
- line : 0,
276
+ line : 1,
277
+ source : '@my-tag {my.type} name description',
187
278
  description : '',
188
279
  tags: [{
189
- line : 1,
190
280
  tag : 'my-tag',
191
- type : '',
281
+ line : 2,
282
+ type : 'my.type',
192
283
  name : 'name',
193
- description : ''
284
+ source : '@my-tag {my.type} name description',
285
+ description : 'description',
286
+ optional : false
287
+ }]
288
+ });
289
+ });
290
+
291
+ it('should parse tag with multiline description', function() {
292
+ expect(parsed(function(){
293
+ /**
294
+ * @my-tag {my.type} name description line 1
295
+ * description line 2
296
+ * description line 3
297
+ */
298
+ })[0])
299
+ .to.eql({
300
+ line : 1,
301
+ source : '@my-tag {my.type} name description line 1\ndescription line 2\ndescription line 3',
302
+ description : '',
303
+ tags: [{
304
+ tag : 'my-tag',
305
+ line : 2,
306
+ type : 'my.type',
307
+ name : 'name',
308
+ source : '@my-tag {my.type} name description line 1\ndescription line 2\ndescription line 3',
309
+ description : 'description line 1\ndescription line 2\ndescription line 3',
310
+ optional : false
194
311
  }]
195
312
  });
196
313
  });
197
314
 
198
- it('should parse `@tag {my.type} [name]`', function() {
315
+ it('should parse tag with type and optional name `@tag {my.type} [name]`', function() {
199
316
  expect(parsed(function(){
200
317
  /**
201
318
  * @my-tag {my.type} [name]
202
319
  */
203
320
  })[0])
204
321
  .to.eql({
205
- line : 0,
322
+ line : 1,
323
+ description : '',
324
+ source : '@my-tag {my.type} [name]',
325
+ tags: [{
326
+ tag : 'my-tag',
327
+ line : 2,
328
+ type : 'my.type',
329
+ name : 'name',
330
+ description : '',
331
+ source : '@my-tag {my.type} [name]',
332
+ optional : true
333
+ }]
334
+ });
335
+ });
336
+
337
+ it('should parse tag with type and optional name with default value `@tag {my.type} [name=value]`', function() {
338
+ expect(parsed(function(){
339
+ /**
340
+ * @my-tag {my.type} [name=value]
341
+ */
342
+ })[0])
343
+ .to.eql({
344
+ line : 1,
206
345
  description : '',
346
+ source : '@my-tag {my.type} [name=value]',
207
347
  tags: [{
208
348
  tag : 'my-tag',
209
- line : 1,
349
+ line : 2,
210
350
  type : 'my.type',
211
351
  name : 'name',
352
+ default : 'value',
353
+ source : '@my-tag {my.type} [name=value]',
212
354
  description : '',
213
355
  optional : true
214
356
  }]
215
357
  });
216
358
  });
217
359
 
218
- // http://usejsdoc.org/tags-param.html
219
- it('should parse `@tag {my.type} [name=John Doe]`', function() {
360
+ it('should tolerate default value with whitespces `@tag {my.type} [name=John Doe]`', function() {
220
361
  expect(parsed(function(){
221
362
  /**
222
363
  * @my-tag {my.type} [name=John Doe]
223
364
  */
224
365
  })[0])
225
366
  .to.eql({
226
- line : 0,
367
+ line : 1,
227
368
  description : '',
369
+ source : '@my-tag {my.type} [name=John Doe]',
228
370
  tags: [{
229
371
  tag : 'my-tag',
230
- line : 1,
372
+ line : 2,
231
373
  type : 'my.type',
232
374
  name : 'name',
233
375
  description : '',
376
+ source : '@my-tag {my.type} [name=John Doe]',
234
377
  optional : true,
235
378
  default : 'John Doe'
236
379
  }]
237
380
  });
238
381
  });
239
382
 
240
- // https://github.com/senchalabs/jsduck/wiki/@param
241
- it('should parse quoted optionals like `@tag [name="yay"] desc`', function() {
383
+ it('should tolerate quoted default value `@tag [name="yay!"]`', function() {
242
384
  expect(parsed(function(){
243
385
  /**
244
- * @tag {t} [name="yay!"] desc
386
+ * @tag {t} [name="yay!"]
245
387
  */
246
388
  })[0])
247
389
  .to.eql({
248
- line: 0,
390
+ line: 1,
391
+ source: '@tag {t} [name="yay!"]',
249
392
  description: '',
250
393
  tags: [{
251
394
  tag : 'tag',
252
- line : 1,
395
+ line : 2,
253
396
  type : 't',
254
397
  name : 'name',
398
+ source : '@tag {t} [name="yay!"]',
255
399
  default : 'yay!',
256
400
  optional : true,
257
- description : 'desc'
401
+ description : ''
258
402
  }]
259
403
  });
260
404
  });
261
405
 
262
- it('shouldn\'t strip different quotes in `@tag [name="yay\'] desc`', function() {
406
+ it('should keep value as is if quotes are mismatched `@tag [name="yay\']`', function() {
263
407
  expect(parsed(function(){
264
408
  /**
265
409
  * @tag {t} [name="yay!'] desc
266
410
  */
267
411
  })[0])
268
412
  .to.eql({
269
- line: 0,
413
+ line: 1,
270
414
  description: '',
415
+ source : '@tag {t} [name="yay!\'] desc',
271
416
  tags: [{
272
417
  tag : 'tag',
273
- line : 1,
418
+ line : 2,
274
419
  type : 't',
275
420
  name : 'name',
421
+ source : '@tag {t} [name="yay!\'] desc',
276
422
  default : '"yay!\'',
277
423
  optional : true,
278
424
  description : 'desc'
@@ -280,21 +426,45 @@ describe('Single comment string parsing', function() {
280
426
  });
281
427
  });
282
428
 
283
- it('should parse optional names like `@tag [...name] desc`', function() {
429
+ it('should parse rest names `@tag ...name desc`', function() {
430
+ expect(parsed(function(){
431
+ /**
432
+ * @tag {t} ...name desc
433
+ */
434
+ })[0])
435
+ .to.eql({
436
+ line : 1,
437
+ description : '',
438
+ source : '@tag {t} ...name desc',
439
+ tags: [{
440
+ tag : 'tag',
441
+ line : 2,
442
+ type : 't',
443
+ name : '...name',
444
+ optional : false,
445
+ source : '@tag {t} ...name desc',
446
+ description : 'desc'
447
+ }]
448
+ });
449
+ });
450
+
451
+ it('should parse optional rest names `@tag [...name] desc`', function() {
284
452
  expect(parsed(function(){
285
453
  /**
286
454
  * @tag {t} [...name] desc
287
455
  */
288
456
  })[0])
289
457
  .to.eql({
290
- line: 0,
291
- description: '',
458
+ line : 1,
459
+ description : '',
460
+ source : '@tag {t} [...name] desc',
292
461
  tags: [{
293
462
  tag : 'tag',
294
- line : 1,
463
+ line : 2,
295
464
  type : 't',
296
465
  name : '...name',
297
466
  optional : true,
467
+ source : '@tag {t} [...name] desc',
298
468
  description : 'desc'
299
469
  }]
300
470
  });
@@ -311,17 +481,22 @@ describe('Single comment string parsing', function() {
311
481
  .to.eql({
312
482
  line : 1,
313
483
  description : 'Description',
484
+ source : 'Description\n@my-tag1\n@my-tag2',
314
485
  tags : [{
315
486
  tag : 'my-tag1',
316
- line : 2,
487
+ line : 3,
317
488
  type : '',
318
489
  name : '',
490
+ optional : false,
491
+ source : '@my-tag1',
319
492
  description : ''
320
493
  }, {
321
494
  tag : 'my-tag2',
322
- line : 3,
495
+ line : 4,
323
496
  type : '',
324
497
  name : '',
498
+ optional : false,
499
+ source : '@my-tag2',
325
500
  description : ''
326
501
  }]
327
502
  });
@@ -339,23 +514,30 @@ describe('Single comment string parsing', function() {
339
514
  .to.eql({
340
515
  line : 1,
341
516
  description : 'Description',
517
+ source : "Description\n@my-tag name\n@my-tag name.sub-name\n@my-tag name.sub-name.sub-sub-name",
342
518
  tags : [{
343
519
  tag : 'my-tag',
344
- line : 2,
520
+ line : 3,
345
521
  type : '',
346
522
  name : 'name',
523
+ source : '@my-tag name',
524
+ optional : false,
347
525
  description : '',
348
526
  tags : [{
349
527
  tag : 'my-tag',
350
- line : 3,
528
+ line : 4,
351
529
  type : '',
352
530
  name : 'sub-name',
531
+ optional : false,
532
+ source : '@my-tag name.sub-name',
353
533
  description : '',
354
534
  tags : [{
355
535
  tag : 'my-tag',
356
- line : 4,
536
+ line : 5,
357
537
  type : '',
358
538
  name : 'sub-sub-name',
539
+ optional : false,
540
+ source : '@my-tag name.sub-name.sub-sub-name',
359
541
  description : ''
360
542
  }]
361
543
  }]
@@ -370,13 +552,16 @@ describe('Single comment string parsing', function() {
370
552
  */
371
553
  })[0])
372
554
  .to.eql({
373
- line: 0,
374
- description: '',
555
+ line : 1,
556
+ source : '@my-tag {{a: number}} name',
557
+ description : '',
375
558
  tags: [{
376
559
  tag : 'my-tag',
377
- line : 1,
560
+ line : 2,
378
561
  type : '{a: number}',
379
562
  name : 'name',
563
+ source : '@my-tag {{a: number}} name',
564
+ optional : false,
380
565
  description : ''
381
566
  }]
382
567
  });
@@ -389,16 +574,41 @@ describe('Single comment string parsing', function() {
389
574
  */
390
575
  })[0])
391
576
  .to.eql({
392
- line: 0,
393
- description: '',
577
+ line : 1,
578
+ description : '',
579
+ source : '@my-tag {{a: number} name',
394
580
  tags: [{
395
581
  tag : 'my-tag',
396
- line : 1,
582
+ line : 2,
397
583
  type : '',
398
584
  name : '',
399
- description : '{{a: number} name',
400
- error : 'Unpaired curly in type doc'
585
+ description : '',
586
+ source : '@my-tag {{a: number} name',
587
+ optional : false,
588
+ errors : ['parse_type: Invalid `{type}`, unpaired curlies']
401
589
  }]
402
590
  });
403
591
  });
592
+
593
+ it('parses $ in description`', function() {
594
+ expect(parsed(function(){
595
+ /**
596
+ * @my-tag {String} name description with $ char
597
+ */
598
+ })[0])
599
+ .to.eql({
600
+ line : 1,
601
+ source : '@my-tag {String} name description with $ char',
602
+ description : '',
603
+ tags: [{
604
+ tag : 'my-tag',
605
+ line : 2,
606
+ type : 'String',
607
+ name : 'name',
608
+ source : '@my-tag {String} name description with $ char',
609
+ optional : false,
610
+ description : 'description with $ char'
611
+ }]
612
+ });
613
+ });
404
614
  });