UNPKG

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