Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set JSHint options on per directory basis

Tags:

jshint

I see that the ability to specify JSHint options on a per directory basis was added here.

However it is not clear to me how you actually take advantage of this. What do I do to set JSH options in a single directory, so that the options differ from other directories?

like image 740
Icarus Avatar asked May 17 '14 19:05

Icarus


1 Answers

It appears that the change in question actually allows you to specify overriding options on a per-file basis. You can add an overrides property to your config, the value of which should be an object. The keys of this object are treated as regular expressions against which file names are tested. If the name of the file being analysed matches an overrides regex then the options specified for that override will apply to that file:

There's an example of this in the cli.js test file diff in the commit you linked to:

{
    "asi": true,
    "overrides": {
        "bar.js$": {
           "asi": false
        }
    }
}

In that example there is a single override which will apply to any files that match the bar.js$ regular expression (which looks like a bit of an oversight, since the . will match any character and presumably was intended to only match a literal . character).


Having said all that, it doesn't look like the overrides property is going to help you. I think what you actually want is a new .jshintrc file in the directory in question. JSHint looks for that file starting in the directory of the file being analysed and moves up the directory tree until it finds one. Whichever it finds first is the one that gets used. From the docs:

In case of .jshintrc, JSHint will start looking for this file in the same directory as the file that's being linted. If not found, it will move one level up the directory tree all the way up to the filesystem root.

A common use case for this is to have separate JSHint configurations for your application code and your test code. This allows you to define the different environments and globals separately.

like image 171
James Allardice Avatar answered Oct 13 '22 18:10

James Allardice