检测不需要的function(fdescribe,describe.only)作为Gulp任务

如何在Node中检测代码库中不需要的函数的使用,特别是Gulp?

我正在检查无意中损坏的规格,即ddescribe / fdescribeiit / fit茉莉花或.only.skip为摩卡:

 // should be reported fdescribe(function () { // should not be reported it(function () { var fit = ...; this.fit = ...; }); // should not be reported // fit(function () { ... }); // should be reported xit(function () { ... }); // should be reported fit(function () { ... }); }); 

 // should be reported describe.only(function () { // should not be reported it(function () { ... }); // should not be reported // it.only(function () { ... }); // should be reported it.skip(function () { ... }); // should be reported it.only(function () { ... }); }); 

该任务应该退出,并输出文件名和所列函数使用的行号。

注释的人肯定不必被检测,以及具有相同名称的函数/属性(最有可能fit ),所以简单的正则expression式匹配在这里不是一个选项(就像console.* )。 一些基于AST的解决scheme,接受用户定义的函数名称将不胜感激。

我将通过ESLint javascript ESLint实用程序在静态分析步骤中解决此问题。 为了捕获意外遗留在代码库中的独占/重点的摩卡规格,在eslint-plugin-mocha插件中实现了一个no-exclusive-tests规则 :

摩卡有一个function,允许您只通过附加到testing套件或testing案例来运行testing。 此function对debugging失败的testing非常有用,因此您不必执行所有testing。 在修改了testing之后,在提交更改之前,必须先删除.only以确保所有testing都在您的构build系统上执行。

这条规则提醒您,只要您使用排他性function,就会通过提出警告从您的testing中删除。

如果你想绑eslint run来eslint – 使用gulp-eslint插件。


在git钩子提交之前运行eslint任务也是一个好主意。 我们使用了pre-git包来安装和跟踪git钩子。

这样,重点testing或排他性testing不会进入代码库。