comment-parser 1.2.1 → 1.3.0

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.
Files changed (41) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/browser/index.js +53 -36
  3. package/es6/index.d.ts +7 -0
  4. package/es6/index.js +2 -0
  5. package/es6/parser/index.d.ts +3 -2
  6. package/es6/parser/index.js +4 -3
  7. package/es6/parser/source-parser.d.ts +3 -2
  8. package/es6/parser/source-parser.js +12 -12
  9. package/es6/parser/tokenizers/description.d.ts +4 -3
  10. package/es6/parser/tokenizers/description.js +7 -6
  11. package/es6/primitives.d.ts +7 -0
  12. package/es6/primitives.js +1 -0
  13. package/es6/transforms/align.d.ts +2 -1
  14. package/es6/transforms/align.js +7 -7
  15. package/lib/index.cjs +9 -1
  16. package/lib/index.cjs.map +1 -1
  17. package/lib/index.d.ts +7 -0
  18. package/lib/parser/index.cjs +6 -2
  19. package/lib/parser/index.cjs.map +1 -1
  20. package/lib/parser/index.d.ts +3 -2
  21. package/lib/parser/source-parser.cjs +11 -10
  22. package/lib/parser/source-parser.cjs.map +1 -1
  23. package/lib/parser/source-parser.d.ts +3 -2
  24. package/lib/parser/tokenizers/description.cjs +7 -6
  25. package/lib/parser/tokenizers/description.cjs.map +1 -1
  26. package/lib/parser/tokenizers/description.d.ts +4 -3
  27. package/lib/primitives.cjs +2 -0
  28. package/lib/primitives.cjs.map +1 -1
  29. package/lib/primitives.d.ts +7 -0
  30. package/lib/transforms/align.cjs +7 -7
  31. package/lib/transforms/align.cjs.map +1 -1
  32. package/lib/transforms/align.d.ts +2 -1
  33. package/package.json +6 -5
  34. package/src/index.ts +3 -0
  35. package/src/parser/index.ts +6 -3
  36. package/src/parser/source-parser.ts +14 -12
  37. package/src/parser/tokenizers/description.ts +11 -8
  38. package/src/primitives.ts +8 -0
  39. package/src/transforms/align.ts +14 -12
  40. package/tests/e2e/examples.js +1 -1
  41. package/tests/unit/source-parser.spec.ts +150 -0
@@ -1,5 +1,5 @@
1
1
  import { Transform } from './index';
2
- import { Markers, Block, Line } from '../primitives';
2
+ import { BlockMarkers, Block, Line, Markers } from '../primitives';
3
3
  import { rewireSource } from '../util';
4
4
 
5
5
  interface Width {
@@ -16,16 +16,18 @@ const zeroWidth = {
16
16
  name: 0,
17
17
  };
18
18
 
19
- const getWidth = (w: Width, { tokens: t }: Line) => ({
20
- start: t.delimiter === Markers.start ? t.start.length : w.start,
21
- tag: Math.max(w.tag, t.tag.length),
22
- type: Math.max(w.type, t.type.length),
23
- name: Math.max(w.name, t.name.length),
24
- });
19
+ const getWidth =
20
+ (markers = Markers) =>
21
+ (w: Width, { tokens: t }: Line) => ({
22
+ start: t.delimiter === markers.start ? t.start.length : w.start,
23
+ tag: Math.max(w.tag, t.tag.length),
24
+ type: Math.max(w.type, t.type.length),
25
+ name: Math.max(w.name, t.name.length),
26
+ });
25
27
 
26
28
  const space = (len: number) => ''.padStart(len, ' ');
27
29
 
28
- export default function align(): Transform {
30
+ export default function align(markers = Markers): Transform {
29
31
  let intoTags = false;
30
32
  let w: Width;
31
33
 
@@ -40,16 +42,16 @@ export default function align(): Transform {
40
42
  tokens.description === '';
41
43
 
42
44
  // dangling '*/'
43
- if (tokens.end === Markers.end && isEmpty) {
45
+ if (tokens.end === markers.end && isEmpty) {
44
46
  tokens.start = space(w.start + 1);
45
47
  return { ...line, tokens };
46
48
  }
47
49
 
48
50
  switch (tokens.delimiter) {
49
- case Markers.start:
51
+ case markers.start:
50
52
  tokens.start = space(w.start);
51
53
  break;
52
- case Markers.delim:
54
+ case markers.delim:
53
55
  tokens.start = space(w.start + 1);
54
56
  break;
55
57
  default:
@@ -101,7 +103,7 @@ export default function align(): Transform {
101
103
  }
102
104
 
103
105
  return ({ source, ...fields }: Block): Block => {
104
- w = source.reduce(getWidth, { ...zeroWidth });
106
+ w = source.reduce(getWidth(markers), { ...zeroWidth });
105
107
  return rewireSource({ ...fields, source: source.map(update) });
106
108
  };
107
109
  }
@@ -80,7 +80,7 @@ function parse_escaping(source, parse, stringify, transforms) {
80
80
  // parse(source, {fence: '###'}) -- update source correspondingly
81
81
 
82
82
  /**
83
- * @example "some code"
83
+ * @example "some code"
84
84
  ###
85
85
  @decorator
86
86
  function hello() {
@@ -286,3 +286,153 @@ test('carriage returns', () => {
286
286
 
287
287
  expect(parsed).toEqual([...nulls(3), block, null]);
288
288
  });
289
+
290
+ test('custom markers', () => {
291
+ _parse = getParser({
292
+ markers: {
293
+ start: '////',
294
+ nostart: '// ',
295
+ delim: '///',
296
+ end: '////',
297
+ },
298
+ });
299
+
300
+ const parsed = parse(`
301
+ ////
302
+ /// description 0
303
+ ///
304
+ /// description 1
305
+ ///
306
+ /// @param {string} value value description 0
307
+ \`\`\`
308
+ @sample code
309
+ \`\`\`
310
+ /// description 1
311
+ ////`);
312
+
313
+ const block = [
314
+ {
315
+ number: 1,
316
+ source: ' ////',
317
+ tokens: seedTokens({
318
+ start: ' ',
319
+ delimiter: '////',
320
+ postDelimiter: '',
321
+ description: '',
322
+ end: '',
323
+ }),
324
+ },
325
+ {
326
+ number: 2,
327
+ source: ' /// description 0',
328
+ tokens: seedTokens({
329
+ start: ' ',
330
+ delimiter: '///',
331
+ postDelimiter: ' ',
332
+ description: 'description 0',
333
+ end: '',
334
+ }),
335
+ },
336
+ {
337
+ number: 3,
338
+ source: ' ///',
339
+ tokens: seedTokens({
340
+ start: ' ',
341
+ delimiter: '///',
342
+ postDelimiter: '',
343
+ description: '',
344
+ end: '',
345
+ }),
346
+ },
347
+ {
348
+ number: 4,
349
+ source: ' /// description 1',
350
+ tokens: seedTokens({
351
+ start: ' ',
352
+ delimiter: '///',
353
+ postDelimiter: ' ',
354
+ description: 'description 1',
355
+ end: '',
356
+ }),
357
+ },
358
+ {
359
+ number: 5,
360
+ source: ' ///',
361
+ tokens: seedTokens({
362
+ start: ' ',
363
+ delimiter: '///',
364
+ postDelimiter: '',
365
+ description: '',
366
+ end: '',
367
+ }),
368
+ },
369
+ {
370
+ number: 6,
371
+ source: ' /// @param {string} value value description 0',
372
+ tokens: seedTokens({
373
+ start: ' ',
374
+ delimiter: '///',
375
+ postDelimiter: ' ',
376
+ description: '@param {string} value value description 0',
377
+ end: '',
378
+ }),
379
+ },
380
+ {
381
+ number: 7,
382
+ source: ' ```',
383
+ tokens: seedTokens({
384
+ start: ' ',
385
+ delimiter: '',
386
+ postDelimiter: '',
387
+ description: '```',
388
+ end: '',
389
+ }),
390
+ },
391
+ {
392
+ number: 8,
393
+ source: ' @sample code',
394
+ tokens: seedTokens({
395
+ start: ' ',
396
+ delimiter: '',
397
+ postDelimiter: '',
398
+ description: '@sample code',
399
+ end: '',
400
+ }),
401
+ },
402
+ {
403
+ number: 9,
404
+ source: ' ```',
405
+ tokens: seedTokens({
406
+ start: ' ',
407
+ delimiter: '',
408
+ postDelimiter: '',
409
+ description: '```',
410
+ end: '',
411
+ }),
412
+ },
413
+ {
414
+ number: 10,
415
+ source: ' /// description 1',
416
+ tokens: seedTokens({
417
+ start: ' ',
418
+ delimiter: '///',
419
+ postDelimiter: ' ',
420
+ description: 'description 1',
421
+ end: '',
422
+ }),
423
+ },
424
+ {
425
+ number: 11,
426
+ source: ' ////',
427
+ tokens: seedTokens({
428
+ start: ' ',
429
+ delimiter: '',
430
+ postDelimiter: '',
431
+ description: '',
432
+ end: '////',
433
+ }),
434
+ },
435
+ ];
436
+
437
+ expect(parsed).toEqual([...nulls(11), block]);
438
+ });