1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
if (typeof JSDOC == "undefined") JSDOC = {};
/**
@constructor
*/
JSDOC.DocTag = function(src) {
this.init();
if (typeof src != "undefined") {
this.parse(src);
}
}
/**
Create and initialize the properties of this.
*/
JSDOC.DocTag.prototype.init = function() {
this.title = "";
this.type = "";
this.name = "";
this.isOptional = false;
this.defaultValue = "";
this.desc = "";
return this;
}
/**
Populate the properties of this from the given tag src.
@param {string} src
*/
JSDOC.DocTag.prototype.parse = function(src) {
if (typeof src != "string") throw "src must be a string not "+(typeof src);
try {
src = this.nibbleTitle(src);
if (JSDOC.PluginManager) {
JSDOC.PluginManager.run("onDocTagSynonym", this);
}
src = this.nibbleType(src);
// only some tags are allowed to have names.
if (this.title == "param" || this.title == "property" || this.title == "config") { // @config is deprecated
src = this.nibbleName(src);
}
}
catch(e) {
if (LOG) LOG.warn(e);
else throw e;
}
this.desc = src; // whatever is left
// example tags need to have whitespace preserved
if (this.title != "example") this.desc = this.desc.trim();
if (JSDOC.PluginManager) {
JSDOC.PluginManager.run("onDocTag", this);
}
}
/**
Automatically called when this is stringified.
*/
JSDOC.DocTag.prototype.toString = function() {
return this.desc;
}
/*t:
plan(1, "testing JSDOC.DocTag#toString");
var tag = new JSDOC.DocTag("param {object} date A valid date.");
is(""+tag, "A valid date.", "stringifying a tag returns the desc.");
*/
/**
Find and shift off the title of a tag.
@param {string} src
@return src
*/
JSDOC.DocTag.prototype.nibbleTitle = function(src) {
if (typeof src != "string") throw "src must be a string not "+(typeof src);
var parts = src.match(/^\s*(\S+)(?:\s([\s\S]*))?$/);
if (parts && parts[1]) this.title = parts[1];
if (parts && parts[2]) src = parts[2];
else src = "";
return src;
}
/*t:
plan(8, "testing JSDOC.DocTag#nibbleTitle");
var tag = new JSDOC.DocTag();
tag.init().nibbleTitle("aTitleGoesHere");
is(tag.title, "aTitleGoesHere", "a title can be found in a single-word string.");
var src = tag.init().nibbleTitle("aTitleGoesHere and the rest");
is(tag.title, "aTitleGoesHere", "a title can be found in a multi-word string.");
is(src, "and the rest", "the rest is returned when the title is nibbled off.");
src = tag.init().nibbleTitle("");
is(tag.title, "", "given an empty string the title is empty.");
is(src, "", "the rest is empty when the tag is empty.");
var src = tag.init().nibbleTitle(" aTitleGoesHere\n a description");
is(tag.title, "aTitleGoesHere", "leading and trailing spaces are not part of the title.");
is(src, " a description", "leading spaces (less one) are part of the description.");
tag.init().nibbleTitle("a.Title::Goes_Here foo");
is(tag.title, "a.Title::Goes_Here", "titles with punctuation are allowed.");
*/
/**
Find and shift off the type of a tag.
@requires frame/String.js
@param {string} src
@return src
*/
JSDOC.DocTag.prototype.nibbleType = function(src) {
if (typeof src != "string") throw "src must be a string not "+(typeof src);
if (src.match(/^\s*\{/)) {
var typeRange = src.balance("{", "}");
if (typeRange[1] == -1) {
throw "Malformed comment tag ignored. Tag type requires an opening { and a closing }: "+src;
}
this.type = src.substring(typeRange[0]+1, typeRange[1]).trim();
this.type = this.type.replace(/\s*,\s*/g, "|"); // multiples can be separated by , or |
src = src.substring(typeRange[1]+1);
}
return src;
}
/*t:
plan(5, "testing JSDOC.DocTag.parser.nibbleType");
requires("../frame/String.js");
var tag = new JSDOC.DocTag();
tag.init().nibbleType("{String[]} aliases");
is(tag.type, "String[]", "type can have non-alpha characters.");
tag.init().nibbleType("{ aTypeGoesHere } etc etc");
is(tag.type, "aTypeGoesHere", "type is trimmed.");
tag.init().nibbleType("{ oneType, twoType ,\n threeType } etc etc");
is(tag.type, "oneType|twoType|threeType", "multiple types can be separated by commas.");
var error;
try { tag.init().nibbleType("{widget foo"); }
catch(e) { error = e; }
is(typeof error, "string", "malformed tag type throws error.");
isnt(error.indexOf("Malformed"), -1, "error message tells tag is malformed.");
*/
/**
Find and shift off the name of a tag.
@requires frame/String.js
@param {string} src
@return src
*/
JSDOC.DocTag.prototype.nibbleName = function(src) {
if (typeof src != "string") throw "src must be a string not "+(typeof src);
src = src.trim();
// is optional?
if (src.charAt(0) == "[") {
var nameRange = src.balance("[", "]");
if (nameRange[1] == -1) {
throw "Malformed comment tag ignored. Tag optional name requires an opening [ and a closing ]: "+src;
}
this.name = src.substring(nameRange[0]+1, nameRange[1]).trim();
this.isOptional = true;
src = src.substring(nameRange[1]+1);
// has default value?
var nameAndValue = this.name.split("=");
if (nameAndValue.length) {
this.name = nameAndValue.shift().trim();
this.defaultValue = nameAndValue.join("=");
}
}
else {
var parts = src.match(/^(\S+)(?:\s([\s\S]*))?$/);
if (parts) {
if (parts[1]) this.name = parts[1];
if (parts[2]) src = parts[2].trim();
else src = "";
}
}
return src;
}
/*t:
requires("../frame/String.js");
plan(9, "testing JSDOC.DocTag.parser.nibbleName");
var tag = new JSDOC.DocTag();
tag.init().nibbleName("[foo] This is a description.");
is(tag.isOptional, true, "isOptional syntax is detected.");
is(tag.name, "foo", "optional param name is found.");
tag.init().nibbleName("[foo] This is a description.");
is(tag.isOptional, true, "isOptional syntax is detected when no type.");
is(tag.name, "foo", "optional param name is found when no type.");
tag.init().nibbleName("[foo=7] This is a description.");
is(tag.name, "foo", "optional param name is found when default value.");
is(tag.defaultValue, 7, "optional param default value is found when default value.");
//tag.init().nibbleName("[foo= a value] This is a description.");
//is(tag.defaultValue, " a value", "optional param default value is found when default value has spaces (issue #112).");
tag.init().nibbleName("[foo=[]] This is a description.");
is(tag.defaultValue, "[]", "optional param default value is found when default value is [] (issue #95).");
tag.init().nibbleName("[foo=a=b] This is a description.");
is(tag.name, "foo", "optional param name is found when default value is a=b.");
is(tag.defaultValue, "a=b", "optional param default value is found when default value is a=b.")
*/
/*t:
plan(32, "Testing JSDOC.DocTag.parser.");
requires("../frame/String.js");
var tag = new JSDOC.DocTag();
is(typeof tag, "object", "JSDOC.DocTag.parser with an empty string returns an object.");
is(typeof tag.title, "string", "returned object has a string property 'title'.");
is(typeof tag.type, "string", "returned object has a string property 'type'.");
is(typeof tag.name, "string", "returned object has a string property 'name'.");
is(typeof tag.defaultValue, "string", "returned object has a string property 'defaultValue'.");
is(typeof tag.isOptional, "boolean", "returned object has a boolean property 'isOptional'.");
is(typeof tag.desc, "string", "returned object has a string property 'desc'.");
tag = new JSDOC.DocTag("param {widget} foo");
is(tag.title, "param", "param title is found.");
is(tag.name, "foo", "param name is found when desc is missing.");
is(tag.desc, "", "param desc is empty when missing.");
tag = new JSDOC.DocTag("param {object} date A valid date.");
is(tag.name, "date", "param name is found with a type.");
is(tag.type, "object", "param type is found.");
is(tag.desc, "A valid date.", "param desc is found with a type.");
tag = new JSDOC.DocTag("param aName a description goes\n here.");
is(tag.name, "aName", "param name is found without a type.");
is(tag.desc, "a description goes\n here.", "param desc is found without a type.");
tag = new JSDOC.DocTag("param {widget}");
is(tag.name, "", "param name is empty when it is not given.");
tag = new JSDOC.DocTag("param {widget} [foo] This is a description.");
is(tag.name, "foo", "optional param name is found.");
tag = new JSDOC.DocTag("return {aType} This is a description.");
is(tag.type, "aType", "when return tag has no name, type is found.");
is(tag.desc, "This is a description.", "when return tag has no name, desc is found.");
tag = new JSDOC.DocTag("author Joe Coder <jcoder@example.com>");
is(tag.title, "author", "author tag has a title.");
is(tag.type, "", "the author tag has no type.");
is(tag.name, "", "the author tag has no name.");
is(tag.desc, "Joe Coder <jcoder@example.com>", "author tag has desc.");
tag = new JSDOC.DocTag("private \t\n ");
is(tag.title, "private", "private tag has a title.");
is(tag.type, "", "the private tag has no type.");
is(tag.name, "", "the private tag has no name.");
is(tag.desc, "", "private tag has no desc.");
tag = new JSDOC.DocTag("example\n example(code);\n more();");
is(tag.desc, " example(code);\n more();", "leading whitespace (less one) in examples code is preserved.");
tag = new JSDOC.DocTag("param theName \n");
is(tag.name, "theName", "name only is found.");
tag = new JSDOC.DocTag("type theDesc \n");
is(tag.desc, "theDesc", "desc only is found.");
tag = new JSDOC.DocTag("type {theType} \n");
is(tag.type, "theType", "type only is found.");
tag = new JSDOC.DocTag("");
is(tag.title, "", "title is empty when tag is empty.");
*/
|