comment-parser
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -0,1 +1,4 @@ | ||
# v0.3.2 | ||
- fix RegExp for `description` extraction to allow $ char | ||
# v0.3.1 | ||
@@ -42,2 +45,2 @@ - use `readable-stream` fro Node 0.8 comatibility | ||
Initial implementation | ||
Initial implementation |
{ | ||
"name": "comment-parser", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "Generic JSDoc-like comment parser. ", | ||
@@ -21,3 +21,3 @@ "main": "index.js", | ||
"test": "jshint --reporter node_modules/jshint-stylish/stylish.js index.js && mocha tests/*", | ||
"watch": "nodemon -q -w index.js -w tests/ -x npm test" | ||
"watch": "nodemon -q -w index.js -w parser.js -w tests/ -x npm test" | ||
}, | ||
@@ -24,0 +24,0 @@ "repository": { |
var RE_COMMENT_START = /^\s*\/\*\*\s*$/m; | ||
var RE_COMMENT_LINE = /^\s*\*(?:\s|$)/m; | ||
var RE_COMMENT_LINE = /^\s*\*(?:\s(\s*)|$)/m; | ||
var RE_COMMENT_END = /^\s*\*\/\s*$/m; | ||
@@ -118,3 +118,3 @@ var RE_COMMENT_1LINE = /^\s*\/\*\*\s*(.*)\s*\*\/\s*$/; | ||
var result = str.match(/^\s+([^$]+)?/); | ||
var result = str.match(/^\s+((.|\s)+)?/); | ||
@@ -147,6 +147,3 @@ if (result) { | ||
result = parser(state.source, merge({}, state.data)); | ||
// console.log('----------------'); | ||
// console.log(parser.name, ':', result); | ||
} catch (err) { | ||
// console.warn('Parser "%s" failed: %s', parser.name, err.message); | ||
state.data.errors = (state.data.errors || []) | ||
@@ -180,7 +177,12 @@ .concat(parser.name + ': ' + err.message); | ||
function trim(s) { | ||
return opts.trim ? s.trim() : s; | ||
} | ||
var source_str = source | ||
.map(function(line) { return line.source; }) | ||
.join('\n') | ||
.trim(); | ||
.map(function(line) { return trim(line.source); }) | ||
.join('\n'); | ||
source_str = trim(source_str); | ||
var start = source[0].number; | ||
@@ -192,5 +194,5 @@ | ||
.reduce(function(tags, line) { | ||
line.source = line.source.trim(); | ||
line.source = trim(line.source); | ||
if (line.source.match(/^@(\w+)/)) { | ||
if (line.source.match(/^\s*@(\w+)/)) { | ||
tags.push({source: [line.source], line: line.number}); | ||
@@ -205,3 +207,3 @@ } else { | ||
.map(function(tag) { | ||
tag.source = tag.source.join('\n').trim(); | ||
tag.source = trim(tag.source.join('\n')); | ||
return tag; | ||
@@ -219,8 +221,3 @@ }); | ||
var tags = source.reduce(function(tags, tag) { | ||
var tag_node = parse_tag(tag.source, opts.parsers || [ | ||
PARSERS.parse_tag, | ||
PARSERS.parse_type, | ||
PARSERS.parse_name, | ||
PARSERS.parse_description | ||
]); | ||
var tag_node = parse_tag(tag.source, opts.parsers); | ||
@@ -267,6 +264,3 @@ if (!tag_node) { return tags; } | ||
}, []); | ||
// console.log('-----------'); | ||
// console.log(description, tags); | ||
return { | ||
@@ -284,6 +278,16 @@ tags : tags, | ||
function mkextract(opts) { | ||
var chunk = null; | ||
var number = 0; | ||
opts = merge({}, { | ||
trim : true, | ||
dotted_names : false, | ||
parsers : [ | ||
PARSERS.parse_tag, | ||
PARSERS.parse_type, | ||
PARSERS.parse_name, | ||
PARSERS.parse_description | ||
] | ||
}, opts || {}); | ||
/** | ||
@@ -298,4 +302,2 @@ * Cumulatively reading lines until they make one comment block | ||
if (line.match(RE_COMMENT_1LINE)) { | ||
// console.log('line (1)', line); | ||
// console.log(' clean:', line.replace(RE_COMMENT_1LINE, '$1')); | ||
return parse_block([{ | ||
@@ -311,4 +313,2 @@ source: line.replace(RE_COMMENT_1LINE, '$1'), | ||
if (line.match(RE_COMMENT_START)) { | ||
// console.log('line (1)', line); | ||
// console.log(' clean:', line.replace(RE_COMMENT_START, '')); | ||
chunk = [{source: line.replace(RE_COMMENT_START, ''), number: number - 1}]; | ||
@@ -321,5 +321,6 @@ return null; | ||
if (chunk && line.match(RE_COMMENT_LINE)) { | ||
// console.log('line (2)', line); | ||
// console.log(' clean:', line.replace(RE_COMMENT_LINE, '')); | ||
chunk.push({source: line.replace(RE_COMMENT_LINE, ''), number: number - 1}); | ||
chunk.push({ | ||
number: number - 1, | ||
source: line.replace(RE_COMMENT_LINE, opts.trim ? '' : '$1') | ||
}); | ||
return null; | ||
@@ -331,4 +332,2 @@ } | ||
if (chunk && line.match(RE_COMMENT_END)) { | ||
// console.log('line (3)', line); | ||
// console.log(' clean:', line.replace(RE_COMMENT_END, '')); | ||
chunk.push({source: line.replace(RE_COMMENT_END, ''), number: number - 1}); | ||
@@ -347,4 +346,2 @@ return parse_block(chunk, opts); | ||
module.exports = function parse(source, opts) { | ||
opts = opts || {}; | ||
var block; | ||
@@ -366,2 +363,2 @@ var blocks = []; | ||
module.exports.PARSERS = PARSERS; | ||
module.exports.mkextract = mkextract; | ||
module.exports.mkextract = mkextract; |
@@ -77,3 +77,3 @@ # comment-parser | ||
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}`. | ||
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. | ||
@@ -90,8 +90,8 @@ | ||
// takes entire string | ||
function parse_tag(str, data) { | ||
return {source: ' @tag', data: {tag: 'tag'}}; | ||
}, | ||
function parse_tag(str, data) { | ||
return {source: ' @tag', data: {tag: 'tag'}}; | ||
}, | ||
// parser throwing exception | ||
function check_tag(str, data) { | ||
if (allowed_tags.indexOf(data.tag) === -1) { | ||
if (allowed_tags.indexOf(data.tag) === -1) { | ||
throw new Error('Unrecognized tag "' + data.tag + '"'); | ||
@@ -101,9 +101,9 @@ } | ||
// takes the rest of the string after ' @tag'' | ||
function parse_name1(str, data) { | ||
return {source: ' name', data: {name: 'name1'}}; | ||
function parse_name1(str, data) { | ||
return {source: ' name', data: {name: 'name1'}}; | ||
}, | ||
// alternative name parser | ||
function parse_name2(str, data) { | ||
return {source: ' name', data: {name: 'name2'}}; | ||
} | ||
function parse_name2(str, data) { | ||
return {source: ' name', data: {name: 'name2'}}; | ||
} | ||
]}); | ||
@@ -141,7 +141,8 @@ ``` | ||
- [Sergii Iavorskyi](https://github.com/yavorskiy) | ||
- [Alexej Yaroshevich](https://github.com/zxqfox) | ||
- [Evgeny Reznichenko](https://github.com/zxcabs) | ||
- [Jordan Harband](https://github.com/ljharb) | ||
- [Sergii Iavorskyi](https://github.com/yavorskiy) | ||
Happy coding :) |
@@ -13,3 +13,3 @@ var fs = require('fs'); | ||
* 2 } | ||
* | ||
* | ||
*/ | ||
@@ -43,7 +43,7 @@ | ||
* | ||
* | ||
* | ||
* Description first line | ||
* | ||
* Description second line | ||
* | ||
* | ||
*/ | ||
@@ -64,3 +64,3 @@ var a; | ||
/** | ||
* | ||
* | ||
*/ | ||
@@ -138,2 +138,24 @@ var a; | ||
it('should preserve empty lines and indentation with `opts.trim = false`', function() { | ||
expect(parsed(function(){ | ||
/** | ||
* | ||
* | ||
* Description first line | ||
* second line | ||
* | ||
* third line | ||
*/ | ||
var a; | ||
}, { | ||
trim: false | ||
})[0]) | ||
.eql({ | ||
description : '\n\n\n Description first line\n second line\n\n third line\n', | ||
source : '\n\n\n Description first line\n second line\n\n third line\n', | ||
line : 1, | ||
tags : [] | ||
}); | ||
}); | ||
it('should parse one line block with tag', function() { | ||
@@ -163,3 +185,3 @@ expect(parsed(function(){ | ||
/** | ||
* | ||
* | ||
* @my-tag | ||
@@ -238,3 +260,3 @@ */ | ||
line : 1, | ||
source : '@my-tag {my.type} name', | ||
source : '@my-tag {my.type} name', | ||
description : '', | ||
@@ -246,3 +268,3 @@ tags: [{ | ||
name : 'name', | ||
source : '@my-tag {my.type} name', | ||
source : '@my-tag {my.type} name', | ||
description : '', | ||
@@ -269,3 +291,3 @@ optional : false | ||
name : 'name', | ||
source : '@my-tag {my.type} name description', | ||
source : '@my-tag {my.type} name description', | ||
description : 'description', | ||
@@ -437,3 +459,3 @@ optional : false | ||
it('should parse optionalrest names `@tag [...name] desc`', function() { | ||
it('should parse optional rest names `@tag [...name] desc`', function() { | ||
expect(parsed(function(){ | ||
@@ -579,2 +601,24 @@ /** | ||
}); | ||
it('parses $ in description`', function() { | ||
expect(parsed(function(){ | ||
/** | ||
* @my-tag {String} name description with $ char | ||
*/ | ||
})[0]) | ||
.to.eql({ | ||
line : 1, | ||
source : '@my-tag {String} name description with $ char', | ||
description : '', | ||
tags: [{ | ||
tag : 'my-tag', | ||
line : 2, | ||
type : 'String', | ||
name : 'name', | ||
source : '@my-tag {String} name description with $ char', | ||
optional : false, | ||
description : 'description with $ char' | ||
}] | ||
}); | ||
}); | ||
}); |
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
39522
1112
145