我可以在 JSDOC 中使用变量吗

Can I have variables inside of JSDOC

我有几个函数共享一些底层数据结构,同时也做非常不同的事情,因此抽象并不是一个好主意。

文档可能如下所示(虽然这只是一个小示例,许多方法只共享此文档的一部分):

/**
 * Creates an array of objects, with each object containing the average distance that minute. The
 * objects have the keys **Timestamp** (ms since epoch), **Year** (year in device local time) **Month** (
 * month in device local time), **Day** (day in device local time), **Hour** (hour in device local time)
 * **Season** (season in device local time, Northern Hemisphere), **Weekday** (Week day name in device
 * local time), **WeekNum** (week number (1-53) in device local time), **Depth** (the average depth that
 * minute), and **Vibration** (the magnitude of the maximum acceleration for the minute, in Gs).
 * <snip>
 */

/**
 * Creates an array of objects, with each object containing the data for one minute of temperature data
 * from temperature probes. The objects have the keys **Timestamp** (ms since epoch), **Year** (year in
 * device local time) **Month** (month in device local time), **Day** (day in device local time), **Hour**
 * (hour in device local time) **Season** (season in device local time, Northern Hemisphere), **Weekday**
 * (Week day name in device local time), **WeekNum** (week number (1-53) in device local time),
 * **Temperature** (Temperature measured by the temperature probe), **Vib**(the standard deviation of the
 * acceleration of the accelerometer, in Gs).
 * <snip>
 */

正如您从示例中看到的,我的振动文档及其含义不一致。我不想每次更改它的含义(或者更糟糕的是,硬件工程师更改它的含义)时都必须去修复 6 个地方的文档。有没有办法让我有一个全球术语词典并在适当的时候插入它?类似于:

terms.json
    > {vibDef: "the magnitude of the maximum acceleration for the minute, in Gs"}

code.js
    > /**
       * Creates an array of objects, with each object containing the average distance that minute. The
       * objects have the keys **Timestamp** (ms since epoch), **Year** (year in device local time) **Month** (
       * month in device local time), **Day** (day in device local time), **Hour** (hour in device local time)
       * **Season** (season in device local time, Northern Hemisphere), **Weekday** (Week day name in device
       * local time), **WeekNum** (week number (1-53) in device local time), **Depth** (the average depth that
       * minute), and **Vibration** (<<vibDef>>).
       * <snip>
       */

这会在文档字符串中找到的任何地方插入我对 vibDef 的定义吗?

感谢@ssube 的建议,我编写了一个插件,将 !><! 之间的文本写入扩展为更长的定义,保存在全局文件中。作为记录,这里是:

var globalDict = require('../globals.js').jsDoc;


exports.handlers = {
    beforeParse: function(e) {
        var reg = /!>(?=\S)(\w+)(?=\S)<!/;
        do {
            m = reg.exec(e.source);
            if (m) {
                var originalTxt = m[0];
                var expandedDef = globalDict[m[1]];
                if (expandedDef) {
                    e.source = e.source.replace(originalTxt, expandedDef);
                } else {
                    e.source = e.source.replace(originalTxt, m[1]); // Prevent infinite loop
                    console.log('Error: Missing definition for jsDoc keyword', m[1]);
                }
            }
        } while (m);
    }
};

globals.js 看起来像:

exports.jsDoc = {
    vibration: "twice the standard deviation in the recorded acceleration over the course of a minute"
};