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 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 | 1 1 1 1 1 1269 1269 1269 594 1269 1 936 936 291 1 10 10 1 10 10 10 10 10 10 10 10 10 1 9 9 9 9 13 13 1 1 12 8 8 8 4 1 156 156 66 2 2 156 1 404 404 404 404 404 248 156 156 156 2 2 2 2 1 156 156 66 9 57 156 1 404 404 404 404 404 404 404 248 156 156 156 156 65 24 41 41 4 37 5 37 1 3 3 3 3 3 3 3 3 3 1 1269 1269 1269 1269 1269 1269 1269 1269 1269 1269 1269 1269 1869 1 684 684 684 684 684 1 216 216 216 1 216 1 5 1 1 3 1 5925 5925 5925 11850 5925 1 1954 1 5925 1 2 1 2 2 1 5925 5925 5925 5925 5925 5273 652 652 641 652 25 652 685 685 684 684 684 2 652 1 5925 5925 5925 5925 8 8 8 5925 1269 5925 5925 1940 5925 1 936 936 936 936 936 936 1 5925 5925 5925 5925 5925 5925 5925 5925 316 316 316 316 230 316 10 10 1 1 1 10 7 7 7 7 1 1 8 8 8 404 404 404 404 404 404 1863 1863 8 8 8 1863 676 4 676 10 3 10 10 132 132 186 183 186 186 3 3 1 1 1 3 191 191 191 191 8 191 5925 4656 5925 | /** * @module jsdoc/src/visitor */ 'use strict'; // TODO: consider exporting more stuff so users can override it var jsdoc = { doclet: require('jsdoc/doclet'), name: require('jsdoc/name'), src: { astnode: require('jsdoc/src/astnode'), syntax: require('jsdoc/src/syntax') }, util: { logger: require('jsdoc/util/logger') } }; var util = require('util'); var hasOwnProp = Object.prototype.hasOwnProperty; var Syntax = jsdoc.src.syntax.Syntax; // TODO: docs function getLeadingComment(node) { var comment = null; var leadingComments = node.leadingComments; if (Array.isArray(leadingComments) && leadingComments.length && leadingComments[0].raw) { comment = leadingComments[0].raw; } return comment; } // TODO: docs function makeVarsFinisher(scopeDoclet) { return function(e) { // no need to evaluate all things related to scopeDoclet again, just use it if ( scopeDoclet && e.doclet && (e.doclet.alias || e.doclet.memberof) ) { scopeDoclet.meta.vars[e.code.name] = e.doclet.longname; } }; } // Given an event, get the parent node's doclet. function getParentDocletFromEvent(parser, e) { Eif (e.doclet && e.doclet.meta && e.doclet.meta.code && e.doclet.meta.code.node && e.doclet.meta.code.node.parent) { return parser._getDoclet(e.doclet.meta.code.node.parent.nodeId); } return null; } /** * For function parameters that have inline documentation, create a function that will merge the * inline documentation into the function's doclet. If the parameter is already documented in the * function's doclet, the inline documentation will be ignored. * * @private * @param {module:jsdoc/src/parser.Parser} parser - The JSDoc parser. * @return {function} A function that merges a parameter's inline documentation into the function's * doclet. */ function makeInlineParamsFinisher(parser) { return function(e) { var documentedParams; var knownParams; var param; var parentDoclet; var i = 0; parentDoclet = getParentDocletFromEvent(parser, e); Iif (!parentDoclet) { return; } // we only want to use the doclet if it's param-specific (but not, for example, if it's // a param tagged with `@exports` in an AMD module) if (e.doclet.kind !== 'param') { return; } parentDoclet.params = parentDoclet.params || []; documentedParams = parentDoclet.params; knownParams = parentDoclet.meta.code.paramnames || []; while (true) { param = documentedParams[i]; // is the param already documented? if so, we don't need to use the doclet if (param && param.name === e.doclet.name) { e.doclet.undocumented = true; break; } // if we ran out of documented params, or we're at the parameter's actual position, // splice in the param at the current index if ( !param || i === knownParams.indexOf(e.doclet.name) ) { documentedParams.splice(i, 0, { type: e.doclet.type || {}, description: '', name: e.doclet.name }); // the doclet is no longer needed e.doclet.undocumented = true; break; } i++; } }; } /** * Given an array of nodes that represent function parameters, find the node for the rest parameter, * if any. * * @private * @param {Array.<Object>} params - An array of nodes that represent function parameters. * @return {Object?} The node for the rest parameter. */ function findRestParam(params) { var restParam = null; params.some(function(param) { if (param.type === Syntax.RestElement) { restParam = param; return true; } }); return restParam; } /** * For functions that may include a rest parameter, create a function that will automatically update * the rest parameter's documentation to indicate that the parameter is repeatable. If the parameter * is not documented, the function's doclet will remain unchanged. * * @private * @param {module:jsdoc/src/parser.Parser} parser - The JSDoc parser. * @return {function} A function that updates the rest parameter's documentation to indicate that * the parameter is repeatable. */ function makeRestParamFinisher(parser) { return function(e) { var doclet = e.doclet; var documentedParams; var restNode; if (!doclet) { return; } documentedParams = doclet.params = doclet.params || []; restNode = findRestParam(e.code.node.params); if (restNode) { for (var i = documentedParams.length - 1; i >= 0; i--) { Eif (documentedParams[i].name === restNode.argument.name) { documentedParams[i].variable = true; break; } } } }; } /** * Given an array of nodes that represent function parameters, find the nodes for the default * parameters, if any. * * @private * @param {Array.<Object>} params - An array of nodes that represent function parameters. * @return {Array.<Object>} The nodes for the default parameters. */ function findDefaultParams(params) { var defaultParams = []; params.forEach(function(param) { if (param.type === Syntax.AssignmentPattern) { defaultParams.push(param); } else { defaultParams.push(null); } }); return defaultParams; } /** * For functions that may have at least one parameter with default values, create a function that * will automatically add the parameters' default values to the function's documentation. If any * default value is already documented, the function's doclet will remain unchanged. * * This function is only intended to handle default parameters whose node type is `Syntax.Literal` * (string, numeric, and boolean literals). This is because more complex default values may include, * for example, references to internal variables, which it may not make sense to include in * documentation. * * @private * @param {module:jsdoc/src/parser.Parser} parser - The JSDoc parser. * @return {function} A function that updates the function doclet to include the default values of * parameters. */ function makeDefaultParamFinisher(parser) { return function(e) { var defaultValues; var doclet = e.doclet; var documentedParams; var paramName; var params; if (!doclet) { return; } documentedParams = doclet.params = doclet.params || []; params = e.code.node.params; defaultValues = findDefaultParams(e.code.node.params); for (var i = 0, j = 0, l = params.length; i < l; i++) { // bail out if we ran out of documented params if (!documentedParams[j]) { break; } // if the current parameter doesn't appear to be documented, move to the next one paramName = params[i].type === Syntax.AssignmentPattern ? params[i].left.name : params[i].name; if (paramName !== documentedParams[j].name) { continue; } // add the default value iff a) a literal default value is defined in the code, // b) no default value is documented, and c) the default value is not an empty string if (defaultValues[i] && defaultValues[i].right && defaultValues[i].right.type === Syntax.Literal && typeof documentedParams[j].defaultvalue === 'undefined' && defaultValues[i].right.value !== '') { documentedParams[j].defaultvalue = jsdoc.src.astnode.nodeToValue(defaultValues[i].right); } // move to the next documented param j++; } }; } /** * For method definitions that are constructors, create a function that will merge portions of the * constructor's doclet into the class's doclet, provided that a doclet exists for the class. * Merging the constructor's documentation allows ES 2015 classes to be documented in a natural way, * with separate JSDoc comments for the class and its constructor. * * @private * @param {module:jsdoc/src/parser.Parser} parser - The JSDoc parser. * @return {function} A function that merges the constructor's doclet into the class's doclet. */ function makeConstructorFinisher(parser) { return function(e) { var doclet = e.doclet; var parentDoclet = parser._getDoclet(e.code.node.parent.parent.nodeId); Iif (!doclet || !parentDoclet || parentDoclet.undocumented) { return; } Eif (!parentDoclet.description && doclet.description) { parentDoclet.description = doclet.description; } Eif (!parentDoclet.params && doclet.params) { parentDoclet.params = doclet.params.slice(0); } doclet.undocumented = true; }; } // TODO: docs function SymbolFound(node, filename, extras) { var self = this; extras = extras || {}; this.id = extras.id || node.nodeId; this.comment = extras.comment || getLeadingComment(node) || '@undocumented'; this.lineno = extras.lineno || node.loc.start.line; this.range = extras.range || node.range; this.filename = extras.filename || filename; this.astnode = extras.astnode || node; this.code = extras.code; this.event = extras.event || 'symbolFound'; this.finishers = extras.finishers || []; // make sure the event includes properties that don't have default values Object.keys(extras).forEach(function(key) { self[key] = extras[key]; }); } // TODO: docs function JsdocCommentFound(comment, filename) { this.comment = comment.raw; this.lineno = comment.loc.start.line; this.filename = filename; this.range = comment.range; Object.defineProperty(this, 'event', { value: 'jsdocCommentFound' }); } // TODO: docs var Visitor = exports.Visitor = function() { this._parser = null; // Mozilla Parser API node visitors added by plugins this._nodeVisitors = []; // built-in visitors this._visitors = [ this.visitNodeComments, this.visitNode ]; }; /** * Set the parser instance that visitors can use. * * @param {module:jsdoc/src/parser.Parser} parser - The parser instance. */ Visitor.prototype.setParser = function(parser) { this._parser = parser; }; // TODO: docs Visitor.prototype.addAstNodeVisitor = function(visitor) { this._nodeVisitors.push(visitor); }; // TODO: docs Visitor.prototype.removeAstNodeVisitor = function(visitor) { var idx = this._nodeVisitors.indexOf(visitor); if (idx !== -1) { this._nodeVisitors.splice(idx, 1); } }; // TODO: docs Visitor.prototype.getAstNodeVisitors = function() { return this._nodeVisitors; }; // TODO: docs; visitor signature is (node, parser, filename) Visitor.prototype.visit = function(node, filename) { var i; var l; for (i = 0, l = this._visitors.length; i < l; i++) { this._visitors[i].call(this, node, this._parser, filename); } return true; }; // TODO: docs /** * Verify that a block comment exists and that its leading delimiter does not contain three or more * asterisks. * * @private * @memberof module:jsdoc/src/parser.Parser */ function isValidJsdoc(commentSrc) { return commentSrc && commentSrc.indexOf('/***') !== 0; } // TODO: docs function hasJsdocComments(node) { return (node && node.leadingComments && node.leadingComments.length) || (node && node.trailingComments && node.trailingComments.length); } // TODO: docs function removeCommentDelimiters(comment) { return comment.substring(2, comment.length - 2); } // TODO: docs function updateCommentNode(commentNode, comment) { commentNode.raw = comment; commentNode.value = removeCommentDelimiters(comment); } // TODO: docs Visitor.prototype.visitNodeComments = function(node, parser, filename) { var comment; var comments; var e; var BLOCK_COMMENT = 'Block'; if ( !hasJsdocComments(node) && (!node.type || node.type !== BLOCK_COMMENT) ) { return true; } comments = (node.type === BLOCK_COMMENT) ? [node] : []; if (node.leadingComments && node.leadingComments.length) { comments = comments.concat( node.leadingComments.slice(0) ); } if (node.trailingComments && node.trailingComments.length) { comments = comments.concat( node.trailingComments.slice(0) ); } for (var i = 0, l = comments.length; i < l; i++) { comment = comments[i]; if ( isValidJsdoc(comment.raw) ) { e = new JsdocCommentFound(comment, filename); parser.emit(e.event, e, parser); if (e.comment !== comment.raw) { updateCommentNode(comment, e.comment); } } } return true; }; // TODO: docs Visitor.prototype.visitNode = function(node, parser, filename) { var i; var l; var e = this.makeSymbolFoundEvent(node, parser, filename); if (this._nodeVisitors && this._nodeVisitors.length) { for (i = 0, l = this._nodeVisitors.length; i < l; i++) { this._nodeVisitors[i].visitNode(node, e, parser, filename); Iif (e.stopPropagation) { break; } } } if (!e.preventDefault && e.comment && isValidJsdoc(e.comment)) { parser.emit(e.event, e, parser); } // add the node to the parser's lookup table parser.addDocletRef(e); for (i = 0, l = e.finishers.length; i < l; i++) { e.finishers[i].call(parser, e); } return true; }; // TODO: docs // TODO: note that it's essential to call this function before you try to resolve names! function trackVars(parser, node, e) { var enclosingScopeId = node.enclosingScope ? node.enclosingScope.nodeId : jsdoc.name.LONGNAMES.GLOBAL; var doclet = parser.refs[enclosingScopeId]; Eif (doclet) { doclet.meta.vars = doclet.meta.vars || {}; doclet.meta.vars[e.code.name] = null; e.finishers.push( makeVarsFinisher(doclet) ); } } // TODO: docs Visitor.prototype.makeSymbolFoundEvent = function(node, parser, filename) { var logger = jsdoc.util.logger; var e; var basename; var i; var l; var parent; var extras = { code: jsdoc.src.astnode.getInfo(node) }; switch (node.type) { // like: i = 0; case Syntax.AssignmentExpression: e = new SymbolFound(node, filename, extras); trackVars(parser, node, e); basename = parser.getBasename(e.code.name); if (basename !== 'this') { e.code.funcscope = parser.resolveVar(node, basename); } break; // like `bar='baz'` in: function foo(bar='baz') {} case Syntax.AssignmentPattern: parent = node.parent; if ( node.leadingComments && parent && jsdoc.src.astnode.isFunction(parent) ) { extras.finishers = [makeInlineParamsFinisher(parser)]; e = new SymbolFound(node, filename, extras); trackVars(parser, node, e); } break; // like: class foo {} case Syntax.ClassDeclaration: // falls through // like: let MyClass = class {} case Syntax.ClassExpression: e = new SymbolFound(node, filename, extras); trackVars(parser, node, e); basename = parser.getBasename(e.code.name); break; // like: export * from 'foo' case Syntax.ExportAllDeclaration: e = new SymbolFound(node, filename, extras); break; // like: export default 'foo' case Syntax.ExportDefaultDeclaration: // falls through // like: export var foo; // or: export {foo} case Syntax.ExportNamedDeclaration: // falls through // like `foo as bar` in: export {foo as bar} case Syntax.ExportSpecifier: e = new SymbolFound(node, filename, extras); trackVars(parser, node, e); break; // like: function foo() {} case Syntax.FunctionDeclaration: // falls through // like: var foo = function() {}; case Syntax.FunctionExpression: extras.finishers = [ // handle cases where at least one parameter has a default value makeDefaultParamFinisher(parser), // handle rest parameters makeRestParamFinisher(parser) ]; e = new SymbolFound(node, filename, extras); trackVars(parser, node, e); basename = parser.getBasename(e.code.name); e.code.funcscope = parser.resolveVar(node, basename); break; // like `bar` in: function foo(/** @type {string} */ bar) {} // or `module` in: define("MyModule", function(/** @exports MyModule */ module) {} // This is an extremely common type of node; we only care about function parameters with // inline comments. No need to fire an event in other cases. case Syntax.Identifier: parent = node.parent; // function parameters with inline comments if ( node.leadingComments && parent && jsdoc.src.astnode.isFunction(parent) ) { extras.finishers = [makeInlineParamsFinisher(parser)]; e = new SymbolFound(node, filename, extras); trackVars(parser, node, e); } break; // like `obj.prop` in: /** @typedef {string} */ obj.prop; // Closure Compiler uses this pattern extensively for enums. // No need to fire an event unless the node is already commented. case Syntax.MemberExpression: if (node.leadingComments) { e = new SymbolFound(node, filename, extras); } break; // like: foo() {} // or: constructor() {} case Syntax.MethodDefinition: // for constructors, we attempt to merge the constructor's docs into the class's docs if (node.kind === 'constructor') { extras.finishers = [makeConstructorFinisher(parser)]; } e = new SymbolFound(node, filename, extras); break; // like `{}` in: function Foo = Class.create(/** @lends Foo */ {}); case Syntax.ObjectExpression: e = new SymbolFound(node, filename, extras); break; // like `bar: true` in: var foo = { bar: true }; // like `get bar() {}` in: var foo = { get bar() {} }; case Syntax.Property: if (node.kind !== 'get' && node.kind !== 'set') { extras.finishers = [parser.resolveEnum]; } e = new SymbolFound(node, filename, extras); break; // like `...bar` in: function foo(...bar) {} case Syntax.RestElement: parent = node.parent; if ( node.leadingComments && parent && jsdoc.src.astnode.isFunction(parent) ) { extras.finishers = [makeInlineParamsFinisher(parser)]; e = new SymbolFound(node, filename, extras); trackVars(parser, node, e); } break; // like: var i = 0; case Syntax.VariableDeclarator: e = new SymbolFound(node, filename, extras); trackVars(parser, node, e); basename = parser.getBasename(e.code.name); // auto-detect constants if (node.parent.kind === 'const') { e.code.kind = 'constant'; } break; // log a warning for ES 2015 nodes that are not currently handled case Syntax.ArrowFunctionExpression: logger.warn('JSDoc does not currently handle %s nodes. Source file: %s, line %s', node.type, filename, (node.loc && node.loc.start) ? node.loc.start.line : '??'); break; default: // ignore } if (!e) { e = { finishers: [] }; } return e; }; |