Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to describe js module with jsdoc

Tags:

jsdoc

jsdoc3

Please, explain to me the best way to describe this module:

/**
 * Common util methods
 * @module Utils
 */
var Utils = (/** @lends module:Utils */
    function () {

    /**
     * Some default value
     * @constant
     * @public
     * @static
     * @type {string}
     */
    var staticConstantField = "Some value";

    //export to public access
    var exports = {
        staticConstantField: staticConstantField,
        getUrlArgs: getUrlArgs,
        isJSON: isJSON
    };

    return exports;

    /**
     * Return url arguments as associate array
     * @public
     * @static
     * @returns {Array} - url args
     */
    function getUrlArgs() {
        return [];
    }

    /**
     * Validate json
     * @public
     * @static
     * @param {string} json - json as string to validate
     * @returns {boolean}  - is json valid
     */
    function isJSON(json) {
        return true;
    }

    /**
     * Some private method
     * @private
     * @static
     * @param {string} json - json to parse
     * @returns {object}  - parsed object
     */
    function parseJson(json) {
        return {};
    }
})();

In this example my @public and @static annotation are ignored, all @public methods marks as "inner", @private methods marks as "private, inner", return statement is ignored. In generated docs I don't see what methods I can use as api ('exports' object in my code) and if I return

var exports = {
       anotherFieldName: staticConstantField,
       anotherGgetUrlArgsName: getUrlArgs,
       anotherIsJSONName: isJSON
};

this API would not show up in docs.

Generated documentation:

enter image description here

like image 633
Vova Avatar asked Nov 23 '22 10:11

Vova


1 Answers

A quick way to solve the problem at hand would be to add a @method methodName to each function. With this declaration the @static property is recognized and referenced in the generated jsdoc.

/**
 * Common util methods
 * @module utils
 */
var utils = (/** @lends module:utils */ function () {
  /**
   * this variable is neither @static nor @public it's a @constant in a private scope, you can add a @private if you like
   * @constant
   * @private
   */
  var myConstant = 'foo';
  /**
   * @method myMethod
   * @static
   */
  function myMethod(){}

  return {
    myMethod: myMethod
  };
})();

Never the less, I doubt that this construct should be considered a module rather than a namespaced object literal. Thus I would recommend to declare it like this:

/**
 * Common util methods
 * @namespace
 */
var utils = (/** @lends utils */ function () {
  /**
   * @memberOf utils
   */
  function myMethod(){}

  return {
    myMethod: myMethod
  };
})();
like image 193
Marcello di Simone Avatar answered Nov 25 '22 04:11

Marcello di Simone