2016-06-07 55 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" 
}; 
相關問題