2016-06-07 106 views
2

我有几个共享某个底层数据结构的函数,同时也做了非常不同的事情,例如抽象并不是一个好主意。我可以在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无处不在文档字符串中找到它?

+1

我最后一次使用jsdoc,你可以关联自己的解析器(我们把它用于降价),因此增加一个为你的词汇表标签应该是可能的? – ssube

回答

1

感谢@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" 
}; 
相关问题