comment-parser 1.1.2 → 1.1.6-beta.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 +21 -3
- package/browser/index.js +62 -20
- package/es6/index.d.ts +3 -0
- package/es6/index.js +3 -0
- package/es6/transforms/crlf.d.ts +3 -0
- package/es6/transforms/crlf.js +42 -0
- package/es6/transforms/index.d.ts +1 -1
- package/es6/util.js +1 -1
- package/lib/index.d.ts +3 -0
- package/lib/index.js +13 -0
- package/lib/transforms/crlf.d.ts +3 -0
- package/lib/transforms/crlf.js +45 -0
- package/lib/transforms/index.d.ts +1 -1
- package/lib/util.js +1 -1
- package/package.json +5 -4
- package/src/index.ts +4 -0
- package/src/parser/tokenizers/name.ts +1 -1
- package/src/parser/tokenizers/type.ts +1 -1
- package/src/stringifier/inspect.ts +2 -2
- package/src/transforms/align.ts +1 -1
- package/src/transforms/crlf.ts +38 -0
- package/src/transforms/indent.ts +1 -1
- package/src/transforms/index.ts +1 -1
- package/src/util.ts +1 -1
- package/tests/unit/source-parser.spec.ts +49 -1
- package/tests/unit/transforms-crlf.spec.ts +70 -0
- package/tests/unit/util.spec.ts +2 -2
- package/{tsconfig.es6.json → tsconfig.json} +1 -1
- package/tsconfig.node.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
# v1.1.6-beta.0
|
|
2
|
+
- keep original CR line endings
|
|
3
|
+
- allow to normalize line endings with `crlf` transform
|
|
4
|
+
|
|
5
|
+
# v1.1.5
|
|
6
|
+
- drop unused variables
|
|
7
|
+
- add .editorconfig
|
|
8
|
+
|
|
9
|
+
# v1.1.4
|
|
10
|
+
- `bugfix` fix unsynced lib/
|
|
11
|
+
|
|
12
|
+
# v1.1.3
|
|
13
|
+
- export primitive type on the top level: Markers, Block, Spec, Line, Tokens, Problem
|
|
14
|
+
|
|
15
|
+
# v1.1.2
|
|
16
|
+
- `bugfix` Allow to build nested tags from `name.subname` even if `name` wasn't d
|
|
17
|
+
- `bugfix` Preserve indentation when extracting comments
|
|
18
|
+
|
|
19
|
+
# v1.1.1
|
|
20
|
+
- add helpers for rewiring Spec.source <-> Spec.tags.source
|
|
21
|
+
|
|
1
22
|
# v1.1.0
|
|
2
23
|
- split tokenizers into separate modules
|
|
3
24
|
- allow multiline {type} definitions - issue #109
|
|
@@ -5,9 +26,6 @@
|
|
|
5
26
|
- allow using "=" in quoted [name=default] defaults – issue #112
|
|
6
27
|
- add tokenizers usage example - issue #111
|
|
7
28
|
|
|
8
|
-
# v1.1.1
|
|
9
|
-
- add helpers for rewiring Spec.source <-> Spec.tags.source
|
|
10
|
-
|
|
11
29
|
# v1.0.0
|
|
12
30
|
- complete rewrite in TS with more flexible API
|
|
13
31
|
|
package/browser/index.js
CHANGED
|
@@ -11,7 +11,7 @@ var CommentParser = (function (exports) {
|
|
|
11
11
|
: [source.slice(0, matches[0].length), source.slice(matches[0].length)];
|
|
12
12
|
}
|
|
13
13
|
function splitLines(source) {
|
|
14
|
-
return source.split(/\
|
|
14
|
+
return source.split(/\n/);
|
|
15
15
|
}
|
|
16
16
|
function seedSpec(spec = {}) {
|
|
17
17
|
return Object.assign({ tag: '', name: '', type: '', optional: false, description: '', problems: [], source: [] }, spec);
|
|
@@ -62,13 +62,12 @@ var CommentParser = (function (exports) {
|
|
|
62
62
|
return fence;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
var Markers;
|
|
66
65
|
(function (Markers) {
|
|
67
66
|
Markers["start"] = "/**";
|
|
68
67
|
Markers["nostart"] = "/***";
|
|
69
68
|
Markers["delim"] = "*";
|
|
70
69
|
Markers["end"] = "*/";
|
|
71
|
-
})(Markers || (Markers = {}));
|
|
70
|
+
})(exports.Markers || (exports.Markers = {}));
|
|
72
71
|
|
|
73
72
|
function getParser$1({ startLine = 0, } = {}) {
|
|
74
73
|
let block = null;
|
|
@@ -78,29 +77,29 @@ var CommentParser = (function (exports) {
|
|
|
78
77
|
const tokens = seedTokens();
|
|
79
78
|
[tokens.start, rest] = splitSpace(rest);
|
|
80
79
|
if (block === null &&
|
|
81
|
-
rest.startsWith(Markers.start) &&
|
|
82
|
-
!rest.startsWith(Markers.nostart)) {
|
|
80
|
+
rest.startsWith(exports.Markers.start) &&
|
|
81
|
+
!rest.startsWith(exports.Markers.nostart)) {
|
|
83
82
|
block = [];
|
|
84
|
-
tokens.delimiter = rest.slice(0, Markers.start.length);
|
|
85
|
-
rest = rest.slice(Markers.start.length);
|
|
83
|
+
tokens.delimiter = rest.slice(0, exports.Markers.start.length);
|
|
84
|
+
rest = rest.slice(exports.Markers.start.length);
|
|
86
85
|
[tokens.postDelimiter, rest] = splitSpace(rest);
|
|
87
86
|
}
|
|
88
87
|
if (block === null) {
|
|
89
88
|
num++;
|
|
90
89
|
return null;
|
|
91
90
|
}
|
|
92
|
-
const isClosed = rest.trimRight().endsWith(Markers.end);
|
|
91
|
+
const isClosed = rest.trimRight().endsWith(exports.Markers.end);
|
|
93
92
|
if (tokens.delimiter === '' &&
|
|
94
|
-
rest.startsWith(Markers.delim) &&
|
|
95
|
-
!rest.startsWith(Markers.end)) {
|
|
96
|
-
tokens.delimiter = Markers.delim;
|
|
97
|
-
rest = rest.slice(Markers.delim.length);
|
|
93
|
+
rest.startsWith(exports.Markers.delim) &&
|
|
94
|
+
!rest.startsWith(exports.Markers.end)) {
|
|
95
|
+
tokens.delimiter = exports.Markers.delim;
|
|
96
|
+
rest = rest.slice(exports.Markers.delim.length);
|
|
98
97
|
[tokens.postDelimiter, rest] = splitSpace(rest);
|
|
99
98
|
}
|
|
100
99
|
if (isClosed) {
|
|
101
100
|
const trimmed = rest.trimRight();
|
|
102
|
-
tokens.end = rest.slice(trimmed.length - Markers.end.length);
|
|
103
|
-
rest = trimmed.slice(0, -Markers.end.length);
|
|
101
|
+
tokens.end = rest.slice(trimmed.length - exports.Markers.end.length);
|
|
102
|
+
rest = trimmed.slice(0, -exports.Markers.end.length);
|
|
104
103
|
}
|
|
105
104
|
tokens.description = rest;
|
|
106
105
|
block.push({ number: num, source, tokens });
|
|
@@ -343,13 +342,13 @@ var CommentParser = (function (exports) {
|
|
|
343
342
|
return '';
|
|
344
343
|
// skip the opening line with no description
|
|
345
344
|
if (lines[0].tokens.description === '' &&
|
|
346
|
-
lines[0].tokens.delimiter === Markers.start)
|
|
345
|
+
lines[0].tokens.delimiter === exports.Markers.start)
|
|
347
346
|
lines = lines.slice(1);
|
|
348
347
|
// skip the closing line with no description
|
|
349
348
|
const lastLine = lines[lines.length - 1];
|
|
350
349
|
if (lastLine !== undefined &&
|
|
351
350
|
lastLine.tokens.description === '' &&
|
|
352
|
-
lastLine.tokens.end.endsWith(Markers.end))
|
|
351
|
+
lastLine.tokens.end.endsWith(exports.Markers.end))
|
|
353
352
|
lines = lines.slice(0, -1);
|
|
354
353
|
// description starts at the last line of type definition
|
|
355
354
|
lines = lines.slice(lines.reduce(lineNo, 0));
|
|
@@ -425,7 +424,7 @@ var CommentParser = (function (exports) {
|
|
|
425
424
|
name: 0,
|
|
426
425
|
};
|
|
427
426
|
const getWidth = (w, { tokens: t }) => ({
|
|
428
|
-
start: t.delimiter === Markers.start ? t.start.length : w.start,
|
|
427
|
+
start: t.delimiter === exports.Markers.start ? t.start.length : w.start,
|
|
429
428
|
tag: Math.max(w.tag, t.tag.length),
|
|
430
429
|
type: Math.max(w.type, t.type.length),
|
|
431
430
|
name: Math.max(w.name, t.name.length),
|
|
@@ -443,15 +442,15 @@ var CommentParser = (function (exports) {
|
|
|
443
442
|
tokens.type === '' &&
|
|
444
443
|
tokens.description === '';
|
|
445
444
|
// dangling '*/'
|
|
446
|
-
if (tokens.end === Markers.end && isEmpty) {
|
|
445
|
+
if (tokens.end === exports.Markers.end && isEmpty) {
|
|
447
446
|
tokens.start = space(w.start + 1);
|
|
448
447
|
return Object.assign(Object.assign({}, line), { tokens });
|
|
449
448
|
}
|
|
450
449
|
switch (tokens.delimiter) {
|
|
451
|
-
case Markers.start:
|
|
450
|
+
case exports.Markers.start:
|
|
452
451
|
tokens.start = space(w.start);
|
|
453
452
|
break;
|
|
454
|
-
case Markers.delim:
|
|
453
|
+
case exports.Markers.delim:
|
|
455
454
|
tokens.start = space(w.start + 1);
|
|
456
455
|
break;
|
|
457
456
|
default:
|
|
@@ -531,6 +530,48 @@ var CommentParser = (function (exports) {
|
|
|
531
530
|
};
|
|
532
531
|
}
|
|
533
532
|
|
|
533
|
+
var __rest$2 = (window && window.__rest) || function (s, e) {
|
|
534
|
+
var t = {};
|
|
535
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
536
|
+
t[p] = s[p];
|
|
537
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
538
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
539
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
540
|
+
t[p[i]] = s[p[i]];
|
|
541
|
+
}
|
|
542
|
+
return t;
|
|
543
|
+
};
|
|
544
|
+
const order = [
|
|
545
|
+
'end',
|
|
546
|
+
'description',
|
|
547
|
+
'postType',
|
|
548
|
+
'type',
|
|
549
|
+
'postName',
|
|
550
|
+
'name',
|
|
551
|
+
'postTag',
|
|
552
|
+
'tag',
|
|
553
|
+
'postDelimiter',
|
|
554
|
+
'delimiter',
|
|
555
|
+
'start',
|
|
556
|
+
];
|
|
557
|
+
function crlf(ending) {
|
|
558
|
+
const normalize = (source) => source.replace(/\r*$/, ending === 'LF' ? '' : '\r');
|
|
559
|
+
function update(line) {
|
|
560
|
+
const { tokens } = line;
|
|
561
|
+
for (const f of order) {
|
|
562
|
+
if (tokens[f] !== '') {
|
|
563
|
+
tokens[f] = normalize(tokens[f]);
|
|
564
|
+
break;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
return Object.assign(Object.assign({}, line), { tokens });
|
|
568
|
+
}
|
|
569
|
+
return (_a) => {
|
|
570
|
+
var { source } = _a, fields = __rest$2(_a, ["source"]);
|
|
571
|
+
return rewireSource(Object.assign(Object.assign({}, fields), { source: source.map(update) }));
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
|
|
534
575
|
function flow(...transforms) {
|
|
535
576
|
return (block) => transforms.reduce((block, t) => t(block), block);
|
|
536
577
|
}
|
|
@@ -584,6 +625,7 @@ var CommentParser = (function (exports) {
|
|
|
584
625
|
flow: flow,
|
|
585
626
|
align: align,
|
|
586
627
|
indent: indent,
|
|
628
|
+
crlf: crlf,
|
|
587
629
|
};
|
|
588
630
|
const tokenizers = {
|
|
589
631
|
tag: tagTokenizer,
|
package/es6/index.d.ts
CHANGED
|
@@ -5,7 +5,9 @@ import tagTokenizer from './parser/tokenizers/tag';
|
|
|
5
5
|
import typeTokenizer from './parser/tokenizers/type';
|
|
6
6
|
import alignTransform from './transforms/align';
|
|
7
7
|
import indentTransform from './transforms/indent';
|
|
8
|
+
import crlfTransform from './transforms/crlf';
|
|
8
9
|
import { flow as flowTransform } from './transforms/index';
|
|
10
|
+
export * from './primitives';
|
|
9
11
|
export declare function parse(source: string, options?: Partial<ParserOptions>): import("./primitives").Block[];
|
|
10
12
|
export declare const stringify: import("./stringifier").Stringifier;
|
|
11
13
|
export { default as inspect } from './stringifier/inspect';
|
|
@@ -13,6 +15,7 @@ export declare const transforms: {
|
|
|
13
15
|
flow: typeof flowTransform;
|
|
14
16
|
align: typeof alignTransform;
|
|
15
17
|
indent: typeof indentTransform;
|
|
18
|
+
crlf: typeof crlfTransform;
|
|
16
19
|
};
|
|
17
20
|
export declare const tokenizers: {
|
|
18
21
|
tag: typeof tagTokenizer;
|
package/es6/index.js
CHANGED
|
@@ -6,7 +6,9 @@ import typeTokenizer from './parser/tokenizers/type';
|
|
|
6
6
|
import getStringifier from './stringifier/index';
|
|
7
7
|
import alignTransform from './transforms/align';
|
|
8
8
|
import indentTransform from './transforms/indent';
|
|
9
|
+
import crlfTransform from './transforms/crlf';
|
|
9
10
|
import { flow as flowTransform } from './transforms/index';
|
|
11
|
+
export * from './primitives';
|
|
10
12
|
export function parse(source, options = {}) {
|
|
11
13
|
return getParser(options)(source);
|
|
12
14
|
}
|
|
@@ -16,6 +18,7 @@ export const transforms = {
|
|
|
16
18
|
flow: flowTransform,
|
|
17
19
|
align: alignTransform,
|
|
18
20
|
indent: indentTransform,
|
|
21
|
+
crlf: crlfTransform,
|
|
19
22
|
};
|
|
20
23
|
export const tokenizers = {
|
|
21
24
|
tag: tagTokenizer,
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
import { rewireSource } from '../util';
|
|
13
|
+
const order = [
|
|
14
|
+
'end',
|
|
15
|
+
'description',
|
|
16
|
+
'postType',
|
|
17
|
+
'type',
|
|
18
|
+
'postName',
|
|
19
|
+
'name',
|
|
20
|
+
'postTag',
|
|
21
|
+
'tag',
|
|
22
|
+
'postDelimiter',
|
|
23
|
+
'delimiter',
|
|
24
|
+
'start',
|
|
25
|
+
];
|
|
26
|
+
export default function crlf(ending) {
|
|
27
|
+
const normalize = (source) => source.replace(/\r*$/, ending === 'LF' ? '' : '\r');
|
|
28
|
+
function update(line) {
|
|
29
|
+
const { tokens } = line;
|
|
30
|
+
for (const f of order) {
|
|
31
|
+
if (tokens[f] !== '') {
|
|
32
|
+
tokens[f] = normalize(tokens[f]);
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return Object.assign(Object.assign({}, line), { tokens });
|
|
37
|
+
}
|
|
38
|
+
return (_a) => {
|
|
39
|
+
var { source } = _a, fields = __rest(_a, ["source"]);
|
|
40
|
+
return rewireSource(Object.assign(Object.assign({}, fields), { source: source.map(update) }));
|
|
41
|
+
};
|
|
42
|
+
}
|
package/es6/util.js
CHANGED
|
@@ -8,7 +8,7 @@ export function splitSpace(source) {
|
|
|
8
8
|
: [source.slice(0, matches[0].length), source.slice(matches[0].length)];
|
|
9
9
|
}
|
|
10
10
|
export function splitLines(source) {
|
|
11
|
-
return source.split(/\
|
|
11
|
+
return source.split(/\n/);
|
|
12
12
|
}
|
|
13
13
|
export function seedBlock(block = {}) {
|
|
14
14
|
return Object.assign({ description: '', tags: [], source: [], problems: [] }, block);
|
package/lib/index.d.ts
CHANGED
|
@@ -5,7 +5,9 @@ import tagTokenizer from './parser/tokenizers/tag';
|
|
|
5
5
|
import typeTokenizer from './parser/tokenizers/type';
|
|
6
6
|
import alignTransform from './transforms/align';
|
|
7
7
|
import indentTransform from './transforms/indent';
|
|
8
|
+
import crlfTransform from './transforms/crlf';
|
|
8
9
|
import { flow as flowTransform } from './transforms/index';
|
|
10
|
+
export * from './primitives';
|
|
9
11
|
export declare function parse(source: string, options?: Partial<ParserOptions>): import("./primitives").Block[];
|
|
10
12
|
export declare const stringify: import("./stringifier").Stringifier;
|
|
11
13
|
export { default as inspect } from './stringifier/inspect';
|
|
@@ -13,6 +15,7 @@ export declare const transforms: {
|
|
|
13
15
|
flow: typeof flowTransform;
|
|
14
16
|
align: typeof alignTransform;
|
|
15
17
|
indent: typeof indentTransform;
|
|
18
|
+
crlf: typeof crlfTransform;
|
|
16
19
|
};
|
|
17
20
|
export declare const tokenizers: {
|
|
18
21
|
tag: typeof tagTokenizer;
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
2
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
13
|
exports.tokenizers = exports.transforms = exports.inspect = exports.stringify = exports.parse = void 0;
|
|
4
14
|
const index_1 = require("./parser/index");
|
|
@@ -9,7 +19,9 @@ const type_1 = require("./parser/tokenizers/type");
|
|
|
9
19
|
const index_2 = require("./stringifier/index");
|
|
10
20
|
const align_1 = require("./transforms/align");
|
|
11
21
|
const indent_1 = require("./transforms/indent");
|
|
22
|
+
const crlf_1 = require("./transforms/crlf");
|
|
12
23
|
const index_3 = require("./transforms/index");
|
|
24
|
+
__exportStar(require("./primitives"), exports);
|
|
13
25
|
function parse(source, options = {}) {
|
|
14
26
|
return index_1.default(options)(source);
|
|
15
27
|
}
|
|
@@ -21,6 +33,7 @@ exports.transforms = {
|
|
|
21
33
|
flow: index_3.flow,
|
|
22
34
|
align: align_1.default,
|
|
23
35
|
indent: indent_1.default,
|
|
36
|
+
crlf: crlf_1.default,
|
|
24
37
|
};
|
|
25
38
|
exports.tokenizers = {
|
|
26
39
|
tag: tag_1.default,
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
+
var t = {};
|
|
4
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
+
t[p] = s[p];
|
|
6
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
+
t[p[i]] = s[p[i]];
|
|
10
|
+
}
|
|
11
|
+
return t;
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
const util_1 = require("../util");
|
|
15
|
+
const order = [
|
|
16
|
+
'end',
|
|
17
|
+
'description',
|
|
18
|
+
'postType',
|
|
19
|
+
'type',
|
|
20
|
+
'postName',
|
|
21
|
+
'name',
|
|
22
|
+
'postTag',
|
|
23
|
+
'tag',
|
|
24
|
+
'postDelimiter',
|
|
25
|
+
'delimiter',
|
|
26
|
+
'start',
|
|
27
|
+
];
|
|
28
|
+
function crlf(ending) {
|
|
29
|
+
const normalize = (source) => source.replace(/\r*$/, ending === 'LF' ? '' : '\r');
|
|
30
|
+
function update(line) {
|
|
31
|
+
const { tokens } = line;
|
|
32
|
+
for (const f of order) {
|
|
33
|
+
if (tokens[f] !== '') {
|
|
34
|
+
tokens[f] = normalize(tokens[f]);
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return Object.assign(Object.assign({}, line), { tokens });
|
|
39
|
+
}
|
|
40
|
+
return (_a) => {
|
|
41
|
+
var { source } = _a, fields = __rest(_a, ["source"]);
|
|
42
|
+
return util_1.rewireSource(Object.assign(Object.assign({}, fields), { source: source.map(update) }));
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
exports.default = crlf;
|
package/lib/util.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "comment-parser",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.6-beta.0",
|
|
4
4
|
"description": "Generic JSDoc-like comment parser",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -20,10 +20,11 @@
|
|
|
20
20
|
"node": ">= 10.0.0"
|
|
21
21
|
},
|
|
22
22
|
"scripts": {
|
|
23
|
-
"build": "rimraf lib es6 browser; tsc -p tsconfig.
|
|
23
|
+
"build": "rimraf lib es6 browser; tsc -p tsconfig.json && tsc -p tsconfig.node.json && rollup -o browser/index.js -f iife --context window -n CommentParser es6/index.js",
|
|
24
24
|
"format": "prettier --write src/ tests/",
|
|
25
25
|
"pretest": "rimraf coverage; npm run build",
|
|
26
|
-
"test": "prettier --check src/ tests/ && jest --verbose"
|
|
26
|
+
"test": "prettier --check src/ tests/ && jest --verbose",
|
|
27
|
+
"preversion": "npm run build"
|
|
27
28
|
},
|
|
28
29
|
"repository": {
|
|
29
30
|
"type": "git",
|
|
@@ -52,4 +53,4 @@
|
|
|
52
53
|
},
|
|
53
54
|
"homepage": "https://github.com/syavorsky/comment-parser",
|
|
54
55
|
"dependencies": {}
|
|
55
|
-
}
|
|
56
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -6,8 +6,11 @@ import typeTokenizer from './parser/tokenizers/type';
|
|
|
6
6
|
import getStringifier from './stringifier/index';
|
|
7
7
|
import alignTransform from './transforms/align';
|
|
8
8
|
import indentTransform from './transforms/indent';
|
|
9
|
+
import crlfTransform from './transforms/crlf';
|
|
9
10
|
import { flow as flowTransform } from './transforms/index';
|
|
10
11
|
|
|
12
|
+
export * from './primitives';
|
|
13
|
+
|
|
11
14
|
export function parse(source: string, options: Partial<ParserOptions> = {}) {
|
|
12
15
|
return getParser(options)(source);
|
|
13
16
|
}
|
|
@@ -19,6 +22,7 @@ export const transforms = {
|
|
|
19
22
|
flow: flowTransform,
|
|
20
23
|
align: alignTransform,
|
|
21
24
|
indent: indentTransform,
|
|
25
|
+
crlf: crlfTransform,
|
|
22
26
|
};
|
|
23
27
|
|
|
24
28
|
export const tokenizers = {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Spec, Line } from '../../primitives';
|
|
2
|
-
import { splitSpace, isSpace
|
|
2
|
+
import { splitSpace, isSpace } from '../../util';
|
|
3
3
|
import { Tokenizer } from './index';
|
|
4
4
|
|
|
5
5
|
const isQuoted = (s: string) => s && s.startsWith('"') && s.endsWith('"');
|
package/src/transforms/align.ts
CHANGED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Transform } from './index';
|
|
2
|
+
import { Block, Line } from '../primitives';
|
|
3
|
+
import { rewireSource } from '../util';
|
|
4
|
+
|
|
5
|
+
const order = [
|
|
6
|
+
'end',
|
|
7
|
+
'description',
|
|
8
|
+
'postType',
|
|
9
|
+
'type',
|
|
10
|
+
'postName',
|
|
11
|
+
'name',
|
|
12
|
+
'postTag',
|
|
13
|
+
'tag',
|
|
14
|
+
'postDelimiter',
|
|
15
|
+
'delimiter',
|
|
16
|
+
'start',
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
export type Ending = 'LF' | 'CRLF';
|
|
20
|
+
|
|
21
|
+
export default function crlf(ending: Ending): Transform {
|
|
22
|
+
const normalize = (source: string): string =>
|
|
23
|
+
source.replace(/\r*$/, ending === 'LF' ? '' : '\r');
|
|
24
|
+
|
|
25
|
+
function update(line: Line): Line {
|
|
26
|
+
const { tokens } = line;
|
|
27
|
+
for (const f of order) {
|
|
28
|
+
if (tokens[f] !== '') {
|
|
29
|
+
tokens[f] = normalize(tokens[f]);
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return { ...line, tokens };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return ({ source, ...fields }: Block): Block =>
|
|
37
|
+
rewireSource({ ...fields, source: source.map(update) });
|
|
38
|
+
}
|
package/src/transforms/indent.ts
CHANGED
|
@@ -9,7 +9,7 @@ const push = (offset: number) => {
|
|
|
9
9
|
};
|
|
10
10
|
|
|
11
11
|
export default function indent(pos: number): Transform {
|
|
12
|
-
let shift: (string) => string;
|
|
12
|
+
let shift: (string: string) => string;
|
|
13
13
|
const pad = (start: string) => {
|
|
14
14
|
if (shift === undefined) {
|
|
15
15
|
const offset = pos - start.length;
|
package/src/transforms/index.ts
CHANGED
package/src/util.ts
CHANGED
|
@@ -12,7 +12,7 @@ export function splitSpace(source: string): [string, string] {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
export function splitLines(source: string): string[] {
|
|
15
|
-
return source.split(/\
|
|
15
|
+
return source.split(/\n/);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export function seedBlock(block: Partial<Block> = {}): Block {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import getParser, { Parser } from '../../src/parser/source-parser';
|
|
2
|
-
import {
|
|
2
|
+
import { Line } from '../../src/primitives';
|
|
3
3
|
import { splitLines, seedBlock, seedTokens } from '../../src/util';
|
|
4
4
|
|
|
5
5
|
let _parse: Parser;
|
|
@@ -235,3 +235,51 @@ test('start line number', () => {
|
|
|
235
235
|
|
|
236
236
|
expect(parsed).toEqual([null, block]);
|
|
237
237
|
});
|
|
238
|
+
|
|
239
|
+
test('carriage returns', () => {
|
|
240
|
+
const source = splitLines(
|
|
241
|
+
['/**', ' * description', ' *', ' */', ''].join('\r\n')
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
const parsed = source.map(getParser());
|
|
245
|
+
|
|
246
|
+
const block = [
|
|
247
|
+
{
|
|
248
|
+
number: 0,
|
|
249
|
+
source: '/**\r',
|
|
250
|
+
tokens: seedTokens({
|
|
251
|
+
delimiter: '/**',
|
|
252
|
+
postDelimiter: '\r',
|
|
253
|
+
}),
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
number: 1,
|
|
257
|
+
source: ' * description\r',
|
|
258
|
+
tokens: seedTokens({
|
|
259
|
+
start: ' ',
|
|
260
|
+
delimiter: '*',
|
|
261
|
+
postDelimiter: ' ',
|
|
262
|
+
description: 'description\r',
|
|
263
|
+
}),
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
number: 2,
|
|
267
|
+
source: ' *\r',
|
|
268
|
+
tokens: seedTokens({
|
|
269
|
+
start: ' ',
|
|
270
|
+
delimiter: '*',
|
|
271
|
+
postDelimiter: '\r',
|
|
272
|
+
}),
|
|
273
|
+
},
|
|
274
|
+
{
|
|
275
|
+
number: 3,
|
|
276
|
+
source: ' */\r',
|
|
277
|
+
tokens: seedTokens({
|
|
278
|
+
start: ' ',
|
|
279
|
+
end: '*/\r',
|
|
280
|
+
}),
|
|
281
|
+
},
|
|
282
|
+
];
|
|
283
|
+
|
|
284
|
+
expect(parsed).toEqual([...nulls(3), block, null]);
|
|
285
|
+
});
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import crlf, { Ending } from '../../src/transforms/crlf';
|
|
2
|
+
import getParser, { Parser } from '../../src/parser/index';
|
|
3
|
+
import getStringifier, { Stringifier } from '../../src/stringifier/index';
|
|
4
|
+
|
|
5
|
+
const tests = [
|
|
6
|
+
[
|
|
7
|
+
'no CR',
|
|
8
|
+
'CRLF',
|
|
9
|
+
`
|
|
10
|
+
/**
|
|
11
|
+
* description
|
|
12
|
+
*
|
|
13
|
+
*/`,
|
|
14
|
+
`
|
|
15
|
+
/**\r
|
|
16
|
+
* description\r
|
|
17
|
+
*\r
|
|
18
|
+
*/\r`,
|
|
19
|
+
],
|
|
20
|
+
[
|
|
21
|
+
'mixed',
|
|
22
|
+
'CRLF',
|
|
23
|
+
`
|
|
24
|
+
/**
|
|
25
|
+
* description
|
|
26
|
+
*\r
|
|
27
|
+
*/`,
|
|
28
|
+
`
|
|
29
|
+
/**\r
|
|
30
|
+
* description\r
|
|
31
|
+
*\r
|
|
32
|
+
*/\r`,
|
|
33
|
+
],
|
|
34
|
+
[
|
|
35
|
+
'no CR',
|
|
36
|
+
'LF',
|
|
37
|
+
`
|
|
38
|
+
/**
|
|
39
|
+
* description
|
|
40
|
+
*
|
|
41
|
+
*/`,
|
|
42
|
+
`
|
|
43
|
+
/**
|
|
44
|
+
* description
|
|
45
|
+
*
|
|
46
|
+
*/`,
|
|
47
|
+
],
|
|
48
|
+
[
|
|
49
|
+
'mixed',
|
|
50
|
+
'LF',
|
|
51
|
+
`
|
|
52
|
+
/**
|
|
53
|
+
* description
|
|
54
|
+
*\r
|
|
55
|
+
*/`,
|
|
56
|
+
`
|
|
57
|
+
/**
|
|
58
|
+
* description
|
|
59
|
+
*
|
|
60
|
+
*/`,
|
|
61
|
+
],
|
|
62
|
+
];
|
|
63
|
+
|
|
64
|
+
test.each(tests)('CRLF - %s to %s', (name, mode, source, expected) => {
|
|
65
|
+
expected = expected.slice(1);
|
|
66
|
+
const parsed = getParser()(source);
|
|
67
|
+
const normalized = crlf(mode as Ending)(parsed[0]);
|
|
68
|
+
const out = getStringifier()(normalized);
|
|
69
|
+
expect(out).toBe(expected);
|
|
70
|
+
});
|
package/tests/unit/util.spec.ts
CHANGED
|
@@ -8,9 +8,9 @@ import {
|
|
|
8
8
|
} from '../../src/util';
|
|
9
9
|
|
|
10
10
|
test.each([
|
|
11
|
-
['win', 'a\r\nb\r\nc', ['a', 'b', 'c']],
|
|
11
|
+
['win', 'a\r\nb\r\nc', ['a\r', 'b\r', 'c']],
|
|
12
12
|
['unix', 'a\nb\nc', ['a', 'b', 'c']],
|
|
13
|
-
['mixed', 'a\nb\r\nc', ['a', 'b', 'c']],
|
|
13
|
+
['mixed', 'a\nb\r\nc', ['a', 'b\r', 'c']],
|
|
14
14
|
['none', 'abc', ['abc']],
|
|
15
15
|
])('spliLines - %s', (name, source, parsed) =>
|
|
16
16
|
expect(splitLines(source)).toEqual(parsed)
|
package/tsconfig.node.json
CHANGED