Permalink
Browse files

Merge branch 'master' of github.com:visionmedia/dox

  • Loading branch information...
2 parents d2544d4 + acb29db commit 87ef976345421e4a6168862e4c52cea4b921cebb @tj committed Oct 1, 2012
Showing with 59 additions and 7 deletions.
  1. +21 −6 lib/dox.js
  2. +28 −1 test/dox.test.js
  3. +7 −0 test/fixtures/singleline.js
  4. +3 −0 test/fixtures/uncommented.js
View
@@ -1,4 +1,3 @@
-
/*!
* Module dependencies.
*/
@@ -24,18 +23,20 @@ exports.version = '0.3.1';
exports.parseComments = function(js, options){
options = options || {};
+ js = js.replace(/\r\n/gm, '\n');
var comments = []
, raw = options.raw
, comment
, buf = ''
, ignore
- , within
+ , withinMultiline = false
+ , withinSingle = false
, code;
for (var i = 0, len = js.length; i < len; ++i) {
// start comment
- if (!within && '/' == js[i] && '*' == js[i+1]) {
+ if (!withinMultiline && !withinSingle && '/' == js[i] && '*' == js[i+1]) {
// code following previous comment
if (buf.trim().length) {
comment = comments[comments.length - 1];
@@ -46,23 +47,37 @@ exports.parseComments = function(js, options){
buf = '';
}
i += 2;
- within = true;
+ withinMultiline = true;
ignore = '!' == js[i];
// end comment
- } else if (within && '*' == js[i] && '/' == js[i+1]) {
+ } else if (withinMultiline && !withinSingle && '*' == js[i] && '/' == js[i+1]) {
i += 2;
buf = buf.replace(/^ *\* ?/gm, '');
var comment = exports.parseComment(buf, options);
comment.ignore = ignore;
comments.push(comment);
- within = ignore = false;
+ withinMultiline = ignore = false;
buf = '';
+ } else if (!withinSingle && !withinMultiline && '/' == js[i] && '/' == js[i+1]) {
+ withinSingle = true;
+ buf += js[i];
+ } else if (withinSingle && !withinMultiline && '\n' == js[i]) {
+ withinSingle = false;
+ buf += js[i];
// buffer comment or code
} else {
buf += js[i];
}
}
+ if (comments.length === 0) {
+ comments.push({
+ tags: [],
+ description: {full: '', summary: '', body: ''},
+ isPrivate: false
+ });
+ }
+
// trailing code
if (buf.trim().length) {
comment = comments[comments.length - 1];
View
@@ -110,6 +110,15 @@ module.exports = {
parseComment.description.full.should.equal('<p>Parse the given comment <code>str</code>.</p>\n\n<h2>The comment object returned contains the following</h2>\n\n<ul>\n<li><code>tags</code> array of tag objects</li>\n<li><code>description</code> the first line of the comment</li>\n<li><code>body</code> lines following the description</li>\n<li><code>content</code> both the description and the body</li>\n<li><code>isPrivate</code> true when "@api private" is used</li>\n</ul>');
parseComment.description.body.should.equal('<h2>The comment object returned contains the following</h2>\n\n<ul>\n<li><code>tags</code> array of tag objects</li>\n<li><code>description</code> the first line of the comment</li>\n<li><code>body</code> lines following the description</li>\n<li><code>content</code> both the description and the body</li>\n<li><code>isPrivate</code> true when "@api private" is used</li>\n</ul>');
+ var parseTag = comments.shift();
+
+ // Should be the comment be parsed ?
+ var shouldNotFail = comments.shift();
+
+ var parseTagTypes = comments.shift();
+ parseTagTypes.tags.should.have.length(3);
+ parseTagTypes.description.full.should.equal('<p>Parse tag type string \"{Array|Object}\" etc.</p>');
+
var escape = comments.pop();
escape.tags.should.have.length(3);
escape.description.full.should.equal('<p>Escape the given <code>html</code>.</p>');
@@ -283,5 +292,23 @@ module.exports = {
var tag = dox.parseTag('@hello universe is better than world');
tag.type.should.equal('hello');
tag.string.should.equal('universe is better than world');
- }
+ },
+
+ 'test .parseComments() code with no comments': function(done){
+ fixture('uncommented.js', function(err, str){
+ var comments = dox.parseComments(str)
+ , all = comments.shift();
+ all.code.should.equal("function foo() {\n doSomething();\n}");
+ done();
+ });
+ },
+
+ 'test .parseComments() with a simple single line comment in code': function(done){
+ fixture('singleline.js', function(err, str){
+ var comments = dox.parseComments(str)
+ , all = comments.shift();
+ all.code.should.equal("function foo() {\n // Maybe useful\n doSomething();\n}");
+ done();
+ });
+ },
};
@@ -0,0 +1,7 @@
+/**
+ * Code below must contain the useful comment
+ */
+function foo() {
+ // Maybe useful
+ doSomething();
+}
@@ -0,0 +1,3 @@
+function foo() {
+ doSomething();
+}

0 comments on commit 87ef976

Please sign in to comment.