comment-parser 0.3.2 → 0.4.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 -3
- package/README.md +28 -15
- package/package.json +1 -1
- package/parser.js +2 -2
- package/tests/parse.spec.js +10 -32
package/CHANGELOG.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
# v0.
|
|
2
|
-
-
|
|
1
|
+
# v0.4.0
|
|
2
|
+
- allow to preserve exact source and line numbers with `opts.trim = false`
|
|
3
3
|
|
|
4
4
|
# v0.3.1
|
|
5
5
|
- use `readable-stream` fro Node 0.8 comatibility
|
|
6
|
-
- allow to pass optional parameters to `parse.file(path [,opts], done)`
|
|
6
|
+
- allow to pass optional parameters to `parse.file(path [,opts], done)`
|
|
7
7
|
- allow `parse.stream` to work with Buffers in addition to strings
|
|
8
8
|
|
|
9
9
|
# v0.3.0
|
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@ Generic JSDoc-like comment parser. This library is not intended to be documentat
|
|
|
5
5
|
|
|
6
6
|
`npm install comment-parser`
|
|
7
7
|
|
|
8
|
-
Module provides `parse(s:String[,
|
|
8
|
+
Module provides `parse(s:String[, options:Object]):Object` function which takes `/** ... */` comment string and returns array of objects with parsed data.
|
|
9
9
|
|
|
10
10
|
It is not trying to detect relations between tags or somehow recognize their meaning. Any tag can be used, as long as it satisfies the format.
|
|
11
11
|
|
|
@@ -64,17 +64,31 @@ this would be parsed into following
|
|
|
64
64
|
}]
|
|
65
65
|
```
|
|
66
66
|
|
|
67
|
-
By default dotted names like `name.subname.subsubname` will be expanded into nested sections, this can be prevented by passing `opts.dotted_names = false`.
|
|
68
|
-
|
|
69
67
|
Invalid comment blocks are skipped. Comments starting with `/*` and `/***` are considered not valid.
|
|
70
68
|
|
|
71
69
|
Also you can parse entire file with `parse.file('path/to/file', callback)` or acquire an instance of [Transform](http://nodejs.org/api/stream.html#stream_class_stream_transform) stream with `parse.stream()`.
|
|
72
70
|
|
|
71
|
+
|
|
72
|
+
### Options
|
|
73
|
+
|
|
74
|
+
#### dotted_names `true`
|
|
75
|
+
|
|
76
|
+
Tells parser to expand dotted names like `name.subname.subsubname` into nested sections
|
|
77
|
+
|
|
78
|
+
#### trim `true`
|
|
79
|
+
|
|
80
|
+
Makes parser to trim white spaces, set it to `false` to preserve exact source and line numbers
|
|
81
|
+
|
|
82
|
+
#### parsers, `[PARSERS.{parse_tag, parse_type, parse_name, parse_description}]`
|
|
83
|
+
|
|
84
|
+
Array of alternative parsers (read below).
|
|
85
|
+
|
|
86
|
+
|
|
73
87
|
## Custom parsers
|
|
74
88
|
|
|
75
89
|
|
|
76
90
|
In case you need to parse tags in different way you can pass `opts.parsers = [parser1, ..., parserN]`, where each parser is `function name(str:String, data:Object):{source:String, data:Object}`.
|
|
77
|
-
|
|
91
|
+
|
|
78
92
|
Each parser function takes string left after previous parsers applied and data produced by them. And returns `null` or `{source: '', data:{}}` where `source` is consumed substring and `data` is a payload with tag node fields.
|
|
79
93
|
|
|
80
94
|
Tag node data is build by merging result bits from all parsers. Here is some example that is not doing actual parsing but is demonstrating the flow:
|
|
@@ -86,23 +100,23 @@ Tag node data is build by merging result bits from all parsers. Here is some exa
|
|
|
86
100
|
*/
|
|
87
101
|
parse(source, {parsers: [
|
|
88
102
|
// takes entire string
|
|
89
|
-
function parse_tag(str, data) {
|
|
90
|
-
return {source: ' @tag', data: {tag: 'tag'}};
|
|
91
|
-
},
|
|
103
|
+
function parse_tag(str, data) {
|
|
104
|
+
return {source: ' @tag', data: {tag: 'tag'}};
|
|
105
|
+
},
|
|
92
106
|
// parser throwing exception
|
|
93
107
|
function check_tag(str, data) {
|
|
94
|
-
if (allowed_tags.indexOf(data.tag) === -1) {
|
|
108
|
+
if (allowed_tags.indexOf(data.tag) === -1) {
|
|
95
109
|
throw new Error('Unrecognized tag "' + data.tag + '"');
|
|
96
110
|
}
|
|
97
111
|
},
|
|
98
112
|
// takes the rest of the string after ' @tag''
|
|
99
|
-
function parse_name1(str, data) {
|
|
100
|
-
return {source: ' name', data: {name: 'name1'}};
|
|
113
|
+
function parse_name1(str, data) {
|
|
114
|
+
return {source: ' name', data: {name: 'name1'}};
|
|
101
115
|
},
|
|
102
116
|
// alternative name parser
|
|
103
|
-
function parse_name2(str, data) {
|
|
104
|
-
return {source: ' name', data: {name: 'name2'}};
|
|
105
|
-
}
|
|
117
|
+
function parse_name2(str, data) {
|
|
118
|
+
return {source: ' name', data: {name: 'name2'}};
|
|
119
|
+
}
|
|
106
120
|
]});
|
|
107
121
|
```
|
|
108
122
|
|
|
@@ -135,10 +149,9 @@ This would produce following:
|
|
|
135
149
|
## Contributors
|
|
136
150
|
|
|
137
151
|
|
|
152
|
+
- [Sergii Iavorskyi](https://github.com/yavorskiy)
|
|
138
153
|
- [Alexej Yaroshevich](https://github.com/zxqfox)
|
|
139
|
-
- [Evgeny Reznichenko](https://github.com/zxcabs)
|
|
140
154
|
- [Jordan Harband](https://github.com/ljharb)
|
|
141
|
-
- [Sergii Iavorskyi](https://github.com/yavorskiy)
|
|
142
155
|
|
|
143
156
|
|
|
144
157
|
Happy coding :)
|
package/package.json
CHANGED
package/parser.js
CHANGED
|
@@ -115,7 +115,7 @@ PARSERS.parse_name = function parse_name(str, data) {
|
|
|
115
115
|
PARSERS.parse_description = function parse_description(str, data) {
|
|
116
116
|
if (data.errors && data.errors.length) { return null; }
|
|
117
117
|
|
|
118
|
-
var result = str.match(/^\s+(
|
|
118
|
+
var result = str.match(/^\s+([^$]+)?/);
|
|
119
119
|
|
|
120
120
|
if (result) {
|
|
121
121
|
return {
|
|
@@ -347,4 +347,4 @@ module.exports = function parse(source, opts) {
|
|
|
347
347
|
};
|
|
348
348
|
|
|
349
349
|
module.exports.PARSERS = PARSERS;
|
|
350
|
-
module.exports.mkextract = mkextract;
|
|
350
|
+
module.exports.mkextract = mkextract;
|
package/tests/parse.spec.js
CHANGED
|
@@ -10,7 +10,7 @@ describe('Comment string parsing', function() {
|
|
|
10
10
|
* 0 function() {
|
|
11
11
|
* 1 // source with comments
|
|
12
12
|
* 2 }
|
|
13
|
-
*
|
|
13
|
+
*
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
16
|
function parsed(func, opts) {
|
|
@@ -39,11 +39,11 @@ describe('Comment string parsing', function() {
|
|
|
39
39
|
expect(parsed(function(){
|
|
40
40
|
/**
|
|
41
41
|
*
|
|
42
|
-
*
|
|
42
|
+
*
|
|
43
43
|
* Description first line
|
|
44
44
|
*
|
|
45
45
|
* Description second line
|
|
46
|
-
*
|
|
46
|
+
*
|
|
47
47
|
*/
|
|
48
48
|
var a;
|
|
49
49
|
})[0])
|
|
@@ -59,7 +59,7 @@ describe('Comment string parsing', function() {
|
|
|
59
59
|
|
|
60
60
|
expect(parsed(function(){
|
|
61
61
|
/**
|
|
62
|
-
*
|
|
62
|
+
*
|
|
63
63
|
*/
|
|
64
64
|
var a;
|
|
65
65
|
}).length)
|
|
@@ -136,10 +136,10 @@ describe('Comment string parsing', function() {
|
|
|
136
136
|
expect(parsed(function(){
|
|
137
137
|
/**
|
|
138
138
|
*
|
|
139
|
-
*
|
|
139
|
+
*
|
|
140
140
|
* Description first line
|
|
141
141
|
* second line
|
|
142
|
-
*
|
|
142
|
+
*
|
|
143
143
|
* third line
|
|
144
144
|
*/
|
|
145
145
|
var a;
|
|
@@ -178,7 +178,7 @@ describe('Comment string parsing', function() {
|
|
|
178
178
|
it('should parse `@tag`', function() {
|
|
179
179
|
expect(parsed(function(){
|
|
180
180
|
/**
|
|
181
|
-
*
|
|
181
|
+
*
|
|
182
182
|
* @my-tag
|
|
183
183
|
*/
|
|
184
184
|
var a;
|
|
@@ -252,14 +252,14 @@ describe('Comment string parsing', function() {
|
|
|
252
252
|
})[0])
|
|
253
253
|
.to.eql({
|
|
254
254
|
line : 1,
|
|
255
|
-
source : '@my-tag {my.type} name',
|
|
255
|
+
source : '@my-tag {my.type} name',
|
|
256
256
|
description : '',
|
|
257
257
|
tags: [{
|
|
258
258
|
tag : 'my-tag',
|
|
259
259
|
line : 2,
|
|
260
260
|
type : 'my.type',
|
|
261
261
|
name : 'name',
|
|
262
|
-
source : '@my-tag {my.type} name',
|
|
262
|
+
source : '@my-tag {my.type} name',
|
|
263
263
|
description : '',
|
|
264
264
|
optional : false
|
|
265
265
|
}]
|
|
@@ -281,7 +281,7 @@ describe('Comment string parsing', function() {
|
|
|
281
281
|
line : 2,
|
|
282
282
|
type : 'my.type',
|
|
283
283
|
name : 'name',
|
|
284
|
-
source : '@my-tag {my.type} name description',
|
|
284
|
+
source : '@my-tag {my.type} name description',
|
|
285
285
|
description : 'description',
|
|
286
286
|
optional : false
|
|
287
287
|
}]
|
|
@@ -589,26 +589,4 @@ describe('Comment string parsing', function() {
|
|
|
589
589
|
}]
|
|
590
590
|
});
|
|
591
591
|
});
|
|
592
|
-
|
|
593
|
-
it('parses $ in description`', function() {
|
|
594
|
-
expect(parsed(function(){
|
|
595
|
-
/**
|
|
596
|
-
* @my-tag {String} name description with $ char
|
|
597
|
-
*/
|
|
598
|
-
})[0])
|
|
599
|
-
.to.eql({
|
|
600
|
-
line : 1,
|
|
601
|
-
source : '@my-tag {String} name description with $ char',
|
|
602
|
-
description : '',
|
|
603
|
-
tags: [{
|
|
604
|
-
tag : 'my-tag',
|
|
605
|
-
line : 2,
|
|
606
|
-
type : 'String',
|
|
607
|
-
name : 'name',
|
|
608
|
-
source : '@my-tag {String} name description with $ char',
|
|
609
|
-
optional : false,
|
|
610
|
-
description : 'description with $ char'
|
|
611
|
-
}]
|
|
612
|
-
});
|
|
613
|
-
});
|
|
614
592
|
});
|