2016-08-18 64 views
0

我想要一個非常簡單的eslint規則,確保每個文件的頂部行都顯示'ouch'。像這樣的東西似乎工作 - 但我怎麼context.report它沒有節點?或者我如何獲得文件的第一行使用的節點? 我應該使用在'return'中聲明的回調來代替(如果是這樣,哪個事件)? 謝謝!如何獲得自定義eslint規則來執行context.report而無需節點?

'use strict'; 
function checkFirstLine (context) { 
    if (context.getSourceCode().lines[0] !== 'ouch') { 
     // How do I context.report without a node? 
    } 
} 

module.exports = { 
    meta : { 
     docs : { 
      description : 'check first line', 
      category : 'Possible Errors', 
      recommended : true 
     }, 
    }, 
    create : function (context) { 
     checkFirstLine (context); 
     return {}; 
    } 
}; 

回答

0

就想通了,我可以用:

context.report(context.getSourceCode().ast, 'Cripes');