Tickolas
Tickolas

Reputation: 113

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. 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. I also looked at ESLint for its custom plugins, but I can't figure out how to write a plugin for this particular case.

Could you guys give me some ideas on how to solve this issue? Alternative solutions are also appreciated, of course!

Upvotes: 0

Views: 953

Answers (1)

Ruan Mendes
Ruan Mendes

Reputation: 92284

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. 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.

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

Upvotes: 1

Related Questions