eventDumper.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * Dump information about parser events to the console.
  3. *
  4. * @module plugins/eventDumper
  5. */
  6. const _ = require('underscore');
  7. const doop = require('jsdoc/util/doop');
  8. const dump = require('jsdoc/util/dumper').dump;
  9. const env = require('jsdoc/env');
  10. const util = require('util');
  11. const conf = env.conf.eventDumper || {};
  12. // Dump the included parser events (defaults to all events)
  13. let 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. const OBJECT_PLACEHOLDER = '<Object>';
  36. if (o.code && o.code.node) {
  37. // don't break the original object!
  38. o.code = doop(o.code);
  39. o.code.node = OBJECT_PLACEHOLDER;
  40. }
  41. if (o.doclet && o.doclet.meta && o.doclet.meta.code && o.doclet.meta.code.node) {
  42. // don't break the original object!
  43. o.doclet.meta.code = doop(o.doclet.meta.code);
  44. o.doclet.meta.code.node = OBJECT_PLACEHOLDER;
  45. }
  46. if (o.astnode) {
  47. o.astnode = OBJECT_PLACEHOLDER;
  48. }
  49. return o;
  50. }
  51. /**
  52. * Get rid of unwanted crud in an event object.
  53. *
  54. * @param {object} e The event object.
  55. * @return {object} The fixed-up object.
  56. */
  57. function cleanse(e) {
  58. let result = {};
  59. Object.keys(e).forEach(prop => {
  60. // by default, don't stringify properties that contain an array of functions
  61. if (!conf.includeFunctions && util.isArray(e[prop]) && e[prop][0] &&
  62. String(typeof e[prop][0]) === 'function') {
  63. result[prop] = `function[${e[prop].length}]`;
  64. }
  65. // never include functions that belong to the object
  66. else if (typeof e[prop] !== 'function') {
  67. result[prop] = e[prop];
  68. }
  69. });
  70. // allow users to omit node objects, which can be enormous
  71. if (conf.omitNodes) {
  72. result = replaceNodeObjects(result);
  73. }
  74. return result;
  75. }
  76. exports.handlers = {};
  77. events.forEach(eventType => {
  78. exports.handlers[eventType] = e => {
  79. console.log( dump({
  80. type: eventType,
  81. content: cleanse(e)
  82. }) );
  83. };
  84. });