如何在 Node 中检测代码库中不需要的函数的使用,尤其是 Gulp?
我正在检查无意破坏的规范,即 ddescribe
/fdescribe
和 iit
/fit
Jasmine 或 .only
和 .skip
用于 Mocha:
// 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
),所以简单的正则表达式匹配在这里不是一个选项(喜欢它将用于 console.*
)。一些接受用户定义函数名称的基于 AST 的解决方案将不胜感激。
最佳答案
我将通过 ESLint
在静态分析步骤中解决它javascript linting 实用程序。为了捕捉代码库中不小心留下的独家/重点 Mocha 规范,有一个 no-exclusive-tests
rule在 eslint-plugin-mocha
plugin 中实现:
Mocha has a feature that allows you to run tests exclusively by appending
.only
to a test-suite or a test-case. This feature is really helpful to debug a failing test, so you don’t have to execute all of your tests. After you have fixed your test and before committing the changes you have to remove.only
to ensure all tests are executed on your build system.This rule reminds you to remove
.only
from your tests by raising a warning whenever you are using the exclusivity feature.
如果你想将 eslint
运行绑定(bind)到 gulp
- 使用 gulp-eslint
插件。
在 git 钩子(Hook)中运行 gulp
eslint
任务 before 也是一个好主意。我们使用了pre-git
用于安装和跟踪 git hooks 的包。
这样,重点测试或排他测试一开始就不会进入代码库。
关于node.js - 将不需要的功能(fdescribe、describe.only)检测为 Gulp 任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35485625/