comment-parser 0.4.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/.travis.yml +2 -4
- package/CHANGELOG.md +3 -0
- package/package.json +1 -1
- package/parser.js +24 -8
- package/tests/parse.spec.js +96 -0
- package/.jshintrc +0 -11
package/.travis.yml
CHANGED
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/parser.js
CHANGED
|
@@ -12,8 +12,8 @@ function merge (/* ...objects */) {
|
|
|
12
12
|
var res = {}
|
|
13
13
|
var objs = Array.prototype.slice.call(arguments)
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
obj = objs
|
|
15
|
+
for (var i = 0, l = objs.length; i < l; i++) {
|
|
16
|
+
obj = objs[i]
|
|
17
17
|
for (k in obj) {
|
|
18
18
|
if (obj.hasOwnProperty(k)) {
|
|
19
19
|
res[k] = obj[k]
|
|
@@ -83,9 +83,9 @@ function parse_tag (str, parsers) {
|
|
|
83
83
|
* Parses comment block (array of String lines)
|
|
84
84
|
*/
|
|
85
85
|
function parse_block (source, opts) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
86
|
+
var trim = opts.trim
|
|
87
|
+
? function trim (s) { return s.trim() }
|
|
88
|
+
: function trim (s) { return s }
|
|
89
89
|
|
|
90
90
|
var source_str = source
|
|
91
91
|
.map(function (line) { return trim(line.source) })
|
|
@@ -211,10 +211,26 @@ function mkextract (opts) {
|
|
|
211
211
|
|
|
212
212
|
// if we are on middle of comment block
|
|
213
213
|
if (chunk) {
|
|
214
|
+
var lineStart = indent
|
|
215
|
+
|
|
216
|
+
// figure out if we slice from opening marker pos
|
|
217
|
+
// or line start is shifted to the left
|
|
218
|
+
var nonSpaceChar = line.match(/\S/)
|
|
219
|
+
|
|
220
|
+
// skip for the first line starting with /** (fresh chunk)
|
|
221
|
+
// it always has the right indentation
|
|
222
|
+
if (chunk.length > 0 && nonSpaceChar) {
|
|
223
|
+
if (nonSpaceChar[0] === '*') {
|
|
224
|
+
lineStart = nonSpaceChar.index + 2
|
|
225
|
+
} else if (nonSpaceChar.index < indent) {
|
|
226
|
+
lineStart = nonSpaceChar.index
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
214
230
|
// slice the line until end or until closing marker start
|
|
215
|
-
chunk
|
|
231
|
+
chunk.push({
|
|
216
232
|
number: number,
|
|
217
|
-
source: line.slice(
|
|
233
|
+
source: line.slice(lineStart, endPos === -1 ? line.length : endPos)
|
|
218
234
|
})
|
|
219
235
|
|
|
220
236
|
// finalize block if end marker detected
|
|
@@ -238,7 +254,7 @@ module.exports = function parse (source, opts) {
|
|
|
238
254
|
var extract = mkextract(opts)
|
|
239
255
|
var lines = source.split(/\n/)
|
|
240
256
|
|
|
241
|
-
|
|
257
|
+
for (var i = 0, l = lines.length; i < l; i++) {
|
|
242
258
|
block = extract(lines.shift())
|
|
243
259
|
if (block) {
|
|
244
260
|
blocks.push(block)
|
package/tests/parse.spec.js
CHANGED
|
@@ -689,4 +689,100 @@ describe('Comment string parsing', function () {
|
|
|
689
689
|
}]
|
|
690
690
|
})
|
|
691
691
|
})
|
|
692
|
+
|
|
693
|
+
it('should parse doc block with bound forced to the left', function () {
|
|
694
|
+
expect(parse(function () {
|
|
695
|
+
/**
|
|
696
|
+
* Description text
|
|
697
|
+
* @tag tagname Tag description
|
|
698
|
+
*/
|
|
699
|
+
})[0])
|
|
700
|
+
.to.eql({
|
|
701
|
+
description: 'Description text',
|
|
702
|
+
source: 'Description text\n@tag tagname Tag description',
|
|
703
|
+
line: 1,
|
|
704
|
+
tags: [{
|
|
705
|
+
tag: 'tag',
|
|
706
|
+
name: 'tagname',
|
|
707
|
+
optional: false,
|
|
708
|
+
description: 'Tag description',
|
|
709
|
+
type: '',
|
|
710
|
+
line: 3,
|
|
711
|
+
source: '@tag tagname Tag description'
|
|
712
|
+
}]
|
|
713
|
+
})
|
|
714
|
+
})
|
|
715
|
+
|
|
716
|
+
it('should parse doc block with bound forced to the right', function () {
|
|
717
|
+
expect(parse(function () {
|
|
718
|
+
/**
|
|
719
|
+
* Description text
|
|
720
|
+
* @tag tagname Tag description
|
|
721
|
+
*/
|
|
722
|
+
})[0])
|
|
723
|
+
.to.eql({
|
|
724
|
+
description: 'Description text',
|
|
725
|
+
source: 'Description text\n@tag tagname Tag description',
|
|
726
|
+
line: 1,
|
|
727
|
+
tags: [{
|
|
728
|
+
tag: 'tag',
|
|
729
|
+
name: 'tagname',
|
|
730
|
+
optional: false,
|
|
731
|
+
description: 'Tag description',
|
|
732
|
+
type: '',
|
|
733
|
+
line: 3,
|
|
734
|
+
source: '@tag tagname Tag description'
|
|
735
|
+
}]
|
|
736
|
+
})
|
|
737
|
+
})
|
|
738
|
+
|
|
739
|
+
it('should parse doc block with soft bound', function () {
|
|
740
|
+
expect(parse(function () {
|
|
741
|
+
/**
|
|
742
|
+
Description text
|
|
743
|
+
another line
|
|
744
|
+
@tag tagname Tag description
|
|
745
|
+
*/
|
|
746
|
+
})[0])
|
|
747
|
+
.to.eql({
|
|
748
|
+
description: 'Description text\nanother line',
|
|
749
|
+
source: 'Description text\nanother line\n@tag tagname Tag description',
|
|
750
|
+
line: 1,
|
|
751
|
+
tags: [{
|
|
752
|
+
tag: 'tag',
|
|
753
|
+
name: 'tagname',
|
|
754
|
+
optional: false,
|
|
755
|
+
description: 'Tag description',
|
|
756
|
+
type: '',
|
|
757
|
+
line: 4,
|
|
758
|
+
source: '@tag tagname Tag description'
|
|
759
|
+
}]
|
|
760
|
+
})
|
|
761
|
+
})
|
|
762
|
+
|
|
763
|
+
it('should parse doc block with soft bound respecting `opts.trim = false`', function () {
|
|
764
|
+
expect(parse(function () {
|
|
765
|
+
/**
|
|
766
|
+
Description text
|
|
767
|
+
another line
|
|
768
|
+
@tag tagname Tag description
|
|
769
|
+
*/
|
|
770
|
+
}, {
|
|
771
|
+
trim: false
|
|
772
|
+
})[0])
|
|
773
|
+
.to.eql({
|
|
774
|
+
description: '\nDescription text\n another line',
|
|
775
|
+
source: '\nDescription text\n another line\n@tag tagname Tag description\n',
|
|
776
|
+
line: 1,
|
|
777
|
+
tags: [{
|
|
778
|
+
tag: 'tag',
|
|
779
|
+
name: 'tagname',
|
|
780
|
+
optional: false,
|
|
781
|
+
description: 'Tag description\n',
|
|
782
|
+
type: '',
|
|
783
|
+
line: 4,
|
|
784
|
+
source: '@tag tagname Tag description\n'
|
|
785
|
+
}]
|
|
786
|
+
})
|
|
787
|
+
})
|
|
692
788
|
})
|