UNPKG

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