简体   繁体   English

如何在JavaScript中调用特定函数?

[英]How to lint specific function call in JavaScript?

My team has had the occasional problem of developers pushing Karma/Protractor tests containing the .only() function call, which of course makes our Jenkins etc only run that particular test, potentially allowing bugs to slip by. 我的团队偶尔遇到开发人员推动包含.only()函数调用的Karma / Protractor测试的问题,这当然会使我们的Jenkins等仅运行该特定测试,从而可能会漏掉错误。 As such I thought I'd try and figure out a way to stop this from happening without being discovered. 因此,我想我会尝试找出一种方法来阻止这种情况发生而不会被发现。

First, I thought I'd look into simply using JSHint to point out the function call, but I can't seem to find a way to do that. 首先,我以为我会简单地使用JSHint来指出函数调用,但是我似乎找不到找到这种方法的方法。 I also looked at ESLint for its custom plugins, but I can't figure out how to write a plugin for this particular case. 我还查看了ESLint的自定义插件,但是我不知道如何为这种特殊情况编写插件。

Could you guys give me some ideas on how to solve this issue? 你们能给我一些解决这个问题的想法吗? Alternative solutions are also appreciated, of course! 当然,还可以选择其他解决方案!

Here's a (probably not working example) of how to create a plugin that flags an error if the parser ever sees a only() call. 这是一个(可能不起作用的示例)如何创建一个插件,如果解析器看到only()调用,该插件会标记错误。 Again, mileage may vary, but it should be enough to get you started. 同样,里程可能会有所不同,但足以让您入门。 This does not work if it sees a.only() , we'll leave that up to you. 如果看到a.only() ,这将a.only() ,我们a.only()您自己决定。

module.exports.rules = {
    "no-only-call": context => ({
        CallExpression: (node) => {
            if(node.callee.name == "only"){
                context.report(node, 'Calls to only() are disallowed');
            }
        }
    })
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM