comment-parser 1.2.3 → 1.3.1
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.
- package/CHANGELOG.md +10 -0
- package/browser/index.js +39 -40
- package/es6/parser/index.d.ts +3 -2
- package/es6/parser/index.js +4 -6
- package/es6/parser/source-parser.d.ts +3 -2
- package/es6/parser/source-parser.js +12 -12
- package/es6/parser/tokenizers/description.d.ts +4 -3
- package/es6/parser/tokenizers/description.js +7 -6
- package/es6/parser/tokenizers/tag.d.ts +1 -1
- package/es6/parser/tokenizers/tag.js +1 -1
- package/es6/primitives.d.ts +7 -0
- package/es6/primitives.js +1 -0
- package/es6/transforms/align.d.ts +2 -1
- package/es6/transforms/align.js +7 -7
- package/lib/parser/index.cjs +6 -6
- package/lib/parser/index.cjs.map +1 -1
- package/lib/parser/index.d.ts +3 -2
- package/lib/parser/source-parser.cjs +11 -10
- package/lib/parser/source-parser.cjs.map +1 -1
- package/lib/parser/source-parser.d.ts +3 -2
- package/lib/parser/tokenizers/description.cjs +7 -6
- package/lib/parser/tokenizers/description.cjs.map +1 -1
- package/lib/parser/tokenizers/description.d.ts +4 -3
- package/lib/parser/tokenizers/tag.cjs +1 -1
- package/lib/parser/tokenizers/tag.cjs.map +1 -1
- package/lib/parser/tokenizers/tag.d.ts +1 -1
- package/lib/primitives.cjs +2 -0
- package/lib/primitives.cjs.map +1 -1
- package/lib/primitives.d.ts +7 -0
- package/lib/transforms/align.cjs +7 -7
- package/lib/transforms/align.cjs.map +1 -1
- package/lib/transforms/align.d.ts +2 -1
- package/package.json +6 -6
- package/src/parser/index.ts +6 -7
- package/src/parser/source-parser.ts +14 -12
- package/src/parser/tokenizers/description.ts +11 -8
- package/src/parser/tokenizers/tag.ts +1 -1
- package/src/primitives.ts +8 -0
- package/src/transforms/align.ts +14 -12
- package/tests/e2e/examples.js +1 -1
- package/tests/e2e/parse.spec.js +0 -9
- package/tests/unit/parser.spec.ts +105 -5
- package/tests/unit/source-parser.spec.ts +150 -0
- package/tests/unit/util.spec.ts +10 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
|
+
# v1.3.1
|
|
2
|
+
- allow for valid empty jsdoc; fixes #128
|
|
3
|
+
|
|
4
|
+
# v1.3.0
|
|
5
|
+
- add support for custom block markers
|
|
6
|
+
|
|
7
|
+
# v1.2.4
|
|
8
|
+
- reverting engine constraint back to ^12.0.0
|
|
9
|
+
|
|
1
10
|
# v1.2.3
|
|
2
11
|
- publishing missing fix: point package's main to .cjs file
|
|
12
|
+
|
|
3
13
|
# v1.2.2
|
|
4
14
|
- re-export ./util on the top-level for compatibility with older Node
|
|
5
15
|
- point package's main to .cjs file
|
package/browser/index.js
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
var CommentParser = (function (exports) {
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
+
/** @deprecated */
|
|
5
|
+
exports.Markers = void 0;
|
|
6
|
+
(function (Markers) {
|
|
7
|
+
Markers["start"] = "/**";
|
|
8
|
+
Markers["nostart"] = "/***";
|
|
9
|
+
Markers["delim"] = "*";
|
|
10
|
+
Markers["end"] = "*/";
|
|
11
|
+
})(exports.Markers || (exports.Markers = {}));
|
|
12
|
+
|
|
4
13
|
function isSpace(source) {
|
|
5
14
|
return /^\s+$/.test(source);
|
|
6
15
|
}
|
|
@@ -81,15 +90,7 @@ var CommentParser = (function (exports) {
|
|
|
81
90
|
return fence;
|
|
82
91
|
}
|
|
83
92
|
|
|
84
|
-
exports.Markers =
|
|
85
|
-
(function (Markers) {
|
|
86
|
-
Markers["start"] = "/**";
|
|
87
|
-
Markers["nostart"] = "/***";
|
|
88
|
-
Markers["delim"] = "*";
|
|
89
|
-
Markers["end"] = "*/";
|
|
90
|
-
})(exports.Markers || (exports.Markers = {}));
|
|
91
|
-
|
|
92
|
-
function getParser$2({ startLine = 0, } = {}) {
|
|
93
|
+
function getParser$2({ startLine = 0, markers = exports.Markers, } = {}) {
|
|
93
94
|
let block = null;
|
|
94
95
|
let num = startLine;
|
|
95
96
|
return function parseSource(source) {
|
|
@@ -98,29 +99,29 @@ var CommentParser = (function (exports) {
|
|
|
98
99
|
[tokens.lineEnd, rest] = splitCR(rest);
|
|
99
100
|
[tokens.start, rest] = splitSpace(rest);
|
|
100
101
|
if (block === null &&
|
|
101
|
-
rest.startsWith(
|
|
102
|
-
!rest.startsWith(
|
|
102
|
+
rest.startsWith(markers.start) &&
|
|
103
|
+
!rest.startsWith(markers.nostart)) {
|
|
103
104
|
block = [];
|
|
104
|
-
tokens.delimiter = rest.slice(0,
|
|
105
|
-
rest = rest.slice(
|
|
105
|
+
tokens.delimiter = rest.slice(0, markers.start.length);
|
|
106
|
+
rest = rest.slice(markers.start.length);
|
|
106
107
|
[tokens.postDelimiter, rest] = splitSpace(rest);
|
|
107
108
|
}
|
|
108
109
|
if (block === null) {
|
|
109
110
|
num++;
|
|
110
111
|
return null;
|
|
111
112
|
}
|
|
112
|
-
const isClosed = rest.trimRight().endsWith(
|
|
113
|
+
const isClosed = rest.trimRight().endsWith(markers.end);
|
|
113
114
|
if (tokens.delimiter === '' &&
|
|
114
|
-
rest.startsWith(
|
|
115
|
-
!rest.startsWith(
|
|
116
|
-
tokens.delimiter =
|
|
117
|
-
rest = rest.slice(
|
|
115
|
+
rest.startsWith(markers.delim) &&
|
|
116
|
+
!rest.startsWith(markers.end)) {
|
|
117
|
+
tokens.delimiter = markers.delim;
|
|
118
|
+
rest = rest.slice(markers.delim.length);
|
|
118
119
|
[tokens.postDelimiter, rest] = splitSpace(rest);
|
|
119
120
|
}
|
|
120
121
|
if (isClosed) {
|
|
121
122
|
const trimmed = rest.trimRight();
|
|
122
|
-
tokens.end = rest.slice(trimmed.length -
|
|
123
|
-
rest = trimmed.slice(0, -
|
|
123
|
+
tokens.end = rest.slice(trimmed.length - markers.end.length);
|
|
124
|
+
rest = trimmed.slice(0, -markers.end.length);
|
|
124
125
|
}
|
|
125
126
|
tokens.description = rest;
|
|
126
127
|
block.push({ number: num, source, tokens });
|
|
@@ -148,7 +149,7 @@ var CommentParser = (function (exports) {
|
|
|
148
149
|
}
|
|
149
150
|
|
|
150
151
|
/**
|
|
151
|
-
* Splits the `@prefix` from remaining `Spec.lines[].token.
|
|
152
|
+
* Splits the `@prefix` from remaining `Spec.lines[].token.description` into the `tag` token,
|
|
152
153
|
* and populates `spec.tag`
|
|
153
154
|
*/
|
|
154
155
|
function tagTokenizer() {
|
|
@@ -332,11 +333,12 @@ var CommentParser = (function (exports) {
|
|
|
332
333
|
* Makes no changes to `spec.lines[].tokens` but joins them into `spec.description`
|
|
333
334
|
* following given spacing srtategy
|
|
334
335
|
* @param {Spacing} spacing tells how to handle the whitespace
|
|
336
|
+
* @param {BlockMarkers} markers tells how to handle comment block delimitation
|
|
335
337
|
*/
|
|
336
|
-
function descriptionTokenizer(spacing = 'compact') {
|
|
338
|
+
function descriptionTokenizer(spacing = 'compact', markers = exports.Markers) {
|
|
337
339
|
const join = getJoiner(spacing);
|
|
338
340
|
return (spec) => {
|
|
339
|
-
spec.description = join(spec.source);
|
|
341
|
+
spec.description = join(spec.source, markers);
|
|
340
342
|
return spec;
|
|
341
343
|
};
|
|
342
344
|
}
|
|
@@ -347,7 +349,7 @@ var CommentParser = (function (exports) {
|
|
|
347
349
|
return preserveJoiner;
|
|
348
350
|
return spacing;
|
|
349
351
|
}
|
|
350
|
-
function compactJoiner(lines) {
|
|
352
|
+
function compactJoiner(lines, markers = exports.Markers) {
|
|
351
353
|
return lines
|
|
352
354
|
.map(({ tokens: { description } }) => description.trim())
|
|
353
355
|
.filter((description) => description !== '')
|
|
@@ -356,25 +358,25 @@ var CommentParser = (function (exports) {
|
|
|
356
358
|
const lineNo = (num, { tokens }, i) => tokens.type === '' ? num : i;
|
|
357
359
|
const getDescription = ({ tokens }) => (tokens.delimiter === '' ? tokens.start : tokens.postDelimiter.slice(1)) +
|
|
358
360
|
tokens.description;
|
|
359
|
-
function preserveJoiner(lines) {
|
|
361
|
+
function preserveJoiner(lines, markers = exports.Markers) {
|
|
360
362
|
if (lines.length === 0)
|
|
361
363
|
return '';
|
|
362
364
|
// skip the opening line with no description
|
|
363
365
|
if (lines[0].tokens.description === '' &&
|
|
364
|
-
lines[0].tokens.delimiter ===
|
|
366
|
+
lines[0].tokens.delimiter === markers.start)
|
|
365
367
|
lines = lines.slice(1);
|
|
366
368
|
// skip the closing line with no description
|
|
367
369
|
const lastLine = lines[lines.length - 1];
|
|
368
370
|
if (lastLine !== undefined &&
|
|
369
371
|
lastLine.tokens.description === '' &&
|
|
370
|
-
lastLine.tokens.end.endsWith(
|
|
372
|
+
lastLine.tokens.end.endsWith(markers.end))
|
|
371
373
|
lines = lines.slice(0, -1);
|
|
372
374
|
// description starts at the last line of type definition
|
|
373
375
|
lines = lines.slice(lines.reduce(lineNo, 0));
|
|
374
376
|
return lines.map(getDescription).join('\n');
|
|
375
377
|
}
|
|
376
378
|
|
|
377
|
-
function getParser({ startLine = 0, fence = '```', spacing = 'compact', tokenizers = [
|
|
379
|
+
function getParser({ startLine = 0, fence = '```', spacing = 'compact', markers = exports.Markers, tokenizers = [
|
|
378
380
|
tagTokenizer(),
|
|
379
381
|
typeTokenizer(spacing),
|
|
380
382
|
nameTokenizer(),
|
|
@@ -382,23 +384,20 @@ var CommentParser = (function (exports) {
|
|
|
382
384
|
], } = {}) {
|
|
383
385
|
if (startLine < 0 || startLine % 1 > 0)
|
|
384
386
|
throw new Error('Invalid startLine');
|
|
385
|
-
const parseSource = getParser$2({ startLine });
|
|
387
|
+
const parseSource = getParser$2({ startLine, markers });
|
|
386
388
|
const parseBlock = getParser$3({ fence });
|
|
387
389
|
const parseSpec = getParser$1({ tokenizers });
|
|
388
390
|
const joinDescription = getJoiner(spacing);
|
|
389
|
-
const notEmpty = (line) => line.tokens.description.trim() != '';
|
|
390
391
|
return function (source) {
|
|
391
392
|
const blocks = [];
|
|
392
393
|
for (const line of splitLines(source)) {
|
|
393
394
|
const lines = parseSource(line);
|
|
394
395
|
if (lines === null)
|
|
395
396
|
continue;
|
|
396
|
-
if (lines.find(notEmpty) === undefined)
|
|
397
|
-
continue;
|
|
398
397
|
const sections = parseBlock(lines);
|
|
399
398
|
const specs = sections.slice(1).map(parseSpec);
|
|
400
399
|
blocks.push({
|
|
401
|
-
description: joinDescription(sections[0]),
|
|
400
|
+
description: joinDescription(sections[0], markers),
|
|
402
401
|
tags: specs,
|
|
403
402
|
source: lines,
|
|
404
403
|
problems: specs.reduce((acc, spec) => acc.concat(spec.problems), []),
|
|
@@ -443,14 +442,14 @@ var CommentParser = (function (exports) {
|
|
|
443
442
|
type: 0,
|
|
444
443
|
name: 0,
|
|
445
444
|
};
|
|
446
|
-
const getWidth = (w, { tokens: t }) => ({
|
|
447
|
-
start: t.delimiter ===
|
|
445
|
+
const getWidth = (markers = exports.Markers) => (w, { tokens: t }) => ({
|
|
446
|
+
start: t.delimiter === markers.start ? t.start.length : w.start,
|
|
448
447
|
tag: Math.max(w.tag, t.tag.length),
|
|
449
448
|
type: Math.max(w.type, t.type.length),
|
|
450
449
|
name: Math.max(w.name, t.name.length),
|
|
451
450
|
});
|
|
452
451
|
const space = (len) => ''.padStart(len, ' ');
|
|
453
|
-
function align$1() {
|
|
452
|
+
function align$1(markers = exports.Markers) {
|
|
454
453
|
let intoTags = false;
|
|
455
454
|
let w;
|
|
456
455
|
function update(line) {
|
|
@@ -462,15 +461,15 @@ var CommentParser = (function (exports) {
|
|
|
462
461
|
tokens.type === '' &&
|
|
463
462
|
tokens.description === '';
|
|
464
463
|
// dangling '*/'
|
|
465
|
-
if (tokens.end ===
|
|
464
|
+
if (tokens.end === markers.end && isEmpty) {
|
|
466
465
|
tokens.start = space(w.start + 1);
|
|
467
466
|
return Object.assign(Object.assign({}, line), { tokens });
|
|
468
467
|
}
|
|
469
468
|
switch (tokens.delimiter) {
|
|
470
|
-
case
|
|
469
|
+
case markers.start:
|
|
471
470
|
tokens.start = space(w.start);
|
|
472
471
|
break;
|
|
473
|
-
case
|
|
472
|
+
case markers.delim:
|
|
474
473
|
tokens.start = space(w.start + 1);
|
|
475
474
|
break;
|
|
476
475
|
default:
|
|
@@ -513,7 +512,7 @@ var CommentParser = (function (exports) {
|
|
|
513
512
|
}
|
|
514
513
|
return (_a) => {
|
|
515
514
|
var { source } = _a, fields = __rest$2(_a, ["source"]);
|
|
516
|
-
w = source.reduce(getWidth, Object.assign({}, zeroWidth$1));
|
|
515
|
+
w = source.reduce(getWidth(markers), Object.assign({}, zeroWidth$1));
|
|
517
516
|
return rewireSource(Object.assign(Object.assign({}, fields), { source: source.map(update) }));
|
|
518
517
|
};
|
|
519
518
|
}
|
package/es6/parser/index.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { Block } from '../primitives';
|
|
1
|
+
import { Block, BlockMarkers } from '../primitives';
|
|
2
2
|
import { Tokenizer } from './tokenizers/index';
|
|
3
3
|
export interface Options {
|
|
4
4
|
startLine: number;
|
|
5
5
|
fence: string;
|
|
6
6
|
spacing: 'compact' | 'preserve';
|
|
7
|
+
markers: BlockMarkers;
|
|
7
8
|
tokenizers: Tokenizer[];
|
|
8
9
|
}
|
|
9
10
|
export declare type Parser = (source: string) => Block[];
|
|
10
|
-
export default function getParser({ startLine, fence, spacing, tokenizers, }?: Partial<Options>): Parser;
|
|
11
|
+
export default function getParser({ startLine, fence, spacing, markers, tokenizers, }?: Partial<Options>): Parser;
|
package/es6/parser/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Markers } from '../primitives.js';
|
|
1
2
|
import { splitLines } from '../util.js';
|
|
2
3
|
import blockParser from './block-parser.js';
|
|
3
4
|
import sourceParser from './source-parser.js';
|
|
@@ -6,7 +7,7 @@ import tokenizeTag from './tokenizers/tag.js';
|
|
|
6
7
|
import tokenizeType from './tokenizers/type.js';
|
|
7
8
|
import tokenizeName from './tokenizers/name.js';
|
|
8
9
|
import tokenizeDescription, { getJoiner as getDescriptionJoiner, } from './tokenizers/description.js';
|
|
9
|
-
export default function getParser({ startLine = 0, fence = '```', spacing = 'compact', tokenizers = [
|
|
10
|
+
export default function getParser({ startLine = 0, fence = '```', spacing = 'compact', markers = Markers, tokenizers = [
|
|
10
11
|
tokenizeTag(),
|
|
11
12
|
tokenizeType(spacing),
|
|
12
13
|
tokenizeName(),
|
|
@@ -14,23 +15,20 @@ export default function getParser({ startLine = 0, fence = '```', spacing = 'com
|
|
|
14
15
|
], } = {}) {
|
|
15
16
|
if (startLine < 0 || startLine % 1 > 0)
|
|
16
17
|
throw new Error('Invalid startLine');
|
|
17
|
-
const parseSource = sourceParser({ startLine });
|
|
18
|
+
const parseSource = sourceParser({ startLine, markers });
|
|
18
19
|
const parseBlock = blockParser({ fence });
|
|
19
20
|
const parseSpec = specParser({ tokenizers });
|
|
20
21
|
const joinDescription = getDescriptionJoiner(spacing);
|
|
21
|
-
const notEmpty = (line) => line.tokens.description.trim() != '';
|
|
22
22
|
return function (source) {
|
|
23
23
|
const blocks = [];
|
|
24
24
|
for (const line of splitLines(source)) {
|
|
25
25
|
const lines = parseSource(line);
|
|
26
26
|
if (lines === null)
|
|
27
27
|
continue;
|
|
28
|
-
if (lines.find(notEmpty) === undefined)
|
|
29
|
-
continue;
|
|
30
28
|
const sections = parseBlock(lines);
|
|
31
29
|
const specs = sections.slice(1).map(parseSpec);
|
|
32
30
|
blocks.push({
|
|
33
|
-
description: joinDescription(sections[0]),
|
|
31
|
+
description: joinDescription(sections[0], markers),
|
|
34
32
|
tags: specs,
|
|
35
33
|
source: lines,
|
|
36
34
|
problems: specs.reduce((acc, spec) => acc.concat(spec.problems), []),
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { Line } from '../primitives';
|
|
1
|
+
import { Line, BlockMarkers } from '../primitives';
|
|
2
2
|
export interface Options {
|
|
3
3
|
startLine: number;
|
|
4
|
+
markers: BlockMarkers;
|
|
4
5
|
}
|
|
5
6
|
export declare type Parser = (source: string) => Line[] | null;
|
|
6
|
-
export default function getParser({ startLine, }?: Partial<Options>): Parser;
|
|
7
|
+
export default function getParser({ startLine, markers, }?: Partial<Options>): Parser;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Markers } from '../primitives.js';
|
|
2
2
|
import { seedTokens, splitSpace, splitCR } from '../util.js';
|
|
3
|
-
export default function getParser({ startLine = 0, } = {}) {
|
|
3
|
+
export default function getParser({ startLine = 0, markers = Markers, } = {}) {
|
|
4
4
|
let block = null;
|
|
5
5
|
let num = startLine;
|
|
6
6
|
return function parseSource(source) {
|
|
@@ -9,29 +9,29 @@ export default function getParser({ startLine = 0, } = {}) {
|
|
|
9
9
|
[tokens.lineEnd, rest] = splitCR(rest);
|
|
10
10
|
[tokens.start, rest] = splitSpace(rest);
|
|
11
11
|
if (block === null &&
|
|
12
|
-
rest.startsWith(
|
|
13
|
-
!rest.startsWith(
|
|
12
|
+
rest.startsWith(markers.start) &&
|
|
13
|
+
!rest.startsWith(markers.nostart)) {
|
|
14
14
|
block = [];
|
|
15
|
-
tokens.delimiter = rest.slice(0,
|
|
16
|
-
rest = rest.slice(
|
|
15
|
+
tokens.delimiter = rest.slice(0, markers.start.length);
|
|
16
|
+
rest = rest.slice(markers.start.length);
|
|
17
17
|
[tokens.postDelimiter, rest] = splitSpace(rest);
|
|
18
18
|
}
|
|
19
19
|
if (block === null) {
|
|
20
20
|
num++;
|
|
21
21
|
return null;
|
|
22
22
|
}
|
|
23
|
-
const isClosed = rest.trimRight().endsWith(
|
|
23
|
+
const isClosed = rest.trimRight().endsWith(markers.end);
|
|
24
24
|
if (tokens.delimiter === '' &&
|
|
25
|
-
rest.startsWith(
|
|
26
|
-
!rest.startsWith(
|
|
27
|
-
tokens.delimiter =
|
|
28
|
-
rest = rest.slice(
|
|
25
|
+
rest.startsWith(markers.delim) &&
|
|
26
|
+
!rest.startsWith(markers.end)) {
|
|
27
|
+
tokens.delimiter = markers.delim;
|
|
28
|
+
rest = rest.slice(markers.delim.length);
|
|
29
29
|
[tokens.postDelimiter, rest] = splitSpace(rest);
|
|
30
30
|
}
|
|
31
31
|
if (isClosed) {
|
|
32
32
|
const trimmed = rest.trimRight();
|
|
33
|
-
tokens.end = rest.slice(trimmed.length -
|
|
34
|
-
rest = trimmed.slice(0, -
|
|
33
|
+
tokens.end = rest.slice(trimmed.length - markers.end.length);
|
|
34
|
+
rest = trimmed.slice(0, -markers.end.length);
|
|
35
35
|
}
|
|
36
36
|
tokens.description = rest;
|
|
37
37
|
block.push({ number: num, source, tokens });
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Line } from '../../primitives';
|
|
1
|
+
import { Line, BlockMarkers, Markers } from '../../primitives';
|
|
2
2
|
import { Tokenizer } from './index';
|
|
3
3
|
/**
|
|
4
4
|
* Walks over provided lines joining description token into a single string.
|
|
5
5
|
* */
|
|
6
|
-
export declare type Joiner = (lines: Line[]) => string;
|
|
6
|
+
export declare type Joiner = (lines: Line[], markers?: BlockMarkers) => string;
|
|
7
7
|
/**
|
|
8
8
|
* Shortcut for standard Joiners
|
|
9
9
|
* compact - strip surrounding whitespace and concat lines using a single string
|
|
@@ -14,6 +14,7 @@ export declare type Spacing = 'compact' | 'preserve' | Joiner;
|
|
|
14
14
|
* Makes no changes to `spec.lines[].tokens` but joins them into `spec.description`
|
|
15
15
|
* following given spacing srtategy
|
|
16
16
|
* @param {Spacing} spacing tells how to handle the whitespace
|
|
17
|
+
* @param {BlockMarkers} markers tells how to handle comment block delimitation
|
|
17
18
|
*/
|
|
18
|
-
export default function descriptionTokenizer(spacing?: Spacing): Tokenizer;
|
|
19
|
+
export default function descriptionTokenizer(spacing?: Spacing, markers?: typeof Markers): Tokenizer;
|
|
19
20
|
export declare function getJoiner(spacing: Spacing): Joiner;
|
|
@@ -3,11 +3,12 @@ import { Markers } from '../../primitives.js';
|
|
|
3
3
|
* Makes no changes to `spec.lines[].tokens` but joins them into `spec.description`
|
|
4
4
|
* following given spacing srtategy
|
|
5
5
|
* @param {Spacing} spacing tells how to handle the whitespace
|
|
6
|
+
* @param {BlockMarkers} markers tells how to handle comment block delimitation
|
|
6
7
|
*/
|
|
7
|
-
export default function descriptionTokenizer(spacing = 'compact') {
|
|
8
|
+
export default function descriptionTokenizer(spacing = 'compact', markers = Markers) {
|
|
8
9
|
const join = getJoiner(spacing);
|
|
9
10
|
return (spec) => {
|
|
10
|
-
spec.description = join(spec.source);
|
|
11
|
+
spec.description = join(spec.source, markers);
|
|
11
12
|
return spec;
|
|
12
13
|
};
|
|
13
14
|
}
|
|
@@ -18,7 +19,7 @@ export function getJoiner(spacing) {
|
|
|
18
19
|
return preserveJoiner;
|
|
19
20
|
return spacing;
|
|
20
21
|
}
|
|
21
|
-
function compactJoiner(lines) {
|
|
22
|
+
function compactJoiner(lines, markers = Markers) {
|
|
22
23
|
return lines
|
|
23
24
|
.map(({ tokens: { description } }) => description.trim())
|
|
24
25
|
.filter((description) => description !== '')
|
|
@@ -27,18 +28,18 @@ function compactJoiner(lines) {
|
|
|
27
28
|
const lineNo = (num, { tokens }, i) => tokens.type === '' ? num : i;
|
|
28
29
|
const getDescription = ({ tokens }) => (tokens.delimiter === '' ? tokens.start : tokens.postDelimiter.slice(1)) +
|
|
29
30
|
tokens.description;
|
|
30
|
-
function preserveJoiner(lines) {
|
|
31
|
+
function preserveJoiner(lines, markers = Markers) {
|
|
31
32
|
if (lines.length === 0)
|
|
32
33
|
return '';
|
|
33
34
|
// skip the opening line with no description
|
|
34
35
|
if (lines[0].tokens.description === '' &&
|
|
35
|
-
lines[0].tokens.delimiter ===
|
|
36
|
+
lines[0].tokens.delimiter === markers.start)
|
|
36
37
|
lines = lines.slice(1);
|
|
37
38
|
// skip the closing line with no description
|
|
38
39
|
const lastLine = lines[lines.length - 1];
|
|
39
40
|
if (lastLine !== undefined &&
|
|
40
41
|
lastLine.tokens.description === '' &&
|
|
41
|
-
lastLine.tokens.end.endsWith(
|
|
42
|
+
lastLine.tokens.end.endsWith(markers.end))
|
|
42
43
|
lines = lines.slice(0, -1);
|
|
43
44
|
// description starts at the last line of type definition
|
|
44
45
|
lines = lines.slice(lines.reduce(lineNo, 0));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Tokenizer } from './index';
|
|
2
2
|
/**
|
|
3
|
-
* Splits the `@prefix` from remaining `Spec.lines[].token.
|
|
3
|
+
* Splits the `@prefix` from remaining `Spec.lines[].token.description` into the `tag` token,
|
|
4
4
|
* and populates `spec.tag`
|
|
5
5
|
*/
|
|
6
6
|
export default function tagTokenizer(): Tokenizer;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Splits the `@prefix` from remaining `Spec.lines[].token.
|
|
2
|
+
* Splits the `@prefix` from remaining `Spec.lines[].token.description` into the `tag` token,
|
|
3
3
|
* and populates `spec.tag`
|
|
4
4
|
*/
|
|
5
5
|
export default function tagTokenizer() {
|
package/es6/primitives.d.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
|
+
/** @deprecated */
|
|
1
2
|
export declare enum Markers {
|
|
2
3
|
start = "/**",
|
|
3
4
|
nostart = "/***",
|
|
4
5
|
delim = "*",
|
|
5
6
|
end = "*/"
|
|
6
7
|
}
|
|
8
|
+
export interface BlockMarkers {
|
|
9
|
+
start: string;
|
|
10
|
+
nostart: string;
|
|
11
|
+
delim: string;
|
|
12
|
+
end: string;
|
|
13
|
+
}
|
|
7
14
|
export interface Block {
|
|
8
15
|
description: string;
|
|
9
16
|
tags: Spec[];
|
package/es6/primitives.js
CHANGED
package/es6/transforms/align.js
CHANGED
|
@@ -17,14 +17,14 @@ const zeroWidth = {
|
|
|
17
17
|
type: 0,
|
|
18
18
|
name: 0,
|
|
19
19
|
};
|
|
20
|
-
const getWidth = (w, { tokens: t }) => ({
|
|
21
|
-
start: t.delimiter ===
|
|
20
|
+
const getWidth = (markers = Markers) => (w, { tokens: t }) => ({
|
|
21
|
+
start: t.delimiter === markers.start ? t.start.length : w.start,
|
|
22
22
|
tag: Math.max(w.tag, t.tag.length),
|
|
23
23
|
type: Math.max(w.type, t.type.length),
|
|
24
24
|
name: Math.max(w.name, t.name.length),
|
|
25
25
|
});
|
|
26
26
|
const space = (len) => ''.padStart(len, ' ');
|
|
27
|
-
export default function align() {
|
|
27
|
+
export default function align(markers = Markers) {
|
|
28
28
|
let intoTags = false;
|
|
29
29
|
let w;
|
|
30
30
|
function update(line) {
|
|
@@ -36,15 +36,15 @@ export default function align() {
|
|
|
36
36
|
tokens.type === '' &&
|
|
37
37
|
tokens.description === '';
|
|
38
38
|
// dangling '*/'
|
|
39
|
-
if (tokens.end ===
|
|
39
|
+
if (tokens.end === markers.end && isEmpty) {
|
|
40
40
|
tokens.start = space(w.start + 1);
|
|
41
41
|
return Object.assign(Object.assign({}, line), { tokens });
|
|
42
42
|
}
|
|
43
43
|
switch (tokens.delimiter) {
|
|
44
|
-
case
|
|
44
|
+
case markers.start:
|
|
45
45
|
tokens.start = space(w.start);
|
|
46
46
|
break;
|
|
47
|
-
case
|
|
47
|
+
case markers.delim:
|
|
48
48
|
tokens.start = space(w.start + 1);
|
|
49
49
|
break;
|
|
50
50
|
default:
|
|
@@ -87,7 +87,7 @@ export default function align() {
|
|
|
87
87
|
}
|
|
88
88
|
return (_a) => {
|
|
89
89
|
var { source } = _a, fields = __rest(_a, ["source"]);
|
|
90
|
-
w = source.reduce(getWidth, Object.assign({}, zeroWidth));
|
|
90
|
+
w = source.reduce(getWidth(markers), Object.assign({}, zeroWidth));
|
|
91
91
|
return rewireSource(Object.assign(Object.assign({}, fields), { source: source.map(update) }));
|
|
92
92
|
};
|
|
93
93
|
}
|
package/lib/parser/index.cjs
CHANGED
|
@@ -4,6 +4,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
|
|
7
|
+
const primitives_1 = require("../primitives.cjs");
|
|
8
|
+
|
|
7
9
|
const util_1 = require("../util.cjs");
|
|
8
10
|
|
|
9
11
|
const block_parser_1 = require("./block-parser.cjs");
|
|
@@ -24,11 +26,13 @@ function getParser({
|
|
|
24
26
|
startLine = 0,
|
|
25
27
|
fence = '```',
|
|
26
28
|
spacing = 'compact',
|
|
29
|
+
markers = primitives_1.Markers,
|
|
27
30
|
tokenizers = [tag_1.default(), type_1.default(spacing), name_1.default(), description_1.default(spacing)]
|
|
28
31
|
} = {}) {
|
|
29
32
|
if (startLine < 0 || startLine % 1 > 0) throw new Error('Invalid startLine');
|
|
30
33
|
const parseSource = source_parser_1.default({
|
|
31
|
-
startLine
|
|
34
|
+
startLine,
|
|
35
|
+
markers
|
|
32
36
|
});
|
|
33
37
|
const parseBlock = block_parser_1.default({
|
|
34
38
|
fence
|
|
@@ -37,20 +41,16 @@ function getParser({
|
|
|
37
41
|
tokenizers
|
|
38
42
|
});
|
|
39
43
|
const joinDescription = description_1.getJoiner(spacing);
|
|
40
|
-
|
|
41
|
-
const notEmpty = line => line.tokens.description.trim() != '';
|
|
42
|
-
|
|
43
44
|
return function (source) {
|
|
44
45
|
const blocks = [];
|
|
45
46
|
|
|
46
47
|
for (const line of util_1.splitLines(source)) {
|
|
47
48
|
const lines = parseSource(line);
|
|
48
49
|
if (lines === null) continue;
|
|
49
|
-
if (lines.find(notEmpty) === undefined) continue;
|
|
50
50
|
const sections = parseBlock(lines);
|
|
51
51
|
const specs = sections.slice(1).map(parseSpec);
|
|
52
52
|
blocks.push({
|
|
53
|
-
description: joinDescription(sections[0]),
|
|
53
|
+
description: joinDescription(sections[0], markers),
|
|
54
54
|
tags: specs,
|
|
55
55
|
source: lines,
|
|
56
56
|
problems: specs.reduce((acc, spec) => acc.concat(spec.problems), [])
|
package/lib/parser/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["index.js"],"names":["Object","defineProperty","exports","value","
|
|
1
|
+
{"version":3,"sources":["index.js"],"names":["Object","defineProperty","exports","value","primitives_1","require","util_1","block_parser_1","source_parser_1","spec_parser_1","tag_1","type_1","name_1","description_1","getParser","startLine","fence","spacing","markers","Markers","tokenizers","default","Error","parseSource","parseBlock","parseSpec","joinDescription","getJoiner","source","blocks","line","splitLines","lines","sections","specs","slice","map","push","description","tags","problems","reduce","acc","spec","concat"],"mappings":"AAAA;;AACAA,MAAM,CAACC,cAAP,CAAsBC,OAAtB,EAA+B,YAA/B,EAA6C;AAAEC,EAAAA,KAAK,EAAE;AAAT,CAA7C;;AACA,MAAMC,YAAY,GAAGC,OAAH,qBAAlB;;AACA,MAAMC,MAAM,GAAGD,OAAH,eAAZ;;AACA,MAAME,cAAc,GAAGF,OAAH,sBAApB;;AACA,MAAMG,eAAe,GAAGH,OAAH,uBAArB;;AACA,MAAMI,aAAa,GAAGJ,OAAH,qBAAnB;;AACA,MAAMK,KAAK,GAAGL,OAAH,wBAAX;;AACA,MAAMM,MAAM,GAAGN,OAAH,yBAAZ;;AACA,MAAMO,MAAM,GAAGP,OAAH,yBAAZ;;AACA,MAAMQ,aAAa,GAAGR,OAAH,gCAAnB;;AACA,SAASS,SAAT,CAAmB;AAAEC,EAAAA,SAAS,GAAG,CAAd;AAAiBC,EAAAA,KAAK,GAAG,KAAzB;AAAgCC,EAAAA,OAAO,GAAG,SAA1C;AAAqDC,EAAAA,OAAO,GAAGd,YAAY,CAACe,OAA5E;AAAqFC,EAAAA,UAAU,GAAG,CACjHV,KAAK,CAACW,OAAN,EADiH,EAEjHV,MAAM,CAACU,OAAP,CAAeJ,OAAf,CAFiH,EAGjHL,MAAM,CAACS,OAAP,EAHiH,EAIjHR,aAAa,CAACQ,OAAd,CAAsBJ,OAAtB,CAJiH;AAAlG,IAKZ,EALP,EAKW;AACP,MAAIF,SAAS,GAAG,CAAZ,IAAiBA,SAAS,GAAG,CAAZ,GAAgB,CAArC,EACI,MAAM,IAAIO,KAAJ,CAAU,mBAAV,CAAN;AACJ,QAAMC,WAAW,GAAGf,eAAe,CAACa,OAAhB,CAAwB;AAAEN,IAAAA,SAAF;AAAaG,IAAAA;AAAb,GAAxB,CAApB;AACA,QAAMM,UAAU,GAAGjB,cAAc,CAACc,OAAf,CAAuB;AAAEL,IAAAA;AAAF,GAAvB,CAAnB;AACA,QAAMS,SAAS,GAAGhB,aAAa,CAACY,OAAd,CAAsB;AAAED,IAAAA;AAAF,GAAtB,CAAlB;AACA,QAAMM,eAAe,GAAGb,aAAa,CAACc,SAAd,CAAwBV,OAAxB,CAAxB;AACA,SAAO,UAAUW,MAAV,EAAkB;AACrB,UAAMC,MAAM,GAAG,EAAf;;AACA,SAAK,MAAMC,IAAX,IAAmBxB,MAAM,CAACyB,UAAP,CAAkBH,MAAlB,CAAnB,EAA8C;AAC1C,YAAMI,KAAK,GAAGT,WAAW,CAACO,IAAD,CAAzB;AACA,UAAIE,KAAK,KAAK,IAAd,EACI;AACJ,YAAMC,QAAQ,GAAGT,UAAU,CAACQ,KAAD,CAA3B;AACA,YAAME,KAAK,GAAGD,QAAQ,CAACE,KAAT,CAAe,CAAf,EAAkBC,GAAlB,CAAsBX,SAAtB,CAAd;AACAI,MAAAA,MAAM,CAACQ,IAAP,CAAY;AACRC,QAAAA,WAAW,EAAEZ,eAAe,CAACO,QAAQ,CAAC,CAAD,CAAT,EAAcf,OAAd,CADpB;AAERqB,QAAAA,IAAI,EAAEL,KAFE;AAGRN,QAAAA,MAAM,EAAEI,KAHA;AAIRQ,QAAAA,QAAQ,EAAEN,KAAK,CAACO,MAAN,CAAa,CAACC,GAAD,EAAMC,IAAN,KAAeD,GAAG,CAACE,MAAJ,CAAWD,IAAI,CAACH,QAAhB,CAA5B,EAAuD,EAAvD;AAJF,OAAZ;AAMH;;AACD,WAAOX,MAAP;AACH,GAhBD;AAiBH;;AACD3B,OAAO,CAACmB,OAAR,GAAkBP,SAAlB","sourcesContent":["\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nconst primitives_1 = require(\"../primitives\");\nconst util_1 = require(\"../util\");\nconst block_parser_1 = require(\"./block-parser\");\nconst source_parser_1 = require(\"./source-parser\");\nconst spec_parser_1 = require(\"./spec-parser\");\nconst tag_1 = require(\"./tokenizers/tag\");\nconst type_1 = require(\"./tokenizers/type\");\nconst name_1 = require(\"./tokenizers/name\");\nconst description_1 = require(\"./tokenizers/description\");\nfunction getParser({ startLine = 0, fence = '```', spacing = 'compact', markers = primitives_1.Markers, tokenizers = [\n tag_1.default(),\n type_1.default(spacing),\n name_1.default(),\n description_1.default(spacing),\n], } = {}) {\n if (startLine < 0 || startLine % 1 > 0)\n throw new Error('Invalid startLine');\n const parseSource = source_parser_1.default({ startLine, markers });\n const parseBlock = block_parser_1.default({ fence });\n const parseSpec = spec_parser_1.default({ tokenizers });\n const joinDescription = description_1.getJoiner(spacing);\n return function (source) {\n const blocks = [];\n for (const line of util_1.splitLines(source)) {\n const lines = parseSource(line);\n if (lines === null)\n continue;\n const sections = parseBlock(lines);\n const specs = sections.slice(1).map(parseSpec);\n blocks.push({\n description: joinDescription(sections[0], markers),\n tags: specs,\n source: lines,\n problems: specs.reduce((acc, spec) => acc.concat(spec.problems), []),\n });\n }\n return blocks;\n };\n}\nexports.default = getParser;\n"],"file":"index.cjs"}
|
package/lib/parser/index.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { Block } from '../primitives';
|
|
1
|
+
import { Block, BlockMarkers } from '../primitives';
|
|
2
2
|
import { Tokenizer } from './tokenizers/index';
|
|
3
3
|
export interface Options {
|
|
4
4
|
startLine: number;
|
|
5
5
|
fence: string;
|
|
6
6
|
spacing: 'compact' | 'preserve';
|
|
7
|
+
markers: BlockMarkers;
|
|
7
8
|
tokenizers: Tokenizer[];
|
|
8
9
|
}
|
|
9
10
|
export declare type Parser = (source: string) => Block[];
|
|
10
|
-
export default function getParser({ startLine, fence, spacing, tokenizers, }?: Partial<Options>): Parser;
|
|
11
|
+
export default function getParser({ startLine, fence, spacing, markers, tokenizers, }?: Partial<Options>): Parser;
|
|
@@ -9,7 +9,8 @@ const primitives_1 = require("../primitives.cjs");
|
|
|
9
9
|
const util_1 = require("../util.cjs");
|
|
10
10
|
|
|
11
11
|
function getParser({
|
|
12
|
-
startLine = 0
|
|
12
|
+
startLine = 0,
|
|
13
|
+
markers = primitives_1.Markers
|
|
13
14
|
} = {}) {
|
|
14
15
|
let block = null;
|
|
15
16
|
let num = startLine;
|
|
@@ -19,10 +20,10 @@ function getParser({
|
|
|
19
20
|
[tokens.lineEnd, rest] = util_1.splitCR(rest);
|
|
20
21
|
[tokens.start, rest] = util_1.splitSpace(rest);
|
|
21
22
|
|
|
22
|
-
if (block === null && rest.startsWith(
|
|
23
|
+
if (block === null && rest.startsWith(markers.start) && !rest.startsWith(markers.nostart)) {
|
|
23
24
|
block = [];
|
|
24
|
-
tokens.delimiter = rest.slice(0,
|
|
25
|
-
rest = rest.slice(
|
|
25
|
+
tokens.delimiter = rest.slice(0, markers.start.length);
|
|
26
|
+
rest = rest.slice(markers.start.length);
|
|
26
27
|
[tokens.postDelimiter, rest] = util_1.splitSpace(rest);
|
|
27
28
|
}
|
|
28
29
|
|
|
@@ -31,18 +32,18 @@ function getParser({
|
|
|
31
32
|
return null;
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
const isClosed = rest.trimRight().endsWith(
|
|
35
|
+
const isClosed = rest.trimRight().endsWith(markers.end);
|
|
35
36
|
|
|
36
|
-
if (tokens.delimiter === '' && rest.startsWith(
|
|
37
|
-
tokens.delimiter =
|
|
38
|
-
rest = rest.slice(
|
|
37
|
+
if (tokens.delimiter === '' && rest.startsWith(markers.delim) && !rest.startsWith(markers.end)) {
|
|
38
|
+
tokens.delimiter = markers.delim;
|
|
39
|
+
rest = rest.slice(markers.delim.length);
|
|
39
40
|
[tokens.postDelimiter, rest] = util_1.splitSpace(rest);
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
if (isClosed) {
|
|
43
44
|
const trimmed = rest.trimRight();
|
|
44
|
-
tokens.end = rest.slice(trimmed.length -
|
|
45
|
-
rest = trimmed.slice(0, -
|
|
45
|
+
tokens.end = rest.slice(trimmed.length - markers.end.length);
|
|
46
|
+
rest = trimmed.slice(0, -markers.end.length);
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
tokens.description = rest;
|