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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 405 405 1 4222 4 4218 1 2738 2738 2738 2738 2738 2738 2738 2738 1042 2738 2738 1 2738 437 437 110 327 1 326 2 2 2 324 54 270 84 2301 2738 906 2738 387 2738 829 2738 10 10 2728 383 2 381 2345 154 3 3 151 136 136 136 136 1 154 154 2738 2 2738 1679 1 103 103 103 103 95 103 1 3 1 6 6 6 2 4 1 3 5 5 2 3 1 2743 2743 2743 2743 2743 2743 2743 2743 2743 2743 27 27 8 8 27 27 27 2743 2743 9 9 9 9 6 9 4 2734 1088 1088 1088 1088 2743 4 4 2743 2743 27 27 27 27 2743 1 2743 1 60 1 2 2 2 1 1 1 72 72 72 72 72 734 734 734 102 102 10 632 11 621 76 545 73 70 72 2 70 70 1 344 344 72 72 70 274 274 | /** A collection of functions relating to JSDoc symbol name manipulation. @module jsdoc/name @author Michael Mathews <micmath@gmail.com> @license Apache License 2.0 - See file 'LICENSE.md' in this project. */ 'use strict'; var _ = require('underscore'); var escape = require('escape-string-regexp'); var hasOwnProp = Object.prototype.hasOwnProperty; /** * Longnames that have a special meaning in JSDoc. * * @enum {string} * @static * @memberof module:jsdoc/name */ var LONGNAMES = exports.LONGNAMES = { /** Longname used for doclets that do not have a longname, such as anonymous functions. */ ANONYMOUS: '<anonymous>', /** Longname that represents global scope. */ GLOBAL: '<global>' }; // Module namespace prefix. var MODULE_NAMESPACE = 'module:'; /** * Names and punctuation marks that identify doclet scopes. * * @enum {string} * @static * @memberof module:jsdoc/name */ var SCOPE = exports.SCOPE = { NAMES: { GLOBAL: 'global', INNER: 'inner', INSTANCE: 'instance', STATIC: 'static' }, PUNC: { INNER: '~', INSTANCE: '#', STATIC: '.' } }; // For backwards compatibility, this enum must use lower-case keys var scopeToPunc = exports.scopeToPunc = { 'inner': SCOPE.PUNC.INNER, 'instance': SCOPE.PUNC.INSTANCE, 'static': SCOPE.PUNC.STATIC }; var puncToScope = exports.puncToScope = _.invert(scopeToPunc); var DEFAULT_SCOPE = SCOPE.NAMES.STATIC; var SCOPE_PUNC = _.values(SCOPE.PUNC); var SCOPE_PUNC_STRING = '[' + SCOPE_PUNC.join() + ']'; var REGEXP_LEADING_SCOPE = new RegExp('^(' + SCOPE_PUNC_STRING + ')'); var REGEXP_TRAILING_SCOPE = new RegExp('(' + SCOPE_PUNC_STRING + ')$'); var DESCRIPTION = '(?:(?:[ \\t]*\\-\\s*|\\s+)(\\S[\\s\\S]*))?$'; var REGEXP_DESCRIPTION = new RegExp(DESCRIPTION); var REGEXP_NAME_DESCRIPTION = new RegExp('^(\\[[^\\]]+\\]|\\S+)' + DESCRIPTION); function nameIsLongname(name, memberof) { var regexp = new RegExp('^' + escape(memberof) + SCOPE_PUNC_STRING); return regexp.test(name); } function prototypeToPunc(name) { // don't mangle symbols named "prototype" if (name === 'prototype') { return name; } return name.replace(/(?:^|\.)prototype\.?/g, SCOPE.PUNC.INSTANCE); } // TODO: deprecate exports.resolve in favor of a better name /** Resolves the longname, memberof, variation and name values of the given doclet. @param {module:jsdoc/doclet.Doclet} doclet */ exports.resolve = function(doclet) { var about = {}; var memberof = doclet.memberof || ''; var metaName; var name = doclet.name ? String(doclet.name) : ''; var parentDoc; var puncAndName; var puncAndNameIndex; // change MyClass.prototype.instanceMethod to MyClass#instanceMethod // (but not in function params, which lack doclet.kind) // TODO: check for specific doclet.kind values (probably function, class, and module) if (name && doclet.kind) { name = prototypeToPunc(name); } doclet.name = name; // member of a var in an outer scope? if (name && !memberof && doclet.meta.code && doclet.meta.code.funcscope) { name = doclet.longname = doclet.meta.code.funcscope + SCOPE.PUNC.INNER + name; } if (memberof || doclet.forceMemberof) { // @memberof tag given memberof = prototypeToPunc(memberof); // the name is a complete longname, like @name foo.bar, @memberof foo if (name && nameIsLongname(name, memberof) && name !== memberof) { about = exports.shorten(name, (doclet.forceMemberof ? memberof : undefined)); } // the name and memberof are identical and refer to a module, // like @name module:foo, @memberof module:foo (probably a member like 'var exports') else if (name && name === memberof && name.indexOf(MODULE_NAMESPACE) === 0) { about = exports.shorten(name, (doclet.forceMemberof ? memberof : undefined)); } // the name and memberof are identical, like @name foo, @memberof foo else if (name && name === memberof) { doclet.scope = doclet.scope || DEFAULT_SCOPE; name = memberof + scopeToPunc[doclet.scope] + name; about = exports.shorten(name, (doclet.forceMemberof ? memberof : undefined)); } // like @memberof foo# or @memberof foo~ else if (name && REGEXP_TRAILING_SCOPE.test(memberof) ) { about = exports.shorten(memberof + name, (doclet.forceMemberof ? memberof : undefined)); } else if (name && doclet.scope) { about = exports.shorten(memberof + (scopeToPunc[doclet.scope] || '') + name, (doclet.forceMemberof ? memberof : undefined)); } } else { // no @memberof about = exports.shorten(name); } if (about.name) { doclet.name = about.name; } if (about.memberof) { doclet.setMemberof(about.memberof); } if (about.longname && (!doclet.longname || doclet.longname === doclet.name)) { doclet.setLongname(about.longname); } if (doclet.scope === SCOPE.NAMES.GLOBAL) { // via @global tag? doclet.setLongname(doclet.name); delete doclet.memberof; } else if (about.scope) { if (about.memberof === LONGNAMES.GLOBAL) { // via @memberof <global> ? doclet.scope = SCOPE.NAMES.GLOBAL; } else { doclet.scope = puncToScope[about.scope]; } } else if (doclet.name && doclet.memberof && !doclet.longname) { if ( REGEXP_LEADING_SCOPE.test(doclet.name) ) { doclet.scope = puncToScope[RegExp.$1]; doclet.name = doclet.name.substr(1); } else if (doclet.meta.code && doclet.meta.code.name) { // HACK: Handle cases where an ES 2015 class is a static memberof something else, and // the class has instance members. In these cases, we have to detect the instance // members' scope by looking at the meta info. There's almost certainly a better way to // do this... metaName = String(doclet.meta.code.name); puncAndName = SCOPE.PUNC.INSTANCE + doclet.name; puncAndNameIndex = metaName.indexOf(puncAndName); if ( puncAndNameIndex !== -1 && (puncAndNameIndex === metaName.length - puncAndName.length) ) { doclet.scope = SCOPE.NAMES.INSTANCE; } } doclet.scope = doclet.scope || DEFAULT_SCOPE; doclet.setLongname(doclet.memberof + scopeToPunc[doclet.scope] + doclet.name); } if (about.variation) { doclet.variation = about.variation; } // if we never found a longname, just use an empty string if (!doclet.longname) { doclet.longname = ''; } }; /** @method module:jsdoc/name.applyNamespace @param {string} longname The full longname of the symbol. @param {string} ns The namespace to be applied. @returns {string} The longname with the namespace applied. */ exports.applyNamespace = function(longname, ns) { var nameParts = exports.shorten(longname); var name = nameParts.name; longname = nameParts.longname; if ( !/^[a-zA-Z]+?:.+$/i.test(name) ) { longname = longname.replace( new RegExp(escape(name) + '$'), ns + ':' + name ); } return longname; }; // TODO: docs exports.stripNamespace = function(longname) { return longname.replace(/^[a-zA-Z]+:/, ''); }; /** * Check whether a parent longname is an ancestor of a child longname. * * @param {string} parent - The parent longname. * @param {string} child - The child longname. * @return {boolean} `true` if the parent is an ancestor of the child; otherwise, `false`. */ exports.hasAncestor = function(parent, child) { var hasAncestor = false; var memberof = child; if (!parent || !child) { return hasAncestor; } // fast path for obvious non-ancestors if (child.indexOf(parent) !== 0) { return hasAncestor; } do { memberof = exports.shorten(memberof).memberof; if (memberof === parent) { hasAncestor = true; } } while (!hasAncestor && memberof); return hasAncestor; }; // TODO: docs function atomize(longname, sliceChars, forcedMemberof) { var i; var memberof = ''; var name = ''; var parts; var partsRegExp; var scopePunc = ''; var token; var tokens = []; var variation; // quoted strings in a longname are atomic, so we convert them to tokens: // foo["bar"] => foo.@{1}@ // Foo.prototype["bar"] => Foo#@{1} longname = longname.replace(/(prototype|#)?(\[?["'].+?["']\]?)/g, function($, p1, p2) { var punc = ''; // is there a leading bracket? if ( /^\[/.test(p2) ) { // is it a static or instance member? punc = p1 ? SCOPE.PUNC.INSTANCE : SCOPE.PUNC.STATIC; p2 = p2.replace(/^\[/g, '') .replace(/\]$/g, ''); } token = '@{' + tokens.length + '}@'; tokens.push(p2); return punc + token; }); longname = prototypeToPunc(longname); if (typeof forcedMemberof !== 'undefined') { partsRegExp = new RegExp('^(.*?)([' + sliceChars.join() + ']?)$'); name = longname.substr(forcedMemberof.length); parts = forcedMemberof.match(partsRegExp); if (parts[1]) { memberof = parts[1] || forcedMemberof; } if (parts[2]) { scopePunc = parts[2]; } } else if (longname) { parts = (longname.match(new RegExp('^(:?(.+)([' + sliceChars.join() + ']))?(.+?)$')) || []) .reverse(); name = parts[0] || ''; scopePunc = parts[1] || ''; memberof = parts[2] || ''; } // like /** @name foo.bar(2) */ if ( /(.+)\(([^)]+)\)$/.test(name) ) { name = RegExp.$1; variation = RegExp.$2; } // restore quoted strings i = tokens.length; while (i--) { longname = longname.replace('@{' + i + '}@', tokens[i]); memberof = memberof.replace('@{' + i + '}@', tokens[i]); scopePunc = scopePunc.replace('@{' + i + '}@', tokens[i]); name = name.replace('@{' + i + '}@', tokens[i]); } return { longname: longname, memberof: memberof, scope: scopePunc, name: name, variation: variation }; } // TODO: deprecate exports.shorten in favor of a better name /** Given a longname like "a.b#c(2)", slice it up into an object containing the memberof, the scope, the name, and variation. @param {string} longname @param {string} forcedMemberof @returns {object} Representing the properties of the given name. */ exports.shorten = function(longname, forcedMemberof) { return atomize(longname, SCOPE_PUNC, forcedMemberof); }; // TODO: docs exports.combine = function(parts) { return [ (parts.memberof || ''), (parts.scope || ''), (parts.name || ''), (parts.variation || '') ].join(''); }; // TODO: docs exports.stripVariation = function(name) { var parts = exports.shorten(name); parts.variation = ''; return exports.combine(parts); }; function splitLongname(longname, options) { var chunks = []; var currentNameInfo; var nameInfo = {}; var previousName = longname; var splitters = SCOPE_PUNC.concat('/'); options = _.defaults(options || {}, { includeVariation: true }); do { if (!options.includeVariation) { previousName = exports.stripVariation(previousName); } currentNameInfo = nameInfo[previousName] = atomize(previousName, splitters); previousName = currentNameInfo.memberof; chunks.push(currentNameInfo.scope + currentNameInfo.name); } while (previousName); return { chunks: chunks.reverse(), nameInfo: nameInfo }; } // TODO: docs exports.longnamesToTree = function longnamesToTree(longnames, doclets) { var splitOptions = { includeVariation: false }; var tree = {}; longnames.forEach(function(longname) { var currentLongname = ''; var currentParent = tree; var nameInfo; var processed; // don't try to add empty longnames to the tree if (!longname) { return; } processed = splitLongname(longname, splitOptions); nameInfo = processed.nameInfo; processed.chunks.forEach(function(chunk) { currentLongname += chunk; if (currentParent !== tree) { currentParent.children = currentParent.children || {}; currentParent = currentParent.children; } if (!hasOwnProp.call(currentParent, chunk)) { currentParent[chunk] = nameInfo[currentLongname]; } if (currentParent[chunk]) { currentParent[chunk].doclet = doclets ? doclets[currentLongname] : null; currentParent = currentParent[chunk]; } }); }); return tree; }; /** Split a string that starts with a name and ends with a description into its parts. Allows the defaultvalue (if present) to contain brackets. If the name is found to have mismatched brackets, null is returned. @param {string} nameDesc @returns {object} Hash with "name" and "description" properties. */ function splitNameMatchingBrackets(nameDesc) { var buffer = []; var c; var stack = 0; var stringEnd = null; for (var i = 0; i < nameDesc.length; ++i) { c = nameDesc[i]; buffer.push(c); if (stringEnd) { Iif (c === '\\' && i + 1 < nameDesc.length) { buffer.push(nameDesc[++i]); } else if (c === stringEnd) { stringEnd = null; } } else if (c === '"' || c === "'") { stringEnd = c; } else if (c === '[') { ++stack; } else if (c === ']') { if (--stack === 0) { break; } } } if (stack || stringEnd) { return null; } nameDesc.substr(i).match(REGEXP_DESCRIPTION); return { name: buffer.join(''), description: RegExp.$1 }; } // TODO: deprecate exports.splitName in favor of a better name /** Split a string that starts with a name and ends with a description into its parts. @param {string} nameDesc @returns {object} Hash with "name" and "description" properties. */ exports.splitName = function(nameDesc) { // like: name, [name], name text, [name] text, name - text, or [name] - text // the hyphen must be on the same line as the name; this prevents us from treating a Markdown // dash as a separator // optional values get special treatment var result = null; if (nameDesc[0] === '[') { result = splitNameMatchingBrackets(nameDesc); if (result !== null) { return result; } } nameDesc.match(REGEXP_NAME_DESCRIPTION); return { name: RegExp.$1, description: RegExp.$2 }; }; |