Search code examples
vimjshintvim-pluginsyntasticeslint

How can I make Syntastic load a different checker based on existance of files in root?


At work we use a different syntax checker than I do when working on open source. Is there a way to have Syntastic specify a default checker, and change checkers if an rc file is found at the project root?

Example: if .eslintrc is found use eslint. If no .eslintrc is found, use standard.

Thanks!

edit: also opened an issue on scrooloose/syntastic.


Solution

  • Yes, you can do something like this:

    autocmd FileType javascript let b:syntastic_checkers = findfile('.eslintrc', '.;') != '' ? ['eslint'] : ['standard']
    

    Edit: Upon request, explanation of how this works:

    • autocmd FileType javascript - run the following stuff every time the filetype of a buffer is set to javascript (that is, normally once per buffer)
    • b:syntastic_checkers list of checkers enabled for the current buffer, overriding g:syntastic_javascript_checkers
    • findfile('.eslintrc', ...) - find a file named .eslintrc ...
    • .; - ... in the current directory and upwards
    • != '' ? - if found...
    • ['eslint'] - ... set b:syntastic_checkers to ['eslint']
    • : ['standard'] - ... otherwise set it to ['standard']

    Magic, I tell ya.