UNPKG

3.18 kBJavaScriptView Raw
1/**
2 * @overview Dump information about parser events to the console.
3 * @module plugins/eventDumper
4 * @author Jeff Williams <jeffrey.l.williams@gmail.com>
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 || {};
14var isRhino = require('jsdoc/util/runtime').isRhino();
15
16// Dump the included parser events (defaults to all events)
17var events = conf.include || [
18 'parseBegin',
19 'fileBegin',
20 'beforeParse',
21 'jsdocCommentFound',
22 'symbolFound',
23 'newDoclet',
24 'fileComplete',
25 'parseComplete',
26 'processingComplete'
27];
28// Don't dump the excluded parser events
29if (conf.exclude) {
30 events = _.difference(events, conf.exclude);
31}
32
33/**
34 * Check whether a variable appears to be a Java native object.
35 *
36 * @param {*} o - The variable to check.
37 * @return {boolean} Set to `true` for Java native objects and `false` in all other cases.
38 */
39function isJavaNativeObject(o) {
40 if (!isRhino) {
41 return false;
42 }
43
44 return o && typeof o === 'object' && typeof o.getClass === 'function';
45}
46
47/**
48 * Replace AST node objects in events with a placeholder.
49 *
50 * @param {Object} o - An object whose properties may contain AST node objects.
51 * @return {Object} The modified object.
52 */
53function replaceNodeObjects(o) {
54 var doop = require('jsdoc/util/doop');
55
56 var OBJECT_PLACEHOLDER = '<Object>';
57
58 if (o.code && o.code.node) {
59 // don't break the original object!
60 o.code = doop(o.code);
61 o.code.node = OBJECT_PLACEHOLDER;
62 }
63
64 if (o.doclet && o.doclet.meta && o.doclet.meta.code && o.doclet.meta.code.node) {
65 // don't break the original object!
66 o.doclet.meta.code = doop(o.doclet.meta.code);
67 o.doclet.meta.code.node = OBJECT_PLACEHOLDER;
68 }
69
70 if (o.astnode) {
71 o.astnode = OBJECT_PLACEHOLDER;
72 }
73
74 return o;
75}
76
77/**
78 * Get rid of unwanted crud in an event object.
79 *
80 * @param {object} e The event object.
81 * @return {object} The fixed-up object.
82 */
83function cleanse(e) {
84 var result = {};
85
86 Object.keys(e).forEach(function(prop) {
87 // by default, don't stringify properties that contain an array of functions
88 if (!conf.includeFunctions && util.isArray(e[prop]) && e[prop][0] &&
89 String(typeof e[prop][0]) === 'function') {
90 result[prop] = 'function[' + e[prop].length + ']';
91 }
92 // never include functions that belong to the object
93 else if (typeof e[prop] !== 'function') {
94 // don't call JSON.stringify() on Java native objects--Rhino will throw an exception
95 result[prop] = isJavaNativeObject(e[prop]) ? String(e[prop]) : e[prop];
96 }
97 });
98
99 // allow users to omit node objects, which can be enormous
100 if (conf.omitNodes) {
101 result = replaceNodeObjects(result);
102 }
103
104 return result;
105}
106
107exports.handlers = {};
108
109events.forEach(function(eventType) {
110 exports.handlers[eventType] = function(e) {
111 console.log( dump({
112 type: eventType,
113 content: cleanse(e)
114 }) );
115 };
116});