comment-parser 0.3.2 → 0.5.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/.eslintignore +1 -0
- package/.eslintrc +11 -0
- package/.travis.yml +2 -4
- package/CHANGELOG.md +9 -0
- package/README.md +24 -5
- package/index.js +31 -34
- package/package.json +11 -6
- package/parser.js +180 -246
- 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 +787 -534
- package/.jshintrc +0 -11
- package/.npmignore +0 -15
|
@@ -1,139 +1,130 @@
|
|
|
1
|
-
|
|
2
|
-
var expect = require('chai').expect
|
|
3
|
-
var parse = require('
|
|
1
|
+
/* eslint no-unused-vars:off */
|
|
2
|
+
var expect = require('chai').expect
|
|
3
|
+
var parse = require('./parse')
|
|
4
4
|
|
|
5
|
-
describe('parse() with custom tag parsers', function() {
|
|
6
|
-
|
|
7
|
-
function parsed(func, opts) {
|
|
8
|
-
var str = func.toString();
|
|
9
|
-
return parse(str.slice(
|
|
10
|
-
str.indexOf('{') + 1,
|
|
11
|
-
str.lastIndexOf('}')
|
|
12
|
-
).trim(), opts);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function sample() {
|
|
5
|
+
describe('parse() with custom tag parsers', function () {
|
|
6
|
+
function sample () {
|
|
16
7
|
/**
|
|
17
8
|
* @tag {type} name description
|
|
18
9
|
*/
|
|
19
|
-
var a
|
|
10
|
+
var a
|
|
20
11
|
}
|
|
21
12
|
|
|
22
|
-
it('should use `opts.parsers`', function() {
|
|
13
|
+
it('should use `opts.parsers`', function () {
|
|
23
14
|
var parsers = [
|
|
24
|
-
function everything(str) {
|
|
15
|
+
function everything (str) {
|
|
25
16
|
return {
|
|
26
|
-
source
|
|
27
|
-
data
|
|
28
|
-
tag
|
|
29
|
-
type
|
|
30
|
-
name
|
|
31
|
-
optional
|
|
32
|
-
description
|
|
17
|
+
source: str,
|
|
18
|
+
data: {
|
|
19
|
+
tag: 'tag',
|
|
20
|
+
type: 'type',
|
|
21
|
+
name: 'name',
|
|
22
|
+
optional: false,
|
|
23
|
+
description: 'description'
|
|
33
24
|
}
|
|
34
|
-
}
|
|
25
|
+
}
|
|
35
26
|
}
|
|
36
|
-
]
|
|
27
|
+
]
|
|
37
28
|
|
|
38
|
-
expect(
|
|
29
|
+
expect(parse(sample, {parsers: parsers})[0])
|
|
39
30
|
.to.eql({
|
|
40
|
-
line
|
|
41
|
-
description
|
|
42
|
-
source
|
|
31
|
+
line: 1,
|
|
32
|
+
description: '',
|
|
33
|
+
source: '@tag {type} name description',
|
|
43
34
|
tags: [{
|
|
44
|
-
tag
|
|
45
|
-
type
|
|
46
|
-
name
|
|
47
|
-
description
|
|
48
|
-
optional
|
|
49
|
-
source
|
|
50
|
-
line
|
|
35
|
+
tag: 'tag',
|
|
36
|
+
type: 'type',
|
|
37
|
+
name: 'name',
|
|
38
|
+
description: 'description',
|
|
39
|
+
optional: false,
|
|
40
|
+
source: '@tag {type} name description',
|
|
41
|
+
line: 2
|
|
51
42
|
}]
|
|
52
|
-
})
|
|
53
|
-
})
|
|
43
|
+
})
|
|
44
|
+
})
|
|
54
45
|
|
|
55
|
-
it('should merge parsers result', function() {
|
|
46
|
+
it('should merge parsers result', function () {
|
|
56
47
|
var parsers = [
|
|
57
|
-
function parser1(str) {
|
|
48
|
+
function parser1 (str) {
|
|
58
49
|
return {
|
|
59
|
-
source
|
|
60
|
-
data
|
|
61
|
-
}
|
|
50
|
+
source: '',
|
|
51
|
+
data: {tag: 'tag'}
|
|
52
|
+
}
|
|
62
53
|
},
|
|
63
|
-
function parser2(str) {
|
|
54
|
+
function parser2 (str) {
|
|
64
55
|
return {
|
|
65
|
-
source
|
|
66
|
-
data
|
|
67
|
-
}
|
|
56
|
+
source: '',
|
|
57
|
+
data: {type: 'type'}
|
|
58
|
+
}
|
|
68
59
|
},
|
|
69
|
-
function parser3(str) {
|
|
60
|
+
function parser3 (str) {
|
|
70
61
|
return {
|
|
71
|
-
source
|
|
72
|
-
data
|
|
73
|
-
name
|
|
74
|
-
description
|
|
75
|
-
}
|
|
76
|
-
}
|
|
62
|
+
source: '',
|
|
63
|
+
data: {
|
|
64
|
+
name: 'name',
|
|
65
|
+
description: 'description'
|
|
66
|
+
}
|
|
67
|
+
}
|
|
77
68
|
}
|
|
78
|
-
]
|
|
69
|
+
]
|
|
79
70
|
|
|
80
|
-
expect(
|
|
71
|
+
expect(parse(sample, {parsers: parsers})[0])
|
|
81
72
|
.to.eql({
|
|
82
|
-
line
|
|
83
|
-
description
|
|
84
|
-
source
|
|
73
|
+
line: 1,
|
|
74
|
+
description: '',
|
|
75
|
+
source: '@tag {type} name description',
|
|
85
76
|
tags: [{
|
|
86
|
-
tag
|
|
87
|
-
type
|
|
88
|
-
name
|
|
89
|
-
description
|
|
90
|
-
optional
|
|
91
|
-
source
|
|
92
|
-
line
|
|
77
|
+
tag: 'tag',
|
|
78
|
+
type: 'type',
|
|
79
|
+
name: 'name',
|
|
80
|
+
description: 'description',
|
|
81
|
+
optional: false,
|
|
82
|
+
source: '@tag {type} name description',
|
|
83
|
+
line: 2
|
|
93
84
|
}]
|
|
94
|
-
})
|
|
95
|
-
})
|
|
85
|
+
})
|
|
86
|
+
})
|
|
96
87
|
|
|
97
|
-
it('should catch parser exceptions and populate `errors` field', function() {
|
|
88
|
+
it('should catch parser exceptions and populate `errors` field', function () {
|
|
98
89
|
var parsers = [
|
|
99
|
-
function parser1(str) {
|
|
90
|
+
function parser1 (str) {
|
|
100
91
|
return {
|
|
101
|
-
source
|
|
102
|
-
data
|
|
103
|
-
}
|
|
92
|
+
source: '',
|
|
93
|
+
data: {tag: 'tag'}
|
|
94
|
+
}
|
|
104
95
|
},
|
|
105
|
-
function parser2(str) {
|
|
106
|
-
throw new Error('error 1')
|
|
96
|
+
function parser2 (str) {
|
|
97
|
+
throw new Error('error 1')
|
|
107
98
|
},
|
|
108
|
-
function parser3(str) {
|
|
109
|
-
throw new Error('error 2')
|
|
99
|
+
function parser3 (str) {
|
|
100
|
+
throw new Error('error 2')
|
|
110
101
|
},
|
|
111
|
-
function parser4(str) {
|
|
102
|
+
function parser4 (str) {
|
|
112
103
|
return {
|
|
113
|
-
source
|
|
114
|
-
data
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
]
|
|
104
|
+
source: '',
|
|
105
|
+
data: {name: 'name'}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
]
|
|
118
109
|
|
|
119
|
-
expect(
|
|
110
|
+
expect(parse(sample, {parsers: parsers})[0])
|
|
120
111
|
.to.eql({
|
|
121
|
-
line
|
|
122
|
-
description
|
|
123
|
-
source
|
|
112
|
+
line: 1,
|
|
113
|
+
description: '',
|
|
114
|
+
source: '@tag {type} name description',
|
|
124
115
|
tags: [{
|
|
125
|
-
tag
|
|
126
|
-
type
|
|
127
|
-
name
|
|
128
|
-
description
|
|
129
|
-
optional
|
|
130
|
-
source
|
|
131
|
-
errors
|
|
116
|
+
tag: 'tag',
|
|
117
|
+
type: '',
|
|
118
|
+
name: 'name',
|
|
119
|
+
description: '',
|
|
120
|
+
optional: false,
|
|
121
|
+
source: '@tag {type} name description',
|
|
122
|
+
errors: [
|
|
132
123
|
'parser2: error 1',
|
|
133
124
|
'parser3: error 2'
|
|
134
125
|
],
|
|
135
|
-
line
|
|
126
|
+
line: 2
|
|
136
127
|
}]
|
|
137
|
-
})
|
|
138
|
-
})
|
|
139
|
-
})
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
})
|
package/tests/files.spec.js
CHANGED
|
@@ -1,56 +1,55 @@
|
|
|
1
|
-
|
|
2
|
-
var fs
|
|
3
|
-
var
|
|
1
|
+
/* eslint no-unused-vars:off */
|
|
2
|
+
var fs = require('fs')
|
|
3
|
+
var path = require('path')
|
|
4
|
+
var stream = require('readable-stream')
|
|
4
5
|
var expect = require('chai').expect
|
|
5
|
-
var parse
|
|
6
|
-
|
|
7
|
-
describe('File parsing', function() {
|
|
8
|
-
|
|
9
|
-
it('should parse the file by path', function(done) {
|
|
10
|
-
parse.file(__dirname + '/fixtures/sample.js', function(err, parsed) {
|
|
11
|
-
if (err) { done(err); }
|
|
6
|
+
var parse = require('../index')
|
|
12
7
|
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
describe('File parsing', function () {
|
|
9
|
+
it('should parse the file by path', function (done) {
|
|
10
|
+
parse.file(path.resolve(__dirname, 'fixtures/sample.js'), function (err, parsed) {
|
|
11
|
+
if (err) { done(err) }
|
|
15
12
|
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
expect(parsed)
|
|
14
|
+
.to.be.an('array')
|
|
18
15
|
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
expect(parsed.length)
|
|
17
|
+
.to.eq(4)
|
|
21
18
|
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
expect(parsed[0].description)
|
|
20
|
+
.to.eq('File description')
|
|
24
21
|
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
expect(parsed[1].tags[0].tag)
|
|
23
|
+
.to.eq('class')
|
|
27
24
|
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
expect(parsed[2].tags[0].tag)
|
|
26
|
+
.to.eq('property')
|
|
30
27
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
});
|
|
28
|
+
expect(parsed[3].tags[0].tag)
|
|
29
|
+
.to.eq('method')
|
|
34
30
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
.to.be.instanceof(Error);
|
|
39
|
-
done();
|
|
40
|
-
});
|
|
41
|
-
});
|
|
31
|
+
done()
|
|
32
|
+
})
|
|
33
|
+
})
|
|
42
34
|
|
|
43
|
-
it('should
|
|
35
|
+
it('should path the error if file dows not exist', function (done) {
|
|
36
|
+
parse.file('does/not/exists', function (err, parsed) {
|
|
37
|
+
expect(err)
|
|
38
|
+
.to.be.instanceof(Error)
|
|
39
|
+
done()
|
|
40
|
+
})
|
|
41
|
+
})
|
|
44
42
|
|
|
45
|
-
|
|
43
|
+
it('should return `Transform` stream', function (done) {
|
|
44
|
+
var count = 0
|
|
46
45
|
|
|
47
|
-
var readable = fs.createReadStream(__dirname
|
|
46
|
+
var readable = fs.createReadStream(path.resolve(__dirname, 'fixtures/sample.js'), {encoding: 'utf8'})
|
|
48
47
|
|
|
49
|
-
var writable = new stream.Writable({objectMode: true})
|
|
50
|
-
writable._write = function(data, encoding, done) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
48
|
+
var writable = new stream.Writable({objectMode: true})
|
|
49
|
+
writable._write = function (data, encoding, done) {
|
|
50
|
+
count++
|
|
51
|
+
done()
|
|
52
|
+
}
|
|
54
53
|
|
|
55
54
|
readable
|
|
56
55
|
.on('error', done)
|
|
@@ -58,11 +57,11 @@ describe('File parsing', function() {
|
|
|
58
57
|
.on('error', done)
|
|
59
58
|
.pipe(writable)
|
|
60
59
|
.on('error', done)
|
|
61
|
-
.on('finish', function() {
|
|
60
|
+
.on('finish', function () {
|
|
62
61
|
expect(count)
|
|
63
|
-
.to.eq(4)
|
|
62
|
+
.to.eq(4)
|
|
64
63
|
|
|
65
|
-
done()
|
|
66
|
-
})
|
|
67
|
-
})
|
|
68
|
-
})
|
|
64
|
+
done()
|
|
65
|
+
})
|
|
66
|
+
})
|
|
67
|
+
})
|
package/tests/fixtures/sample.js
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* @param {Int} coords.x
|
|
11
11
|
* @param {Int} coords.y
|
|
12
12
|
*/
|
|
13
|
-
function Point(options) {
|
|
13
|
+
function Point (options) {
|
|
14
14
|
|
|
15
15
|
}
|
|
16
16
|
|
|
@@ -18,19 +18,19 @@ function Point(options) {
|
|
|
18
18
|
* This should be skipped
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Tells if point was ever initilized with coordinates
|
|
23
|
+
* @property {Boolean} [initilized=false]
|
|
24
|
+
*/
|
|
25
|
+
Point.prototype.initilized = false
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Moves point by x,y
|
|
29
|
+
* @method move
|
|
30
|
+
* @param {Object} [by]
|
|
31
|
+
* @param {Int} by.x
|
|
32
|
+
* @param {Int} by.y
|
|
33
|
+
*/
|
|
34
|
+
Point.prototype.move = function (by) {
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
}
|
package/tests/parse.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Source lines numeration:
|
|
3
|
+
*
|
|
4
|
+
* 0 function() {
|
|
5
|
+
* 1 // source with comments
|
|
6
|
+
* 2 }
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var parse = require('../index')
|
|
11
|
+
|
|
12
|
+
module.exports = function (func, opts) {
|
|
13
|
+
var str = func.toString()
|
|
14
|
+
return parse(str.slice(
|
|
15
|
+
str.indexOf('{') + 1,
|
|
16
|
+
str.lastIndexOf('}')
|
|
17
|
+
), opts)
|
|
18
|
+
}
|