comment-parser 0.3.1 → 0.4.2
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/.eslintignore +1 -0
- package/.eslintrc +11 -0
- package/.travis.yml +2 -4
- package/CHANGELOG.md +10 -1
- package/README.md +35 -16
- package/index.js +31 -34
- package/package.json +11 -6
- package/parser.js +172 -257
- package/parsers.js +100 -0
- package/tests/custom-parsers.spec.js +90 -99
- package/tests/files.spec.js +45 -46
- package/tests/fixtures/sample.js +15 -15
- package/tests/parse.js +18 -0
- package/tests/parse.spec.js +731 -513
- package/.jshintrc +0 -11
package/.eslintignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
node_modules/*
|
package/.eslintrc
ADDED
package/.travis.yml
CHANGED
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
# v0.4.2
|
|
2
|
+
- tolerate inconsistent lines alignment within block
|
|
3
|
+
|
|
4
|
+
# v0.4.1
|
|
5
|
+
- refactored parsing, allow to not start lines with "* " inside block
|
|
6
|
+
|
|
7
|
+
# v0.3.2
|
|
8
|
+
- fix RegExp for `description` extraction to allow $ char
|
|
9
|
+
|
|
1
10
|
# v0.3.1
|
|
2
11
|
- use `readable-stream` fro Node 0.8 comatibility
|
|
3
12
|
- allow to pass optional parameters to `parse.file(path [,opts], done)`
|
|
@@ -39,4 +48,4 @@
|
|
|
39
48
|
|
|
40
49
|
# v0.1.0
|
|
41
50
|
|
|
42
|
-
Initial implementation
|
|
51
|
+
Initial implementation
|
package/README.md
CHANGED
|
@@ -66,7 +66,29 @@ this would be parsed into following
|
|
|
66
66
|
|
|
67
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
68
|
|
|
69
|
-
|
|
69
|
+
Below are examples of acceptable comment formats
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
/** online comment */
|
|
73
|
+
|
|
74
|
+
/** first line
|
|
75
|
+
* second line */
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
No *s on middle lines is acceptable too
|
|
79
|
+
which might be convenient for writing big
|
|
80
|
+
chunks of text.
|
|
81
|
+
|
|
82
|
+
* keeping *s on some lines
|
|
83
|
+
* would work either
|
|
84
|
+
|
|
85
|
+
left bound is dermined by opening marker position
|
|
86
|
+
and white space will be trimmed as there was '* '
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Comments starting with `/***` and `/*` are ignored.
|
|
70
92
|
|
|
71
93
|
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
94
|
|
|
@@ -74,7 +96,7 @@ Also you can parse entire file with `parse.file('path/to/file', callback)` or ac
|
|
|
74
96
|
|
|
75
97
|
|
|
76
98
|
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
|
-
|
|
99
|
+
|
|
78
100
|
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
101
|
|
|
80
102
|
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 +108,23 @@ Tag node data is build by merging result bits from all parsers. Here is some exa
|
|
|
86
108
|
*/
|
|
87
109
|
parse(source, {parsers: [
|
|
88
110
|
// takes entire string
|
|
89
|
-
function parse_tag(str, data) {
|
|
90
|
-
return {source: ' @tag', data: {tag: 'tag'}};
|
|
91
|
-
},
|
|
111
|
+
function parse_tag(str, data) {
|
|
112
|
+
return {source: ' @tag', data: {tag: 'tag'}};
|
|
113
|
+
},
|
|
92
114
|
// parser throwing exception
|
|
93
115
|
function check_tag(str, data) {
|
|
94
|
-
if (allowed_tags.indexOf(data.tag) === -1) {
|
|
116
|
+
if (allowed_tags.indexOf(data.tag) === -1) {
|
|
95
117
|
throw new Error('Unrecognized tag "' + data.tag + '"');
|
|
96
118
|
}
|
|
97
119
|
},
|
|
98
120
|
// takes the rest of the string after ' @tag''
|
|
99
|
-
function parse_name1(str, data) {
|
|
100
|
-
return {source: ' name', data: {name: 'name1'}};
|
|
121
|
+
function parse_name1(str, data) {
|
|
122
|
+
return {source: ' name', data: {name: 'name1'}};
|
|
101
123
|
},
|
|
102
124
|
// alternative name parser
|
|
103
|
-
function parse_name2(str, data) {
|
|
104
|
-
return {source: ' name', data: {name: 'name2'}};
|
|
105
|
-
}
|
|
125
|
+
function parse_name2(str, data) {
|
|
126
|
+
return {source: ' name', data: {name: 'name2'}};
|
|
127
|
+
}
|
|
106
128
|
]});
|
|
107
129
|
```
|
|
108
130
|
|
|
@@ -134,10 +156,7 @@ This would produce following:
|
|
|
134
156
|
|
|
135
157
|
## Contributors
|
|
136
158
|
|
|
137
|
-
|
|
138
|
-
- [Sergii Iavorskyi](https://github.com/yavorskiy)
|
|
139
159
|
- [Alexej Yaroshevich](https://github.com/zxqfox)
|
|
160
|
+
- [Evgeny Reznichenko](https://github.com/zxcabs)
|
|
140
161
|
- [Jordan Harband](https://github.com/ljharb)
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
Happy coding :)
|
|
162
|
+
- [Sergii Iavorskyi](https://github.com/yavorskiy)
|
package/index.js
CHANGED
|
@@ -1,64 +1,61 @@
|
|
|
1
1
|
|
|
2
|
-
'use strict'
|
|
2
|
+
'use strict'
|
|
3
3
|
|
|
4
|
-
var fs
|
|
5
|
-
var stream = require('readable-stream')
|
|
6
|
-
var util
|
|
4
|
+
var fs = require('fs')
|
|
5
|
+
var stream = require('readable-stream')
|
|
6
|
+
var util = require('util')
|
|
7
7
|
|
|
8
|
-
var parse
|
|
8
|
+
var parse = require('./parser')
|
|
9
9
|
|
|
10
|
-
module.exports = parse
|
|
10
|
+
module.exports = parse
|
|
11
11
|
|
|
12
12
|
/* ------- Transform stream ------- */
|
|
13
13
|
|
|
14
|
-
function Parser(opts) {
|
|
15
|
-
opts = opts || {}
|
|
16
|
-
stream.Transform.call(this, {objectMode: true})
|
|
17
|
-
this._extract = parse.mkextract(opts)
|
|
14
|
+
function Parser (opts) {
|
|
15
|
+
opts = opts || {}
|
|
16
|
+
stream.Transform.call(this, {objectMode: true})
|
|
17
|
+
this._extract = parse.mkextract(opts)
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
util.inherits(Parser, stream.Transform)
|
|
20
|
+
util.inherits(Parser, stream.Transform)
|
|
21
21
|
|
|
22
|
-
Parser.prototype._transform = function transform(data, encoding, done) {
|
|
23
|
-
|
|
24
|
-
var
|
|
25
|
-
var lines = data.toString().split(/\n/);
|
|
22
|
+
Parser.prototype._transform = function transform (data, encoding, done) {
|
|
23
|
+
var block
|
|
24
|
+
var lines = data.toString().split(/\n/)
|
|
26
25
|
|
|
27
26
|
while (lines.length) {
|
|
28
|
-
block = this._extract(lines.shift())
|
|
27
|
+
block = this._extract(lines.shift())
|
|
29
28
|
if (block) {
|
|
30
|
-
this.push(block)
|
|
29
|
+
this.push(block)
|
|
31
30
|
}
|
|
32
31
|
}
|
|
33
32
|
|
|
34
|
-
done()
|
|
35
|
-
}
|
|
33
|
+
done()
|
|
34
|
+
}
|
|
36
35
|
|
|
37
|
-
module.exports.stream = function stream(opts) {
|
|
38
|
-
return new Parser(opts)
|
|
39
|
-
}
|
|
36
|
+
module.exports.stream = function stream (opts) {
|
|
37
|
+
return new Parser(opts)
|
|
38
|
+
}
|
|
40
39
|
|
|
41
40
|
/* ------- File parser ------- */
|
|
42
41
|
|
|
43
|
-
module.exports.file = function file(file_path, done) {
|
|
44
|
-
|
|
45
|
-
var
|
|
46
|
-
var collected = [];
|
|
42
|
+
module.exports.file = function file (file_path, done) {
|
|
43
|
+
var opts = {}
|
|
44
|
+
var collected = []
|
|
47
45
|
|
|
48
46
|
if (arguments.length === 3) {
|
|
49
|
-
opts = done
|
|
50
|
-
done = arguments[2]
|
|
47
|
+
opts = done
|
|
48
|
+
done = arguments[2]
|
|
51
49
|
}
|
|
52
50
|
|
|
53
51
|
return fs.createReadStream(file_path, {encoding: 'utf8'})
|
|
54
52
|
.on('error', done)
|
|
55
53
|
.pipe(new Parser(opts))
|
|
56
54
|
.on('error', done)
|
|
57
|
-
.on('data', function(data) {
|
|
58
|
-
collected.push(data)
|
|
55
|
+
.on('data', function (data) {
|
|
56
|
+
collected.push(data)
|
|
59
57
|
})
|
|
60
58
|
.on('finish', function () {
|
|
61
|
-
done(null, collected)
|
|
62
|
-
})
|
|
63
|
-
}
|
|
64
|
-
|
|
59
|
+
done(null, collected)
|
|
60
|
+
})
|
|
61
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "comment-parser",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"description": "Generic JSDoc-like comment parser. ",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"directories": {
|
|
@@ -11,14 +11,19 @@
|
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
13
|
"chai": "~1.9.0",
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
14
|
+
"eslint": "^4.7.1",
|
|
15
|
+
"eslint-config-standard": "^10.2.1",
|
|
16
|
+
"eslint-plugin-import": "^2.7.0",
|
|
17
|
+
"eslint-plugin-node": "^5.1.1",
|
|
18
|
+
"eslint-plugin-promise": "^3.5.0",
|
|
19
|
+
"eslint-plugin-standard": "^3.0.1",
|
|
20
|
+
"mocha": "^3.5.3",
|
|
17
21
|
"nodemon": "^1.2.1"
|
|
18
22
|
},
|
|
19
23
|
"scripts": {
|
|
20
|
-
"
|
|
21
|
-
"
|
|
24
|
+
"pretest": "eslint .",
|
|
25
|
+
"test": "mocha tests",
|
|
26
|
+
"watch": "nodemon -q -i node_modules -x npm test"
|
|
22
27
|
},
|
|
23
28
|
"repository": {
|
|
24
29
|
"type": "git",
|