comment-parser 0.5.3 → 0.5.4
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/package.json +1 -1
- package/parsers.js +25 -16
- package/tests/parse.js +4 -4
- package/tests/parse.spec.js +88 -0
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/parsers.js
CHANGED
|
@@ -51,26 +51,35 @@ PARSERS.parse_name = function parse_name (str, data) {
|
|
|
51
51
|
var pos = skipws(str)
|
|
52
52
|
var name = ''
|
|
53
53
|
var brackets = 0
|
|
54
|
+
var res = {optional: false}
|
|
55
|
+
|
|
56
|
+
// if it starts with quoted group assume it is a literal
|
|
57
|
+
var quotedGroups = str.slice(pos).split('"')
|
|
58
|
+
if (quotedGroups.length > 1 && quotedGroups[0] === '' && quotedGroups.length % 2 === 1) {
|
|
59
|
+
name = quotedGroups[1]
|
|
60
|
+
pos += name.length + 2
|
|
61
|
+
// assume name is non-space string or anything wrapped into brackets
|
|
62
|
+
} else {
|
|
63
|
+
while (pos < str.length) {
|
|
64
|
+
brackets += (str[pos] === '[' ? 1 : (str[pos] === ']' ? -1 : 0))
|
|
65
|
+
name += str[pos]
|
|
66
|
+
pos++
|
|
67
|
+
if (brackets === 0 && /\s/.test(str[pos])) { break }
|
|
68
|
+
}
|
|
54
69
|
|
|
55
|
-
|
|
56
|
-
brackets += (str[pos] === '[' ? 1 : (str[pos] === ']' ? -1 : 0))
|
|
57
|
-
name += str[pos]
|
|
58
|
-
pos++
|
|
59
|
-
if (brackets === 0 && /\s/.test(str[pos])) { break }
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (brackets !== 0) { throw new Error('Invalid `name`, unpaired brackets') }
|
|
70
|
+
if (brackets !== 0) { throw new Error('Invalid `name`, unpaired brackets') }
|
|
63
71
|
|
|
64
|
-
|
|
72
|
+
res = {name: name, optional: false}
|
|
65
73
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
74
|
+
if (name[0] === '[' && name[name.length - 1] === ']') {
|
|
75
|
+
res.optional = true
|
|
76
|
+
name = name.slice(1, -1)
|
|
69
77
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
78
|
+
if (name.indexOf('=') !== -1) {
|
|
79
|
+
var parts = name.split('=')
|
|
80
|
+
name = parts[0]
|
|
81
|
+
res.default = parts[1].replace(/^(["'])(.+)(\1)$/, '$2')
|
|
82
|
+
}
|
|
74
83
|
}
|
|
75
84
|
}
|
|
76
85
|
|
package/tests/parse.js
CHANGED
|
@@ -11,8 +11,8 @@ var parse = require('../index')
|
|
|
11
11
|
|
|
12
12
|
module.exports = function (func, opts) {
|
|
13
13
|
var str = func.toString()
|
|
14
|
-
|
|
15
|
-
str.indexOf('{') + 1,
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
str = str
|
|
15
|
+
.slice(str.indexOf('{') + 1, str.lastIndexOf('}'))
|
|
16
|
+
.replace(/\r\n/g, '\n')
|
|
17
|
+
return parse(str, opts)
|
|
18
18
|
}
|
package/tests/parse.spec.js
CHANGED
|
@@ -950,4 +950,92 @@ describe('Comment string parsing', function () {
|
|
|
950
950
|
}]
|
|
951
951
|
})
|
|
952
952
|
})
|
|
953
|
+
|
|
954
|
+
it('should keep optional names spaces (issue #41)`', function () {
|
|
955
|
+
expect(parse(function () {
|
|
956
|
+
/**
|
|
957
|
+
* @section [Brand Colors] Here you can find all the brand colors
|
|
958
|
+
*/
|
|
959
|
+
})[0])
|
|
960
|
+
.to.eql({
|
|
961
|
+
line: 1,
|
|
962
|
+
source: '@section [Brand Colors] Here you can find all the brand colors',
|
|
963
|
+
description: '',
|
|
964
|
+
tags: [{
|
|
965
|
+
tag: 'section',
|
|
966
|
+
line: 2,
|
|
967
|
+
type: '',
|
|
968
|
+
name: 'Brand Colors',
|
|
969
|
+
source: '@section [Brand Colors] Here you can find all the brand colors',
|
|
970
|
+
optional: true,
|
|
971
|
+
description: 'Here you can find all the brand colors'
|
|
972
|
+
}]
|
|
973
|
+
})
|
|
974
|
+
})
|
|
975
|
+
|
|
976
|
+
it('should keep quotes in description (issue #41)`', function () {
|
|
977
|
+
expect(parse(function () {
|
|
978
|
+
/**
|
|
979
|
+
* @section "Brand Colors" Here you can find all the brand colors
|
|
980
|
+
*/
|
|
981
|
+
})[0])
|
|
982
|
+
.to.eql({
|
|
983
|
+
line: 1,
|
|
984
|
+
source: '@section "Brand Colors" Here you can find all the brand colors',
|
|
985
|
+
description: '',
|
|
986
|
+
tags: [{
|
|
987
|
+
tag: 'section',
|
|
988
|
+
line: 2,
|
|
989
|
+
type: '',
|
|
990
|
+
name: 'Brand Colors',
|
|
991
|
+
source: '@section "Brand Colors" Here you can find all the brand colors',
|
|
992
|
+
optional: false,
|
|
993
|
+
description: 'Here you can find all the brand colors'
|
|
994
|
+
}]
|
|
995
|
+
})
|
|
996
|
+
})
|
|
997
|
+
|
|
998
|
+
it('should use only quoted name (issue #41)`', function () {
|
|
999
|
+
expect(parse(function () {
|
|
1000
|
+
/**
|
|
1001
|
+
* @section "Brand Colors" Here you can find "all" the brand colors
|
|
1002
|
+
*/
|
|
1003
|
+
})[0])
|
|
1004
|
+
.to.eql({
|
|
1005
|
+
line: 1,
|
|
1006
|
+
source: '@section "Brand Colors" Here you can find "all" the brand colors',
|
|
1007
|
+
description: '',
|
|
1008
|
+
tags: [{
|
|
1009
|
+
tag: 'section',
|
|
1010
|
+
line: 2,
|
|
1011
|
+
type: '',
|
|
1012
|
+
name: 'Brand Colors',
|
|
1013
|
+
source: '@section "Brand Colors" Here you can find "all" the brand colors',
|
|
1014
|
+
optional: false,
|
|
1015
|
+
description: 'Here you can find "all" the brand colors'
|
|
1016
|
+
}]
|
|
1017
|
+
})
|
|
1018
|
+
})
|
|
1019
|
+
|
|
1020
|
+
it('should ignore inconsitent quoted groups (issue #41)`', function () {
|
|
1021
|
+
expect(parse(function () {
|
|
1022
|
+
/**
|
|
1023
|
+
* @section "Brand Colors Here you can find all the brand colors
|
|
1024
|
+
*/
|
|
1025
|
+
})[0])
|
|
1026
|
+
.to.eql({
|
|
1027
|
+
line: 1,
|
|
1028
|
+
source: '@section "Brand Colors Here you can find all the brand colors',
|
|
1029
|
+
description: '',
|
|
1030
|
+
tags: [{
|
|
1031
|
+
tag: 'section',
|
|
1032
|
+
line: 2,
|
|
1033
|
+
type: '',
|
|
1034
|
+
name: '"Brand',
|
|
1035
|
+
source: '@section "Brand Colors Here you can find all the brand colors',
|
|
1036
|
+
optional: false,
|
|
1037
|
+
description: 'Colors Here you can find all the brand colors'
|
|
1038
|
+
}]
|
|
1039
|
+
})
|
|
1040
|
+
})
|
|
953
1041
|
})
|