9

I'm trying to create a plugin for JSDoc. I'm following the documentation (which, ironically, is lacking) and I'm not sure how to do this.

My plugin is loaded properly, and I'm trying a simple example. Here's my plugin (which loads, because I can throw an error from there to stop jsdoc from running):

visitNode: function(node, e, parser, currentSourceName) {

    if(node.type === 109){
        if(!e.comment || e.comment ==="@undocumented"){

            var startComment = '/**',
                endComment = '\n*/';
            var params = node.getParams(),
                paramsComment = '';
            for(var i=0; i<params.length; i++){
                paramsComment += '\n* @param ' + params[i];
            }

            e.comment = startComment +
                paramsComment +
                endComment;
        }
    }

please note that node.type === 109 is equivalent to Token.FUNCTION, which should be available as per their example here, but Token is undefined in the plugin.

If you know of a better site which explains how to write a JSDoc plugin, then that would be very much appreciated too... thanks

Etai
  • 1,363
  • 1
  • 10
  • 13

1 Answers1

2

I also had this problem and it seems strange that JSDoc does not have some kind of already made option for that or at least a plugin.

Anyway creating this plugin has solved my problem. I am using JSDoc version 3.4:

'use strict';
exports.handlers = {

    symbolFound:function(e) {
        if(e.astnode.type === "FunctionDeclaration" ) {
            if( (e.comment==="@undocumented")){
                e.comment = '/** undocumented */';
            }
        }
    }
};
Natan Rubinstein
  • 635
  • 1
  • 9
  • 24
  • Great pointer! [`eventDumper`](https://github.com/jsdoc3/jsdoc/blob/master/plugins/eventDumper.js) is a great sample plugin to review if you want to see the **jsdoc** processing pipeline where this above strategy is hooked in. Seems like [adding comments is the best route](https://stackoverflow.com/q/21285960/175679) to go though - jsdoc is not as robust as [sphinx or docfx](https://docs.microsoft.com/en-us/aspnet/core/#lf-content=177014839:612203132) when code isn't commented to start with. – SliverNinja - MSFT Jul 04 '17 at 18:32