jsdoc-将文档重用于多个功能吗?

时间:2018-07-02 11:41:42

标签: jsdoc jsdoc3

我有一个带有大量选项的函数:

/**
 * Show dialog in a blocking manner.
 *
 * @param {object} opts
 * @param {string} opts.msg "Body" of the dialog.
 * @param {number} opts.timeout Seconds - floating point values are rounded. (ActiveX imposes this)
 * @param {string} opts.title Title of the dialog.
 * @param {number} opts.icon Use constants for this. (See docs)
 * @param {number} opts.buttons Use constants for this. (See docs)
 * @param {number} opts.defaultButton Use constants for this. (See docs)
 * @returns {number} Use our constants to check for what the user chose.
 */
const showSync = (opts) => {
...
}

但是我也有该功能的非阻塞版本,显然采用相同的选项并返回Promise。复制/粘贴文档似乎很脏,因为这会降低可维护性和意外不一致的可能性。

那么,很棒的事情如下:

/**
 * Shows dialog in a non-blocking manner.
 *
 * @inheritdoc showSync
 * @returns {Promise<number>} Use our constants to check for what the user chose.
 */
const show = (opts) => {
...
}

这有可能吗?

1 个答案:

答案 0 :(得分:2)

我认为用jsdoc做到这一点的最好方法是这样的:

/**
 * Options for showing a dialog.
 * @typedef {Object} ShowOptions
 * @property {string} msg "Body" of the dialog.
 * @property {number} timeout Seconds - floating point values are rounded. (ActiveX imposes this)
 * @property {string} title Title of the dialog.
 * @property {number} icon Use constants for this. (See docs)
 * @property {number} buttons Use constants for this. (See docs)
 * @property {number} defaultButton Use constants for this. (See docs)
 */

/**
 * Show dialog in a blocking manner.
 *
 * @param {ShowOptions} opts
 * @returns {number} Use our constants to check for what the user chose.
 */
const showSync = (opts) => {...}

/**
 * Shows dialog in a non-blocking manner.
 *
 * @param {ShowOptions} opts
 * @returns {Promise<number>} Use our constants to check for what the user chose.
 */
const show = (opts) => {...}

您可以更进一步,并将类似的概念应用于返回值:

/**
 * Use our constants to check for what the user chose.
 * @typedef {number} ShowResult 
 */

/**
 * Show dialog in a blocking manner.
 *
 * @param {ShowOptions} opts
 * @returns {ShowResult} 
 */
const showSync = (opts) => {...}

/**
 * Shows dialog in a non-blocking manner.
 *
 * @param {ShowOptions} opts
 * @returns {Promise<ShowResult>}
 */
const show = (opts) => {...}