comment-parser
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -0,2 +1,10 @@ | ||
# v0.2.1 | ||
- `refactor` make line pasing mechanism more tolerable | ||
# v0.2.0 | ||
- `feature` include source line numbers in parsed data | ||
- `feature` optionally prevent dotten names expanding | ||
# v0.1.2 | ||
@@ -3,0 +11,0 @@ |
84
index.js
@@ -12,4 +12,73 @@ | ||
/** | ||
* analogue of str.match(/@(\S+)(?:\s+\{([^\}]+)\})?(?:\s+(\S+))?(?:\s+([^$]+))?/); | ||
* @param {string} str raw jsdoc string | ||
* @returns {object} parsed tag node | ||
*/ | ||
function parse_tag_line(str) { | ||
if (typeof str !== 'string') { return false; } | ||
if (str[0] !== '@') { return false; } | ||
var pos = 1; | ||
var l = str.length; | ||
var error = null; | ||
var res = { | ||
tag : _tag(), | ||
type : _type() || '', | ||
name : _name() || '', | ||
description : _rest() || '' | ||
}; | ||
if (error) { | ||
res.error = error; | ||
} | ||
return res; | ||
function _skipws() { | ||
while (str[pos] === ' ' && pos < l) { pos ++; } | ||
} | ||
function _tag() { // @(\S+) | ||
var sp = str.indexOf(' ', pos); | ||
sp = sp < 0 ? l : sp; | ||
var res = str.substr(pos, sp - pos); | ||
pos = sp; | ||
return res; | ||
} | ||
function _type() { // (?:\s+\{([^\}]+)\})? | ||
_skipws(); | ||
if (str[pos] !== '{') { return ''; } | ||
var ch; | ||
var res = ''; | ||
var curlies = 0; | ||
while (pos < l) { | ||
ch = str[pos]; | ||
curlies += ch === '{' ? 1 : ch === '}' ? -1 : 0; | ||
res += ch; | ||
pos ++; | ||
if (!curlies) { | ||
break; | ||
} | ||
} | ||
if (curlies) { | ||
// throw new Error('Unpaired curly in type doc'); | ||
error = 'Unpaired curly in type doc'; | ||
pos -= res.length; | ||
return ''; | ||
} | ||
return res.substr(1, res.length - 2); | ||
} | ||
function _name() { // (?:\s+(\S+))? | ||
if (error) { return ''; } | ||
_skipws(); | ||
return _tag(); | ||
} | ||
function _rest() { // (?:\s+([^$]+))? | ||
_skipws(); | ||
return str.substr(pos); | ||
} | ||
} | ||
function parse_chunk(source, opts) { | ||
source = source | ||
@@ -30,13 +99,6 @@ .reduce(function(sections, line) { | ||
var tags = source.reduce(function(tags, tag) { | ||
var matchs = tag.value.match(/@(\S+)(?:\s+\{([^\}]+)\})?(?:\s+(\S+))?(?:\s+([^$]+))?/); | ||
var tag_node = parse_tag_line(tag.value); | ||
if (!tag_node) { return tags; } | ||
if (!matchs) { return tags; } | ||
var tag_node = { | ||
tag : matchs[1], | ||
line : Number(tag.line), | ||
type : matchs[2] || '', | ||
name : matchs[3] || '', | ||
description : matchs[4] || '' | ||
}; | ||
tag_node.line = Number(tag.line); | ||
if (opts.raw_value) { | ||
@@ -43,0 +105,0 @@ tag_node.value = tag.value; |
{ | ||
"name": "comment-parser", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Generic JSDoc-like comment parser. ", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -61,2 +61,4 @@ comment-parser | ||
You can also make raw line available in parsed results by passing `opts.raw_value = true`. | ||
Invalid comment blocks are skipped. Comments starting with `/*` and `/***` are considered not valid. | ||
@@ -66,2 +68,9 @@ | ||
Happy coding :) | ||
Happy coding :) | ||
Contributors | ||
============ | ||
- [Sergii Iavorskyi](https://github.com/yavorskiy) | ||
- [Alexej Yaroshevich](https://github.com/zxqfox) |
@@ -270,2 +270,41 @@ var fs = require('fs'); | ||
}); | ||
it('should parse complex types `@tag {{a: type}} name`', function() { | ||
expect(parsed(function(){ | ||
/** | ||
* @my-tag {{a: number}} name | ||
*/ | ||
})[0]) | ||
.to.eql({ | ||
line: 0, | ||
description: '', | ||
tags: [{ | ||
tag : 'my-tag', | ||
line : 1, | ||
type : '{a: number}', | ||
name : 'name', | ||
description : '' | ||
}] | ||
}); | ||
}); | ||
it('should gracefully fail on syntax errors `@tag {{a: type} name`', function() { | ||
expect(parsed(function(){ | ||
/** | ||
* @my-tag {{a: number} name | ||
*/ | ||
})[0]) | ||
.to.eql({ | ||
line: 0, | ||
description: '', | ||
tags: [{ | ||
tag : 'my-tag', | ||
line : 1, | ||
type : '', | ||
name : '', | ||
description : '{{a: number} name', | ||
error : 'Unpaired curly in type doc' | ||
}] | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28662
890
74