Benj
Benj

Reputation: 1875

Can I set jshint options at the function level?

I'm using jshint to monitor my code quality, but I'd like to apply different rules/options to different parts of my code.

In particular, there's one function where I'm intentionally and necessarily using bitwise operators. For this function, I'd like to set /*jshint bitwise:false */. For the rest of my code, though, I'd like to set /*jshint bitwise:true */. Is there a way to do this, short of splitting that function into another script file? I imagine it would look something like this, but it looks like this doesn't actually work.

/*jshint bitwise:true */  //not really needed since it's default

function whatever () {
    // lots of code here
}

function uses_bitwise () {
    /*jshint bitwise:false */
    //bitwise code here
}

Upvotes: 2

Views: 1034

Answers (2)

Dan
Dan

Reputation: 151

Yes, your syntax in the question is correct. From The JSHint documentation:

In addition to the --config flag and .jshintrc file you can configure JSHint from within your files using special comments. These comments start either with jshint or global and are followed by a comma-separated list of value. For example, the following snippet will enable warnings about undefined and unused variables and tell JSHint about a global variable named MY_GLOBAL.

/* jshint undef: true, unused: true */
/* global MY_GLOBAL */

You can use both multi- and single-line comments to configure JSHint. These comments are function scoped meaning that if you put them inside a function they will affect only this function’s code.

Upvotes: 1

Benj
Benj

Reputation: 1875

Actually, it turns out that the exact syntax proposed in the question works!

Upvotes: 0

Related Questions