I'm trying to get JSDOC (3) to properly document my NodeJS modules. While simple modules work, I can't find the way to document a slightly more complex module.
My NodeJS module has the following structure:
/** @module MyModule */
(function() {
function functionToExportOne {...}
function functionToExportTwo {...}
/**
* @module MyModule.ClassToExportOne
* @constructor
*/
function ClassToExportOne {
this.classMethodOne=function() { ... }
this.classMethodTwo=function() { ... }
}
/** @constructor */
function ClassToExportTwo {
function classMethodOne() { ... }
function classMethodTwo() { ... }
return /** @lends {ClassToExportTwo.prototype} */ {
methodOne:classMethodOne,
methodTwo:classMethodTwo
}
}
/** @exports MyModule */ <--- late addition, see comments
module.exports={
functionOne:functionToExportOne,
functionTwo:functionToExportTwo,
classOne:ClassToExportOne,
classTwo:ClassToExportTwo
}
})()
Well, as you see, the module exports both methods and class constructors. Nothing very strange.
The problem is, JSDOC
- always fails to recognize the inner classes,
- Mistakes the first inner class methods as the (outer) module's methods,
- always skips the real exported module methods.
- The closest I've been is, in
ClassToExportOne
, when I use@module MyModule.<className>
followed by@constructor
(in this order) : In this case the class is recognized, but only the constructor is documented, and its methods are documented as belonging to the parent module
I've tried thousands of different combinations without success:
- defining inner classes methods with
this.method=function() {...}
(as depicted inClassToExportOne
) or returning an object with methods using@lends {ClassToExportTwo.prototype}
(as depicted inClassToExportTwo
) - using
@module MyModule.ClassToExportOne
in the header of each class - defining module.exports vs exports.xxxx
- declaring inner classes
@private
, declaring everything@private
but the exports - moving
@module
declaration to just before the first exports (total mess then, some stuff gets promoted to "global" even though it's inside the main closure!!) - if I add
@exports <modulename>
just before the exports declaration, the methods are documented (mixed with the class methods)
I've tried everything suggested in http://usejsdoc.org/howto-commonjs-modules.html without success.
I'm sure I'm missing something, but can't find what.
I hate to answer my own question, but with a little more trial and error I got quite satisfactory results. Please feel free to comment if this is a correct approach, but the fact is the documentation is now properly generated :)
The key seems to be to use
@exports <modulename>.<classname>
before@constructor
, this way the class appear in the index under "classes", and with its methods properly documented IF the methods are declared withthis.method=function() {...}
I still would like to find a way to make this work, if at all possible, for the case depicted in ClassToExportTwo , sometimes it's very convenient for me to define a class like this, specially for long classes that call private methods so I can avoid totally using "this" inside the class (optimization).
Well here's the template that works for me: