/tags/jsdoc_toolkit-2.0.1/jsdoc-toolkit/app/lib/JSDOC/DocComment.js

http://jsdoc-toolkit.googlecode.com/ · JavaScript · 200 lines · 58 code · 22 blank · 120 comment · 20 complexity · 1760dcdda36dc1a3610a4cfa6cd767a0 MD5 · raw file

  1. if (typeof JSDOC == "undefined") JSDOC = {};
  2. /**
  3. Create a new DocComment. This takes a raw documentation comment,
  4. and wraps it in useful accessors.
  5. @class Represents a documentation comment object.
  6. */
  7. JSDOC.DocComment = function(/**String*/comment) {
  8. this.init();
  9. if (typeof comment != "undefined") {
  10. this.parse(comment);
  11. }
  12. }
  13. JSDOC.DocComment.prototype.init = function() {
  14. this.isUserComment = true;
  15. this.src = "";
  16. this.meta = "";
  17. this.tagTexts = [];
  18. this.tags = [];
  19. }
  20. /**
  21. @requires JSDOC.DocTag
  22. */
  23. JSDOC.DocComment.prototype.parse = function(/**String*/comment) {
  24. if (comment == "") {
  25. comment = "/** @desc */";
  26. this.isUserComment = false;
  27. }
  28. this.src = JSDOC.DocComment.unwrapComment(comment);
  29. this.meta = "";
  30. if (this.src.indexOf("#") == 0) {
  31. this.src.match(/#(.+[+-])([\s\S]*)$/);
  32. if (RegExp.$1) this.meta = RegExp.$1;
  33. if (RegExp.$2) this.src = RegExp.$2;
  34. }
  35. this.fixDesc();
  36. if (typeof JSDOC.PluginManager != "undefined") {
  37. JSDOC.PluginManager.run("onDocCommentSrc", this);
  38. }
  39. this.src = JSDOC.DocComment.shared+"\n"+this.src;
  40. this.tagTexts =
  41. this.src
  42. .split(/(^|[\r\n])\s*@/)
  43. .filter(function($){return $.match(/\S/)});
  44. /**
  45. The tags found in the comment.
  46. @type JSDOC.DocTag[]
  47. */
  48. this.tags = this.tagTexts.map(function($){return new JSDOC.DocTag($)});
  49. if (typeof JSDOC.PluginManager != "undefined") {
  50. JSDOC.PluginManager.run("onDocCommentTags", this);
  51. }
  52. }
  53. /*t:
  54. plan(5, "testing JSDOC.DocComment");
  55. requires("../frame/String.js");
  56. requires("../lib/JSDOC/DocTag.js");
  57. var com = new JSDOC.DocComment("/**@foo some\n* comment here*"+"/");
  58. is(com.tagTexts[0], "foo some\ncomment here", "first tag text is found.");
  59. is(com.tags[0].title, "foo", "the title is found in a comment with one tag.");
  60. var com = new JSDOC.DocComment("/** @foo first\n* @bar second*"+"/");
  61. is(com.getTag("bar").length, 1, "getTag() returns one tag by that title.");
  62. JSDOC.DocComment.shared = "@author John Smith";
  63. var com = new JSDOC.DocComment("/**@foo some\n* comment here*"+"/");
  64. is(com.tags[0].title, "author", "shared comment is added.");
  65. is(com.tags[1].title, "foo", "shared comment is added to existing tag.");
  66. */
  67. /**
  68. If no @desc tag is provided, this function will add it.
  69. */
  70. JSDOC.DocComment.prototype.fixDesc = function() {
  71. if (this.meta && this.meta != "@+") return;
  72. if (/^\s*[^@\s]/.test(this.src)) {
  73. this.src = "@desc "+this.src;
  74. }
  75. }
  76. /*t:
  77. plan(5, "testing JSDOC.DocComment#fixDesc");
  78. var com = new JSDOC.DocComment();
  79. com.src = "this is a desc\n@author foo";
  80. com.fixDesc();
  81. is(com.src, "@desc this is a desc\n@author foo", "if no @desc tag is provided one is added.");
  82. com.src = "x";
  83. com.fixDesc();
  84. is(com.src, "@desc x", "if no @desc tag is provided one is added to a single character.");
  85. com.src = "\nx";
  86. com.fixDesc();
  87. is(com.src, "@desc \nx", "if no @desc tag is provided one is added to return and character.");
  88. com.src = " ";
  89. com.fixDesc();
  90. is(com.src, " ", "if no @desc tag is provided one is not added to just whitespace.");
  91. com.src = "";
  92. com.fixDesc();
  93. is(com.src, "", "if no @desc tag is provided one is not added to empty.");
  94. */
  95. /**
  96. Remove slash-star comment wrapper from a raw comment string.
  97. @type String
  98. */
  99. JSDOC.DocComment.unwrapComment = function(/**String*/comment) {
  100. if (!comment) return "";
  101. var unwrapped = comment.replace(/(^\/\*\*|\*\/$)/g, "").replace(/^\s*\* ?/gm, "");
  102. return unwrapped;
  103. }
  104. /*t:
  105. plan(5, "testing JSDOC.DocComment.unwrapComment");
  106. var com = "/**x*"+"/";
  107. var unwrapped = JSDOC.DocComment.unwrapComment(com);
  108. is(unwrapped, "x", "a single character jsdoc is found.");
  109. com = "/***x*"+"/";
  110. unwrapped = JSDOC.DocComment.unwrapComment(com);
  111. is(unwrapped, "x", "three stars are allowed in the opener.");
  112. com = "/****x*"+"/";
  113. unwrapped = JSDOC.DocComment.unwrapComment(com);
  114. is(unwrapped, "*x", "fourth star in the opener is kept.");
  115. com = "/**x\n * y\n*"+"/";
  116. unwrapped = JSDOC.DocComment.unwrapComment(com);
  117. is(unwrapped, "x\ny\n", "leading stars and spaces are trimmed.");
  118. com = "/**x\n * y\n*"+"/";
  119. unwrapped = JSDOC.DocComment.unwrapComment(com);
  120. is(unwrapped, "x\n y\n", "only first space after leading stars are trimmed.");
  121. */
  122. /**
  123. Provides a printable version of the comment.
  124. @type String
  125. */
  126. JSDOC.DocComment.prototype.toString = function() {
  127. return this.src;
  128. }
  129. /*t:
  130. plan(1, "testing JSDOC.DocComment#fixDesc");
  131. var com = new JSDOC.DocComment();
  132. com.src = "foo";
  133. is(""+com, "foo", "stringifying a comment returns the unwrapped src.");
  134. */
  135. /**
  136. Given the title of a tag, returns all tags that have that title.
  137. @type JSDOC.DocTag[]
  138. */
  139. JSDOC.DocComment.prototype.getTag = function(/**String*/tagTitle) {
  140. return this.tags.filter(function($){return $.title == tagTitle});
  141. }
  142. /*t:
  143. plan(1, "testing JSDOC.DocComment#getTag");
  144. requires("../frame/String.js");
  145. requires("../lib/JSDOC/DocTag.js");
  146. var com = new JSDOC.DocComment("/**@foo some\n* @bar\n* @bar*"+"/");
  147. is(com.getTag("bar").length, 2, "getTag returns expected number of tags.");
  148. */
  149. /**
  150. Used to store the currently shared tag text.
  151. */
  152. JSDOC.DocComment.shared = "";
  153. /*t:
  154. plan(2, "testing JSDOC.DocComment.shared");
  155. requires("../frame/String.js");
  156. requires("../lib/JSDOC/DocTag.js");
  157. JSDOC.DocComment.shared = "@author Michael";
  158. var com = new JSDOC.DocComment("/**@foo\n* @foo*"+"/");
  159. is(com.getTag("author").length, 1, "getTag returns shared tag.");
  160. is(com.getTag("foo").length, 2, "getTag returns unshared tags too.");
  161. */