eventDumper.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Dump information about parser events to the console.
  3. *
  4. * @module plugins/eventDumper
  5. */
  6. 'use strict';
  7. var _ = require('underscore');
  8. var dump = require('jsdoc/util/dumper').dump;
  9. var env = require('jsdoc/env');
  10. var util = require('util');
  11. var conf = env.conf.eventDumper || {};
  12. // Dump the included parser events (defaults to all events)
  13. var events = conf.include || [
  14. 'parseBegin',
  15. 'fileBegin',
  16. 'beforeParse',
  17. 'jsdocCommentFound',
  18. 'symbolFound',
  19. 'newDoclet',
  20. 'fileComplete',
  21. 'parseComplete',
  22. 'processingComplete'
  23. ];
  24. // Don't dump the excluded parser events
  25. if (conf.exclude) {
  26. events = _.difference(events, conf.exclude);
  27. }
  28. /**
  29. * Replace AST node objects in events with a placeholder.
  30. *
  31. * @param {Object} o - An object whose properties may contain AST node objects.
  32. * @return {Object} The modified object.
  33. */
  34. function replaceNodeObjects(o) {
  35. var doop = require('jsdoc/util/doop');
  36. var OBJECT_PLACEHOLDER = '<Object>';
  37. if (o.code && o.code.node) {
  38. // don't break the original object!
  39. o.code = doop(o.code);
  40. o.code.node = OBJECT_PLACEHOLDER;
  41. }
  42. if (o.doclet && o.doclet.meta && o.doclet.meta.code && o.doclet.meta.code.node) {
  43. // don't break the original object!
  44. o.doclet.meta.code = doop(o.doclet.meta.code);
  45. o.doclet.meta.code.node = OBJECT_PLACEHOLDER;
  46. }
  47. if (o.astnode) {
  48. o.astnode = OBJECT_PLACEHOLDER;
  49. }
  50. return o;
  51. }
  52. /**
  53. * Get rid of unwanted crud in an event object.
  54. *
  55. * @param {object} e The event object.
  56. * @return {object} The fixed-up object.
  57. */
  58. function cleanse(e) {
  59. var result = {};
  60. Object.keys(e).forEach(function(prop) {
  61. // by default, don't stringify properties that contain an array of functions
  62. if (!conf.includeFunctions && util.isArray(e[prop]) && e[prop][0] &&
  63. String(typeof e[prop][0]) === 'function') {
  64. result[prop] = 'function[' + e[prop].length + ']';
  65. }
  66. // never include functions that belong to the object
  67. else if (typeof e[prop] !== 'function') {
  68. result[prop] = e[prop];
  69. }
  70. });
  71. // allow users to omit node objects, which can be enormous
  72. if (conf.omitNodes) {
  73. result = replaceNodeObjects(result);
  74. }
  75. return result;
  76. }
  77. exports.handlers = {};
  78. events.forEach(function(eventType) {
  79. exports.handlers[eventType] = function(e) {
  80. console.log( dump({
  81. type: eventType,
  82. content: cleanse(e)
  83. }) );
  84. };
  85. });