-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Look for .jshintrc's in the directory being linted? #833
Comments
Uh, I'd rather extend the default config to accept pre-directory entries: {
"global": { "strict": true },
"test/**": { "strict": false }
} Or something like that. But that'd be another ticket. |
I think there are other reasons to do this rather than multiple .jshintrc files for project (although that would be a nice side-benefit). As a developer, when I put a .jshintrc file above a .js file in my project, I'm saying that I want that rc file to apply to that js file - where I'm running jshint from isn't really relevant to me, I just want those files checked correctly, without having to worry about the cwd, or more significantly, where my editor is running when it's doing linting. I can't stress how annoying it is to have vim throw a bunch of errors at me, just because I forgot to cd before opening a .js file. Then I've got to close it, cd, and re-open it. Makes all my nice FuzzyFinder fast open shortcuts kinda useless. If you're willing to reconsider the feature, I'd be happy to put together a patch for this. I did a bit of research, and while looking up a .jshintrc per file is a bit more work, the drawbacks could be minimized by memoizing the .jshintrc file lookup by target path. I think most of the work would come from re-working the config tests. TL;DR: Lookup relative to linted file makes life easier, better represents developer intent, & shouldn't add significant overhead to the linting process. |
I'd like this too. |
+1. It would really help for example when you've got a web and a node directory that you want to apply different rules to (like browser:true or globals), but have some common settings in the parent directory that apply to both (like the strict rules) |
Perhaps the config is determined by a sort of "inheritance" pattern? Maybe the directory of the file is checked first, and then the script traverses up the directory tree and gathers all the available configs in the hierarchy together. Then it can either go top-to-bottom (closer to root takes precedence) or bottom-to-top. (closer to target file takes precedence) With the "bottom-to-top" approach, you could easily set "global" config rules for a project, and allow specific directories to override only as-needed. Certainly makes pinning down exactly what rules are applied where a bit trickier, but that complexity is only introduced by conscious effort on the part of a project. |
Yes I was imagining something like that, I think you'd want the configs closer to the target file to take precidence really. The "pinning down errors" issue is also offset by the flexibility, and the fact that the rules for determining the config are relatively easy to understand/explain |
I'm actively working on a pull request that should handle this. The goal is to work exactly like the current implementation, expect instead of searching for .jshintrc relative to the current directory, it searches relative to the parent directory of the file being checked. Hopefully I'll have something ready within the next week. |
This patch updates the logic that looks for a .jshintrc file to use to search relative to the file being linted rather than the current working directory. If no file is found, config still falls back to ~/.jshintrc. This behavior should better reflect user intent when linting files, regardless of where jshint (or the editor leveraging it for checks) is run from. Since config lookup is now per-file instead of per-run, the internal findFile method has been memoized to avoid redundant searching. References: Fixes jshintGH-833
Any chance of getting this pull request merged in? I'm currently working on a node application that hosts some public javascript files using jQuery. It doesn't make sense to just consider jQuery a global for the node files. And there are quite a few public files so configuring via comments just seems tedious. |
I'm kind of looking for something like this too. I'd like to be able to specify some extra What do you suggest if multiple option files are off the table? |
This patch updates the logic that looks for a .jshintrc file to use to search relative to the file being linted rather than the current working directory. If no file is found, config still falls back to ~/.jshintrc. This behavior should better reflect user intent when linting files, regardless of where jshint (or the editor leveraging it for checks) is run from. Since config lookup is now per-file instead of per-run, the internal findFile method has been memoized to avoid redundant searching. References: Fixes jshintGH-833
+1 for this concept. See, for example, https://github.com/emberjs/ember.js/blob/master/.jshintrc. That project defines all of the test-related globals for all files, which is slightly dangerous. I could easily see a programmer accidentally typing For this particular project, the pattern-matched globals would work much better: {
"*": { "strict": true },
"packages/*/tests/**": { "globals": [ "QUnit", "..." ] }
} That leaves the question of whether to merge or replace those Arrays. The "closest Still, I think the "closest |
Another option that would work for our case and for ember/ember.js would be to allow jshint lib/foo.js lib/bar.js
jshint spec/foo_spec.js spec/bar_spec.js --extraGlobals="expect,it,describe" |
I have the same needs. I want to extend my JSHint options for my @jamesarosen You might also want to define linting rules for a file glob pattern such as |
I've just run into the same problem. I have a global |
So it says this is closed due to outsideris@7eb565f but I don't experience the behavior of .jshintrc files overriding the topmost .jshintrc. Am I missing something? How do we apply options on a per directory basis? |
@icarus-to-the-sun |
I am very interested in merging behavior, perhaps with an opt-in key, |
@denis-sokolov Just open a pull request if you have code. |
I don't, I just wanted to help organizing issues. |
This patch updates the logic that looks for a .jshintrc file to use to search relative to the file being linted rather than the current working directory. If no file is found, config still falls back to ~/.jshintrc. This behavior should better reflect user intent when linting files, regardless of where jshint (or the editor leveraging it for checks) is run from. Since config lookup is now per-file instead of per-run, the internal findFile method has been memoized to avoid redundant searching. References: Fixes jshintGH-833
AFAICS this was fixed by goonoo#2 and can be closed. |
Given:
I tried
but neither of my
.jshintrc
s were found. This is expected behavior, I guess, since the readme says it looks in the CWD. Still, it's pretty awkward: now I have to doAnd I guess it would be impossible to have nested
.jshintrc
s.Any chance on using the closest
.jshintrc
when linting a file? I know that's a pretty big and probably backward-incompatible change though, so if not, I understand.The text was updated successfully, but these errors were encountered: