Skip to content
This repository
Browse code

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

  • Loading branch information...
commit 87ef976345421e4a6168862e4c52cea4b921cebb 2 parents d2544d4 + acb29db
TJ Holowaychuk authored October 01, 2012
27  lib/dox.js
... ...
@@ -1,4 +1,3 @@
1  
-
2 1
 /*!
3 2
  * Module dependencies.
4 3
  */
@@ -24,18 +23,20 @@ exports.version = '0.3.1';
24 23
 
25 24
 exports.parseComments = function(js, options){
26 25
   options = options || {};
  26
+  js = js.replace(/\r\n/gm, '\n');
27 27
 
28 28
   var comments = []
29 29
     , raw = options.raw
30 30
     , comment
31 31
     , buf = ''
32 32
     , ignore
33  
-    , within
  33
+    , withinMultiline = false
  34
+    , withinSingle = false
34 35
     , code;
35 36
 
36 37
   for (var i = 0, len = js.length; i < len; ++i) {
37 38
     // start comment
38  
-    if (!within && '/' == js[i] && '*' == js[i+1]) {
  39
+    if (!withinMultiline && !withinSingle && '/' == js[i] && '*' == js[i+1]) {
39 40
       // code following previous comment
40 41
       if (buf.trim().length) {
41 42
         comment = comments[comments.length - 1];
@@ -46,23 +47,37 @@ exports.parseComments = function(js, options){
46 47
         buf = '';
47 48
       }
48 49
       i += 2;
49  
-      within = true;
  50
+      withinMultiline = true;
50 51
       ignore = '!' == js[i];
51 52
     // end comment
52  
-    } else if (within && '*' == js[i] && '/' == js[i+1]) {
  53
+    } else if (withinMultiline && !withinSingle && '*' == js[i] && '/' == js[i+1]) {
53 54
       i += 2;
54 55
       buf = buf.replace(/^ *\* ?/gm, '');
55 56
       var comment = exports.parseComment(buf, options);
56 57
       comment.ignore = ignore;
57 58
       comments.push(comment);
58  
-      within = ignore = false;
  59
+      withinMultiline = ignore = false;
59 60
       buf = '';
  61
+    } else if (!withinSingle && !withinMultiline && '/' == js[i] && '/' == js[i+1]) {
  62
+      withinSingle = true;
  63
+      buf += js[i];
  64
+    } else if (withinSingle && !withinMultiline && '\n' == js[i]) {
  65
+      withinSingle = false;
  66
+      buf += js[i];
60 67
     // buffer comment or code
61 68
     } else {
62 69
       buf += js[i];
63 70
     }
64 71
   }
65 72
 
  73
+  if (comments.length === 0) {
  74
+    comments.push({
  75
+      tags: [],
  76
+      description: {full: '', summary: '', body: ''},
  77
+      isPrivate: false
  78
+    });
  79
+  }
  80
+
66 81
   // trailing code
67 82
   if (buf.trim().length) {
68 83
     comment = comments[comments.length - 1];
29  test/dox.test.js
@@ -110,6 +110,15 @@ module.exports = {
110 110
       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>');
111 111
       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>');
112 112
 
  113
+      var parseTag = comments.shift();
  114
+      
  115
+      // Should be the comment be parsed ?
  116
+      var shouldNotFail = comments.shift();
  117
+
  118
+      var parseTagTypes = comments.shift();
  119
+      parseTagTypes.tags.should.have.length(3);
  120
+      parseTagTypes.description.full.should.equal('<p>Parse tag type string \"{Array|Object}\" etc.</p>');
  121
+      
113 122
       var escape = comments.pop();
114 123
       escape.tags.should.have.length(3);
115 124
       escape.description.full.should.equal('<p>Escape the given <code>html</code>.</p>');
@@ -283,5 +292,23 @@ module.exports = {
283 292
     var tag = dox.parseTag('@hello universe is better than world');
284 293
     tag.type.should.equal('hello');
285 294
     tag.string.should.equal('universe is better than world');
286  
-  }
  295
+  },
  296
+
  297
+  'test .parseComments() code with no comments': function(done){
  298
+    fixture('uncommented.js', function(err, str){
  299
+      var comments = dox.parseComments(str)
  300
+        , all = comments.shift();
  301
+      all.code.should.equal("function foo() {\n  doSomething();\n}");
  302
+      done();
  303
+    });
  304
+  },
  305
+
  306
+  'test .parseComments() with a simple single line comment in code': function(done){
  307
+    fixture('singleline.js', function(err, str){
  308
+      var comments = dox.parseComments(str)
  309
+        , all = comments.shift();
  310
+      all.code.should.equal("function foo() {\n  // Maybe useful\n  doSomething();\n}");
  311
+      done();
  312
+    });
  313
+  },
287 314
 };
7  test/fixtures/singleline.js
... ...
@@ -0,0 +1,7 @@
  1
+/**
  2
+ * Code below must contain the useful comment
  3
+ */
  4
+function foo() {
  5
+  // Maybe useful
  6
+  doSomething();
  7
+}
3  test/fixtures/uncommented.js
... ...
@@ -0,0 +1,3 @@
  1
+function foo() {
  2
+  doSomething();
  3
+}

0 notes on commit 87ef976

Please sign in to comment.
Something went wrong with that request. Please try again.