JSDoc文档化socket.on('event', function() {})和路由处理程序的正确方法

19

如何使用JSDoc记录具有以下格式(单个文件)的API

// api.js

exports.addSocketEvents = function(socket) {
   /**
    * This will do that and ...
    * @param {Object} data Some data
    * @param {string} data.bla Something about bla
    * @param {number} data.n Some number
    */
   socket.on('something_1', function(data) { ... });

   /**
    * Another function that will do ..
    * @param {string} id of something
    */
   socket.on('something_2', function(id) { ... });

   ...
};

exports.addRoutes = function(app) {
    /**
     * PATCH /something/:id/juhu
     * Will do this and that and will respond with ...
     * @param {string} _id Id of bonus document
     */
    app.patch('/something/:id/juhu', function(req, res) {...});

    /**
     * GET /something
     * Will fetch and respond back with ...
     */
    app.get('/something', function(req, res) {...});
    ...
};

我唯一的想法是在导出函数之前添加@namespace,在匿名函数之前添加@lends,但这会导致文档为空。

1个回答

1
我正在使用Angular和Angular JSDoc文档。因此,我会像以下示例一样记录我的父类和函数。
/**
 * @class MyApp.Teams
 * @ngdoc class
 * @memberOf MyApp
 * @description
 * Module to handle the interface to the Teams, data and views.
 */

angular.module('MyApp').factory( ...

/**
 * @name TeamRecord
 * @ngdoc factory
 * @memberOf MyApp.Teams
 * @returns Record Clears the Structure to  ""
 * @description
 * Team Data Record structure
 */

因此,根据您上面的文本,它可能看起来像:

/**
 * @class MyApp.socketio
 * @ngdoc class
 * @memberOf MyApp
 * @description
 * Module to handle the interface to the Teams, data and views.
 */

/**
 * @name addSocketEvens
 * @ngdoc function
 * @memberOf MyApp.socketio
 * @param {Object} data Some data
 * @param {string} data.bla Something about bla
 * @param {number} data.n Some number
 * @description
 * This will do that and ...
 */
exports.addSocketEvents = function(socket) {
   socket.on('something_1', function(data) { ... });

   /**
    * Another function that will do ..
    * @param {string} id of something
    */
   socket.on('something_2', function(id) { ... });

   ...
};

/**
 * @name addRoutes
 * @ngdoc function
 * @memberOf MyApp.socketio
 * @param {string} _id Id of bonus document
 * @description
 * Will do this and that and will respond with ...
 */
exports.addRoutes = function(app) {
    app.patch('/something/:id/juhu', function(req, res) {...});

    /**
     * GET /something
     * Will fetch and respond back with ...
     */
    app.get('/something', function(req, res) {...});
    ...
};

运行jsdoc命令对这个文件进行文档化时,所有匿名函数都会被忽略(未记录)。此外,如果可能的话,我会尽量避免使用@class,因为在这种情况下,文档中将包含new socketio(),这可能会给阅读文档的人带来误导性线索。 - Srle
你可以删除 @class,因为我明白你为什么不想要它们。你是否在运行最新的 JSDoc?我也找到了这个问题。https://dev59.com/tWsz5IYBdhLWcg3wLUt2 - Steven Scott

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接