Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Should be able to use jshint for json filetype #1371

Closed
KylePDavis opened this issue Mar 27, 2015 · 11 comments
Closed

Should be able to use jshint for json filetype #1371

KylePDavis opened this issue Mar 27, 2015 · 11 comments

Comments

@KylePDavis
Copy link

I may be doing something wrong but I am trying to map the json filetype to use the jshint checker and I'm having issues getting it to work.

My stripped .vimrc looks like this:

autocmd BufNewFile,BufReadPost *.json set filetype=json syntax=javascript
call plug#begin()
Plug 'scrooloose/syntastic'  " multi-language syntax checker
let g:syntastic_check_on_open=1
let g:syntastic_json_checkers = ['jshint']
let g:syntastic_debug=15
call plug#end()

It works for JS files so I know that it can find jshint binary but for good measure.

$ which jshint
/Users/kdavis/homebrew/bin/jshint

Here are the debug messages when opening the file:

"bower.json" 18L, 424C
syntastic: 0.017600: autocmd: BufReadPost, buffer 1 = 'bower.json'
syntastic: 0.017807: &shell = '/bin/bash', &shellcmdflag = '-c', &shellpipe = '2>&1| tee', &shellquote = '', &shellredir = '>%s 2>&1', &shellslash = 0, &shelltemp = 1, &shellxquote
 = ''
syntastic: 0.017972: UpdateErrors (auto): default checkers
syntastic: 0.018244: CacheErrors: default checkers
syntastic: 0.018413: notifiers: reset
syntastic: 0.018590: highlighting: reset
syntastic: 0.018705: cursor: reset
syntastic: 0.019001: g:syntastic_aggregate_errors = 0
syntastic: 0.019154: getcwd() = '/Users/kdavis/tmp'
syntastic: 0.020003: CacheErrors: no checkers available for json
syntastic: 0.020090: aggregated: {'_sorted': 0, '_name': '', '_owner': 1, '_columns': 1, '_rawLoclist': []}
syntastic: 0.020436: notifiers: refresh
syntastic: 0.020606: signs: refresh
syntastic: 0.020877: highlighting: refresh
syntastic: 0.021107: autoloclist: refresh
syntastic: 0.021187: autoloclist: toggle
syntastic: 0.021361: autocmd: BufEnter, buffer 1 = 'bower.json', &buftype = ''
syntastic: 0.021465: notifiers: refresh
syntastic: 0.021631: signs: refresh
syntastic: 0.021901: highlighting: refresh
syntastic: 0.022128: autoloclist: refresh
syntastic: 0.022207: autoloclist: toggle

Here's the output from the :SyntasticInfo command:

Syntastic version: 3.6.0-65 (Vim 703, Darwin)
Info for filetype: json
Global mode: active
Filetype json is active
The current file will be checked automatically
Available checkers: -
Currently enabled checkers: -

I know that I could just install and use jsonlint (and I may eventually in the interest of correctness) but I thought that this should work.

Am I missing something? Any ideas why this isn't working?

@ddaza
Copy link

ddaza commented Mar 27, 2015

Add this to your vimrc

let g:syntastic_javascript_jshint_exec = '/Users/kdavis/homebrew/bin/jshint'

@KylePDavis
Copy link
Author

It was worth a shot but that didn't work.
It still says "no checkers available for json", the same as before.

@lcd047
Copy link
Collaborator

lcd047 commented Mar 27, 2015

The supported checkers for each filetype are listed in the wiki. You can't just call a javascript checker for a json file and expect it to work.

You can use g:syntastic_filetype_map to map json to javascript if you insist, but then all javascript checkers will also check json files, and any checker that you install later for json will be ignored.

let g:syntastic_filetype_map = { 'json': 'javascript' }

@lcd047
Copy link
Collaborator

lcd047 commented Mar 27, 2015

Another way to do it would be to set the filetype of json files to json.javascript:

augroup filetype
    autocmd! BufRead,BufNewFile    *.json    set filetype=json.javascript
augroup END

This will tell syntastic to use both json and javascript checkers to check json files.

@KylePDavis
Copy link
Author

Okay, I was wondering if it just wouldn't work that way.

I had tried the filetype map route and definitely had the issue that you're describing where unwanted JS checkers were being applied to my JSON files. So that doesn't work very well.

Maybe there's another way to allow jshint as an alternate checker for json files.

@lcd047
Copy link
Collaborator

lcd047 commented Mar 27, 2015

There isn't, and for a very good reason:

$ jshint test.json 
test.json: line 2, col 23, Strings must use singlequote.
test.json: line 3, col 24, Strings must use singlequote.
test.json: line 3, col 30, Strings must use singlequote.
test.json: line 4, col 23, Strings must use singlequote.
test.json: line 5, col 11, Strings must use singlequote.
test.json: line 6, col 13, Strings must use singlequote.
test.json: line 7, col 12, Strings must use singlequote.
test.json: line 8, col 14, Strings must use singlequote.
test.json: line 9, col 11, Strings must use singlequote.
test.json: line 10, col 12, Strings must use singlequote.
test.json: line 11, col 14, Strings must use singlequote.
test.json: line 12, col 13, Strings must use singlequote.
test.json: line 13, col 16, Strings must use singlequote.
test.json: line 15, col 30, Strings must use singlequote.
test.json: line 16, col 11, Strings must use singlequote.
test.json: line 17, col 13, Strings must use singlequote.
test.json: line 18, col 12, Strings must use singlequote.
test.json: line 19, col 14, Strings must use singlequote.
test.json: line 20, col 11, Strings must use singlequote.
test.json: line 21, col 15, Strings must use singlequote.
test.json: line 22, col 15, Strings must use singlequote.
test.json: line 23, col 12, Strings must use singlequote.
test.json: line 24, col 14, Strings must use singlequote.
test.json: line 26, col 31, Strings must use singlequote.
test.json: line 27, col 11, Strings must use singlequote.
test.json: line 28, col 13, Strings must use singlequote.
test.json: line 29, col 12, Strings must use singlequote.
test.json: line 30, col 14, Strings must use singlequote.
test.json: line 31, col 11, Strings must use singlequote.
test.json: line 32, col 15, Strings must use singlequote.
test.json: line 33, col 12, Strings must use singlequote.
test.json: line 34, col 14, Strings must use singlequote.
test.json: line 36, col 33, Strings must use singlequote.

33 errors

$ jsonval test.json
test.json: valid JSON

@lcd047 lcd047 closed this as completed Mar 27, 2015
@KylePDavis
Copy link
Author

I see. It looks like your configuration prevents jshint reporting errors in that JSON file.
Can't you use a JSON-friendly config (possibly the defaults if you tell it no config at all, actually).

@lcd047
Copy link
Collaborator

lcd047 commented Mar 27, 2015

The point is, jshint is typically not very useful for checking JSON. There are JSON checkers for checking JSON, and they work well.

@KylePDavis
Copy link
Author

@lcd047, it's true. I was just trying to figure out why it didn't work the way that I thought it ought to.
Now that I've looked deeper I see that its running the plugin that wraps the binary rather than running the binary directly so I understand more clearly why it doesn't work that way.

@NikolayFrantsev
Copy link

There are no JSON checkers with commens support, so jshint is preferred choice. You can use eslint for JS and jshint for JSON (with different quotes style in configs):

" use eslint for js
let g:syntastic_javascript_checkers = ['eslint']

" use jshint for json
let g:syntastic_filetype_map = {'json': 'javascript'}
autocmd! FileType json let b:syntastic_checkers = ['jshint']

@Leeiio
Copy link

Leeiio commented Mar 13, 2016

Please use jsonlint to check the json files.

" use jsonlint for json
let g:syntastic_json_checkers = ['jsonlint']

You should npm i -g jsonlint first.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants