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 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | 1 1 1 1 744 744 744 744 744 744 3205 3205 3421 3421 592 592 40 40 44 744 1 744 744 744 1 612 612 570 570 568 44 570 1 744 744 568 744 1 744 744 1 44 44 44 532 532 78 44 1 14 15 1 43 43 43 43 1 20 1 15 15 15 15 1 51 3 48 48 1 51 34 51 1 28 28 28 28 28 10 10 28 1 144 144 144 144 144 144 144 144 144 161 161 161 12 12 14 14 33 3 30 30 26 30 30 30 30 30 30 10 20 30 19 19 11 7 7 7 7 2 7 7 4 144 1 20 20 20 20 14 6 6 6 6 6 6 6 1 142 142 142 142 142 142 142 142 142 160 160 160 12 12 14 14 22 2 20 20 20 20 20 15 20 20 142 1 37 23 37 37 29 37 29 1 284 284 284 284 284 284 284 284 284 284 320 320 320 16 16 16 16 23 23 23 23 23 23 4 19 2 2 17 3 3 3 3 3 3 14 284 1 744 744 744 570 570 48 48 31 48 48 1 186 1 186 1 372 1 187 187 187 187 | 'use strict'; /** * Provides methods for augmenting the parse results based on their content. * @module jsdoc/augment */ var doop = require('jsdoc/util/doop'); var name = require('jsdoc/name'); var hasOwnProp = Object.prototype.hasOwnProperty; function mapDependencies(index, propertyName) { var dependencies = {}; var doc; var doclets; var kinds = ['class', 'external', 'interface', 'mixin']; var len = 0; Object.keys(index).forEach(function(indexName) { doclets = index[indexName]; for (var i = 0, ii = doclets.length; i < ii; i++) { doc = doclets[i]; if (kinds.indexOf(doc.kind) !== -1) { dependencies[indexName] = {}; if (hasOwnProp.call(doc, propertyName)) { len = doc[propertyName].length; for (var j = 0; j < len; j++) { dependencies[indexName][doc[propertyName][j]] = true; } } } } }); return dependencies; } function Sorter(dependencies) { this.dependencies = dependencies; this.visited = {}; this.sorted = []; } Sorter.prototype.visit = function(key) { var self = this; if (!(key in this.visited)) { this.visited[key] = true; if (this.dependencies[key]) { Object.keys(this.dependencies[key]).forEach(function(path) { self.visit(path); }); } this.sorted.push(key); } }; Sorter.prototype.sort = function() { var self = this; Object.keys(this.dependencies).forEach(function(key) { self.visit(key); }); return this.sorted; }; function sort(dependencies) { var sorter = new Sorter(dependencies); return sorter.sort(); } function getMembers(longname, docs, scopes) { var candidate; var members = []; for (var i = 0, l = docs.length; i < l; i++) { candidate = docs[i]; if (candidate.memberof === longname && scopes.indexOf(candidate.scope) !== -1) { members.push(candidate); } } return members; } function addDocletProperty(doclets, propName, value) { for (var i = 0, l = doclets.length; i < l; i++) { doclets[i][propName] = value; } } function reparentDoclet(parent, child) { var parts = name.shorten(child.longname); parts.memberof = parent.longname; child.memberof = parent.longname; child.longname = name.combine(parts); } function parentIsClass(parent) { return parent.kind === 'class'; } function staticToInstance(doclet) { var parts = name.shorten(doclet.longname); parts.scope = name.SCOPE.PUNC.INSTANCE; doclet.longname = name.combine(parts); doclet.scope = name.SCOPE.NAMES.INSTANCE; } /** * Update the list of doclets to be added to another symbol. * * We add only one doclet per longname. For example: If `ClassA` inherits from two classes that both * use the same method name, `ClassA` gets docs for one method rather than two. * * Also, the last symbol wins for any given longname. For example: If you write `@extends Class1 * @extends Class2`, and both classes have an instance method called `myMethod`, you get the docs * from `Class2#myMethod`. * * @private * @param {module:jsdoc/doclet.Doclet} doclet - The doclet to be added. * @param {Array.<module:jsdoc/doclet.Doclet>} additions - An array of doclets that will be added to * another symbol. * @param {Object.<string, number>} indexes - A dictionary of indexes into the `additions` array. * Each key is a longname, and each value is the index of the longname's doclet. * @return {void} */ function updateAddedDoclets(doclet, additions, indexes) { if (typeof indexes[doclet.longname] !== 'undefined') { // replace the existing doclet additions[indexes[doclet.longname]] = doclet; } else { // add the doclet to the array, and track its index additions.push(doclet); indexes[doclet.longname] = additions.length - 1; } } /** * Update the index of doclets whose `undocumented` property is not `true`. * * @private * @param {module:jsdoc/doclet.Doclet} doclet - The doclet to be added to the index. * @param {Object.<string, Array.<module:jsdoc/doclet.Doclet>>} documented - The index of doclets * whose `undocumented` property is not `true`. * @return {void} */ function updateDocumentedDoclets(doclet, documented) { if ( !hasOwnProp.call(documented, doclet.longname) ) { documented[doclet.longname] = []; } documented[doclet.longname].push(doclet); } function explicitlyInherits(doclets) { var doclet; var inherits = false; for (var i = 0, l = doclets.length; i < l; i++) { doclet = doclets[i]; if (typeof doclet.inheritdoc !== 'undefined' || typeof doclet.override !== 'undefined') { inherits = true; break; } } return inherits; } // TODO: try to reduce overlap with similar methods function getInheritedAdditions(doclets, docs, documented) { var additionIndexes; var additions = []; var doc; var parents; var members; var member; var parts; // doclets will be undefined if the inherited symbol isn't documented doclets = doclets || []; for (var i = 0, ii = doclets.length; i < ii; i++) { doc = doclets[i]; parents = doc.augments; if ( parents && (doc.kind === 'class' || doc.kind === 'interface') ) { // reset the lookup table of added doclet indexes by longname additionIndexes = {}; for (var j = 0, jj = parents.length; j < jj; j++) { members = getMembers(parents[j], docs, ['instance']); for (var k = 0, kk = members.length; k < kk; k++) { // We only care about symbols that are documented. if (members[k].undocumented) { continue; } member = doop(members[k]); if (!member.inherited) { member.inherits = member.longname; } member.inherited = true; // TODO: this will fail on longnames like: MyClass#"quoted#Longname" // and nested instance members like: MyClass#MyOtherClass#myMethod; // switch to updateLongname()! member.memberof = doc.longname; parts = member.longname.split('#'); parts[0] = doc.longname; member.longname = parts.join('#'); // Indicate what the descendant is overriding. (We only care about the closest // ancestor. For classes A > B > C, if B#a overrides A#a, and C#a inherits B#a, // we don't want the doclet for C#a to say that it overrides A#a.) if ( hasOwnProp.call(docs.index.longname, member.longname) ) { member.overrides = members[k].longname; } else { delete member.overrides; } // Add the ancestor's docs unless the descendant overrides the ancestor AND // documents the override. if ( !hasOwnProp.call(documented, member.longname) ) { updateAddedDoclets(member, additions, additionIndexes); updateDocumentedDoclets(member, documented); } // If the descendant used an @inheritdoc or @override tag, add the ancestor's // docs, and ignore the existing doclets. else if ( explicitlyInherits(documented[member.longname]) ) { // Ignore any existing doclets. (This is safe because we only get here if // `member.longname` is an own property of `documented`.) addDocletProperty(documented[member.longname], 'ignore', true); updateAddedDoclets(member, additions, additionIndexes); updateDocumentedDoclets(member, documented); // Remove property that's no longer accurate. if (member.virtual) { delete member.virtual; } // Remove properties that we no longer need. Iif (member.inheritdoc) { delete member.inheritdoc; } Iif (member.override) { delete member.override; } } // If the descendant overrides the ancestor and documents the override, // update the doclets to indicate what the descendant is overriding. else { addDocletProperty(documented[member.longname], 'overrides', members[k].longname); } } } } } return additions; } function updateMixes(mixedDoclet, mixedLongname) { var idx; var mixedName; var names; // take the fast path if there's no array of mixed-in longnames if (!mixedDoclet.mixes) { mixedDoclet.mixes = [mixedLongname]; } else { // find the short name of the longname we're mixing in mixedName = name.shorten(mixedLongname).name; // find the short name of each previously mixed-in symbol names = mixedDoclet.mixes.map(function(m) { return name.shorten(mixedDoclet.longname).name; }); // if we're mixing `myMethod` into `MixinC` from `MixinB`, and `MixinB` had the method mixed // in from `MixinA`, don't show `MixinA.myMethod` in the `mixes` list idx = names.indexOf(mixedName); Eif (idx !== -1) { mixedDoclet.mixes.splice(idx, 1); } mixedDoclet.mixes.push(mixedLongname); } } // TODO: try to reduce overlap with similar methods function getMixedInAdditions(mixinDoclets, allDoclets, commentedDoclets) { var additionIndexes; var additions = []; var doclet; var idx; var mixedDoclet; var mixedDoclets; var mixes; // mixinDoclets will be undefined if the mixed-in symbol isn't documented mixinDoclets = mixinDoclets || []; for (var i = 0, ii = mixinDoclets.length; i < ii; i++) { doclet = mixinDoclets[i]; mixes = doclet.mixes; if (mixes) { // reset the lookup table of added doclet indexes by longname additionIndexes = {}; for (var j = 0, jj = mixes.length; j < jj; j++) { mixedDoclets = getMembers(mixes[j], allDoclets, ['static']); for (var k = 0, kk = mixedDoclets.length; k < kk; k++) { // We only care about symbols that are documented. if (mixedDoclets[k].undocumented) { continue; } mixedDoclet = doop(mixedDoclets[k]); updateMixes(mixedDoclet, mixedDoclet.longname); mixedDoclet.mixed = true; reparentDoclet(doclet, mixedDoclet); // if we're mixing into a class, treat the mixed-in symbol as an instance member if (parentIsClass(doclet)) { staticToInstance(mixedDoclet); } updateAddedDoclets(mixedDoclet, additions, additionIndexes); updateDocumentedDoclets(mixedDoclet, commentedDoclets); } } } } return additions; } function updateImplements(implDoclets, implementedLongname) { if ( !Array.isArray(implDoclets) ) { implDoclets = [implDoclets]; } implDoclets.forEach(function(implDoclet) { if ( !hasOwnProp.call(implDoclet, 'implements') ) { implDoclet.implements = []; } if (implDoclet.implements.indexOf(implementedLongname) === -1) { implDoclet.implements.push(implementedLongname); } }); } // TODO: try to reduce overlap with similar methods function getImplementedAdditions(implDoclets, allDoclets, commentedDoclets) { var additionIndexes; var additions = []; var doclet; var idx; var implementations; var implExists; var implementationDoclet; var interfaceDoclets; // interfaceDoclets will be undefined if the implemented symbol isn't documented implDoclets = implDoclets || []; for (var i = 0, ii = implDoclets.length; i < ii; i++) { doclet = implDoclets[i]; implementations = doclet.implements; if (implementations) { // reset the lookup table of added doclet indexes by longname additionIndexes = {}; for (var j = 0, jj = implementations.length; j < jj; j++) { interfaceDoclets = getMembers(implementations[j], allDoclets, ['instance']); for (var k = 0, kk = interfaceDoclets.length; k < kk; k++) { // We only care about symbols that are documented. Iif (interfaceDoclets[k].undocumented) { continue; } implementationDoclet = doop(interfaceDoclets[k]); reparentDoclet(doclet, implementationDoclet); updateImplements(implementationDoclet, interfaceDoclets[k].longname); // If there's no implementation, move along. implExists = hasOwnProp.call(allDoclets.index.longname, implementationDoclet.longname); if (!implExists) { continue; } // Add the interface's docs unless the implementation is already documented. if ( !hasOwnProp.call(commentedDoclets, implementationDoclet.longname) ) { updateAddedDoclets(implementationDoclet, additions, additionIndexes); updateDocumentedDoclets(implementationDoclet, commentedDoclets); } // If the implementation used an @inheritdoc or @override tag, add the // interface's docs, and ignore the existing doclets. else if ( explicitlyInherits(commentedDoclets[implementationDoclet.longname]) ) { // Ignore any existing doclets. (This is safe because we only get here if // `implementationDoclet.longname` is an own property of // `commentedDoclets`.) addDocletProperty(commentedDoclets[implementationDoclet.longname], 'ignore', true); updateAddedDoclets(implementationDoclet, additions, additionIndexes); updateDocumentedDoclets(implementationDoclet, commentedDoclets); // Remove property that's no longer accurate. Iif (implementationDoclet.virtual) { delete implementationDoclet.virtual; } // Remove properties that we no longer need. Iif (implementationDoclet.inheritdoc) { delete implementationDoclet.inheritdoc; } Iif (implementationDoclet.override) { delete implementationDoclet.override; } } // If there's an implementation, and it's documented, update the doclets to // indicate what the implementation is implementing. else { updateImplements(commentedDoclets[implementationDoclet.longname], interfaceDoclets[k].longname); } } } } } return additions; } function augment(doclets, propertyName, docletFinder) { var index = doclets.index.longname; var dependencies = sort( mapDependencies(index, propertyName) ); dependencies.forEach(function(depName) { var additions = docletFinder(index[depName], doclets, doclets.index.documented); additions.forEach(function(addition) { var longname = addition.longname; if ( !hasOwnProp.call(index, longname) ) { index[longname] = []; } index[longname].push(addition); doclets.push(addition); }); }); } /** * Add doclets to reflect class inheritance. * * For example, if `ClassA` has the instance method `myMethod`, and `ClassB` inherits from `ClassA`, * calling this method creates a new doclet for `ClassB#myMethod`. * * @param {!Array.<module:jsdoc/doclet.Doclet>} doclets - The doclets generated by JSDoc. * @param {!Object} doclets.index - The doclet index added by {@link module:jsdoc/borrow.indexAll}. * @return {void} */ exports.addInherited = function(doclets) { augment(doclets, 'augments', getInheritedAdditions); }; /** * Add doclets to reflect mixins. When a symbol is mixed into a class, the class' version of the * mixed-in symbol is treated as an instance member. * * For example: * * + If `MixinA` has the static method `myMethod`, and `MixinB` mixes `MixinA`, calling this method * creates a new doclet for the static method `MixinB.myMethod`. * + If `MixinA` has the static method `myMethod`, and `ClassA` mixes `MixinA`, calling this method * creates a new doclet for the instance method `ClassA#myMethod`. * * @param {!Array.<module:jsdoc/doclet.Doclet>} doclets - The doclets generated by JSDoc. * @param {!Object} doclets.index - The doclet index added by {@link module:jsdoc/borrow.indexAll}. * @return {void} */ exports.addMixedIn = function(doclets) { augment(doclets, 'mixes', getMixedInAdditions); }; /** * Add and update doclets to reflect implementations of interfaces. * * For example, if `InterfaceA` has the instance method `myMethod`, and `ClassA` implements * `InterfaceA`, calling this method does the following: * * + Updates `InterfaceA` to indicate that it is implemented by `ClassA` * + Updates `InterfaceA#myMethod` to indicate that it is implemented by `ClassA#myMethod` * + Updates `ClassA#myMethod` to indicate that it implements `InterfaceA#myMethod` * * If `ClassA#myMethod` used the `@override` or `@inheritdoc` tag, calling this method would also * generate a new doclet that reflects the interface's documentation for `InterfaceA#myMethod`. * * @param {!Array.<module:jsdoc/doclet.Doclet>} docs - The doclets generated by JSDoc. * @param {!Object} doclets.index - The doclet index added by {@link module:jsdoc/borrow.indexAll}. * @return {void} */ exports.addImplemented = function(doclets) { augment(doclets, 'implements', getImplementedAdditions); }; /** * Add and update doclets to reflect all of the following: * * + Inherited classes * + Mixins * + Interface implementations * * Calling this method is equivalent to calling all other methods exported by this module. * * @return {void} */ exports.augmentAll = function(doclets) { exports.addMixedIn(doclets); exports.addImplemented(doclets); exports.addInherited(doclets); // look for implemented doclets again, in case we inherited an interface exports.addImplemented(doclets); }; |