comment-parser 1.2.4 → 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.
- package/CHANGELOG.md +3 -0
- package/browser/index.js +38 -36
- package/es6/parser/index.d.ts +3 -2
- package/es6/parser/index.js +4 -3
- 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/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 -2
- 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/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 +4 -3
- package/src/parser/index.ts +6 -3
- package/src/parser/source-parser.ts +14 -12
- package/src/parser/tokenizers/description.ts +11 -8
- package/src/primitives.ts +8 -0
- package/src/transforms/align.ts +14 -12
- package/tests/e2e/examples.js +1 -1
- package/tests/unit/source-parser.spec.ts +150 -0
package/CHANGELOG.md
CHANGED
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 });
|
|
@@ -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,7 +384,7 @@ 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);
|
|
@@ -398,7 +400,7 @@ var CommentParser = (function (exports) {
|
|
|
398
400
|
const sections = parseBlock(lines);
|
|
399
401
|
const specs = sections.slice(1).map(parseSpec);
|
|
400
402
|
blocks.push({
|
|
401
|
-
description: joinDescription(sections[0]),
|
|
403
|
+
description: joinDescription(sections[0], markers),
|
|
402
404
|
tags: specs,
|
|
403
405
|
source: lines,
|
|
404
406
|
problems: specs.reduce((acc, spec) => acc.concat(spec.problems), []),
|
|
@@ -443,14 +445,14 @@ var CommentParser = (function (exports) {
|
|
|
443
445
|
type: 0,
|
|
444
446
|
name: 0,
|
|
445
447
|
};
|
|
446
|
-
const getWidth = (w, { tokens: t }) => ({
|
|
447
|
-
start: t.delimiter ===
|
|
448
|
+
const getWidth = (markers = exports.Markers) => (w, { tokens: t }) => ({
|
|
449
|
+
start: t.delimiter === markers.start ? t.start.length : w.start,
|
|
448
450
|
tag: Math.max(w.tag, t.tag.length),
|
|
449
451
|
type: Math.max(w.type, t.type.length),
|
|
450
452
|
name: Math.max(w.name, t.name.length),
|
|
451
453
|
});
|
|
452
454
|
const space = (len) => ''.padStart(len, ' ');
|
|
453
|
-
function align$1() {
|
|
455
|
+
function align$1(markers = exports.Markers) {
|
|
454
456
|
let intoTags = false;
|
|
455
457
|
let w;
|
|
456
458
|
function update(line) {
|
|
@@ -462,15 +464,15 @@ var CommentParser = (function (exports) {
|
|
|
462
464
|
tokens.type === '' &&
|
|
463
465
|
tokens.description === '';
|
|
464
466
|
// dangling '*/'
|
|
465
|
-
if (tokens.end ===
|
|
467
|
+
if (tokens.end === markers.end && isEmpty) {
|
|
466
468
|
tokens.start = space(w.start + 1);
|
|
467
469
|
return Object.assign(Object.assign({}, line), { tokens });
|
|
468
470
|
}
|
|
469
471
|
switch (tokens.delimiter) {
|
|
470
|
-
case
|
|
472
|
+
case markers.start:
|
|
471
473
|
tokens.start = space(w.start);
|
|
472
474
|
break;
|
|
473
|
-
case
|
|
475
|
+
case markers.delim:
|
|
474
476
|
tokens.start = space(w.start + 1);
|
|
475
477
|
break;
|
|
476
478
|
default:
|
|
@@ -513,7 +515,7 @@ var CommentParser = (function (exports) {
|
|
|
513
515
|
}
|
|
514
516
|
return (_a) => {
|
|
515
517
|
var { source } = _a, fields = __rest$2(_a, ["source"]);
|
|
516
|
-
w = source.reduce(getWidth, Object.assign({}, zeroWidth$1));
|
|
518
|
+
w = source.reduce(getWidth(markers), Object.assign({}, zeroWidth$1));
|
|
517
519
|
return rewireSource(Object.assign(Object.assign({}, fields), { source: source.map(update) }));
|
|
518
520
|
};
|
|
519
521
|
}
|
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,7 +15,7 @@ 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);
|
|
@@ -30,7 +31,7 @@ export default function getParser({ startLine = 0, fence = '```', spacing = 'com
|
|
|
30
31
|
const sections = parseBlock(lines);
|
|
31
32
|
const specs = sections.slice(1).map(parseSpec);
|
|
32
33
|
blocks.push({
|
|
33
|
-
description: joinDescription(sections[0]),
|
|
34
|
+
description: joinDescription(sections[0], markers),
|
|
34
35
|
tags: specs,
|
|
35
36
|
source: lines,
|
|
36
37
|
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));
|
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
|
|
@@ -50,7 +54,7 @@ function getParser({
|
|
|
50
54
|
const sections = parseBlock(lines);
|
|
51
55
|
const specs = sections.slice(1).map(parseSpec);
|
|
52
56
|
blocks.push({
|
|
53
|
-
description: joinDescription(sections[0]),
|
|
57
|
+
description: joinDescription(sections[0], markers),
|
|
54
58
|
tags: specs,
|
|
55
59
|
source: lines,
|
|
56
60
|
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","notEmpty","line","tokens","description","trim","source","blocks","splitLines","lines","find","undefined","sections","specs","slice","map","push","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,E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AA,MAAM,CAACS,IAAP,CAAY;AACRZ,QAAAA,WAAW,EAAEL,eAAe,CAACa,QAAQ,CAAC,CAAD,CAAT,EAAcrB,OAAd,CADpB;AAER0B,QAAAA,IAAI,EAAEJ,KAFE;AAGRP,QAAAA,MAAM,EAAEG,KAHA;AAIRS,QAAAA,QAAQ,EAAEL,KAAK,CAACM,MAAN,CAAa,CAACC,GAAD,EAAMC,IAAN,KAAeD,GAAG,CAACE,MAAJ,CAAWD,IAAI,CAACH,QAAhB,CAA5B,EAAuD,EAAvD;AAJF,OAAZ;AAMH;;AACD,WAAOX,MAAP;AACH,GAlBD;AAmBH;;AACDhC,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 const notEmpty = (line) => line.tokens.description.trim() != '';\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 if (lines.find(notEmpty) === undefined)\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;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["source-parser.js"],"names":["Object","defineProperty","exports","value","primitives_1","require","util_1","getParser","startLine","block","num","parseSource","source","rest","tokens","seedTokens","lineEnd","splitCR","start","splitSpace","startsWith","
|
|
1
|
+
{"version":3,"sources":["source-parser.js"],"names":["Object","defineProperty","exports","value","primitives_1","require","util_1","getParser","startLine","markers","Markers","block","num","parseSource","source","rest","tokens","seedTokens","lineEnd","splitCR","start","splitSpace","startsWith","nostart","delimiter","slice","length","postDelimiter","isClosed","trimRight","endsWith","end","delim","trimmed","description","push","number","result","default"],"mappings":"AAAA;;AACAA,MAAM,CAACC,cAAP,CAAsBC,OAAtB,EAA+B,YAA/B,EAA6C;AAAEC,EAAAA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sourcesContent":["\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nconst primitives_1 = require(\"../primitives\");\nconst util_1 = require(\"../util\");\nfunction getParser({ startLine = 0, markers = primitives_1.Markers, } = {}) {\n let block = null;\n let num = startLine;\n return function parseSource(source) {\n let rest = source;\n const tokens = util_1.seedTokens();\n [tokens.lineEnd, rest] = util_1.splitCR(rest);\n [tokens.start, rest] = util_1.splitSpace(rest);\n if (block === null &&\n rest.startsWith(markers.start) &&\n !rest.startsWith(markers.nostart)) {\n block = [];\n tokens.delimiter = rest.slice(0, markers.start.length);\n rest = rest.slice(markers.start.length);\n [tokens.postDelimiter, rest] = util_1.splitSpace(rest);\n }\n if (block === null) {\n num++;\n return null;\n }\n const isClosed = rest.trimRight().endsWith(markers.end);\n if (tokens.delimiter === '' &&\n rest.startsWith(markers.delim) &&\n !rest.startsWith(markers.end)) {\n tokens.delimiter = markers.delim;\n rest = rest.slice(markers.delim.length);\n [tokens.postDelimiter, rest] = util_1.splitSpace(rest);\n }\n if (isClosed) {\n const trimmed = rest.trimRight();\n tokens.end = rest.slice(trimmed.length - markers.end.length);\n rest = trimmed.slice(0, -markers.end.length);\n }\n tokens.description = rest;\n block.push({ number: num, source, tokens });\n num++;\n if (isClosed) {\n const result = block.slice();\n block = null;\n return result;\n }\n return null;\n };\n}\nexports.default = getParser;\n"],"file":"source-parser.cjs"}
|
|
@@ -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;
|