如何配置Syntastic与JSHint?

44

如何使用Syntastic Vim插件和JSHint验证JavaScript代码?

环境:

  • Ubuntu 11.04
  • VIM - Vi IMproved 7.3

我按照 VIM + JSLint? 中的解决方案安装了以下内容:

  • Vundle
  • node.js
  • Node Package Manager
  • 全局安装jshint
  • 通过Vundle安装Syntastic(在Vim中使用:BundleInstall命令确保已安装Syntastic。)

.vimrc:

set nocompatible               " be iMproved
filetype off                   " required!

set rtp+=~/.vim/bundle/vundle/
call vundle#rc()

" let Vundle manage Vundle
" required! 
Bundle 'gmarik/vundle'

" My Bundles here:
Bundle 'scrooloose/syntastic'

filetype plugin indent on     " required! 

let g:syntastic_enable_signs=1
let g:syntastic_auto_jump=1
let g:syntastic_stl_format = '[%E{Err: %fe #%e}%B{, }%W{Warn: %fw #%w}]'

set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*

查找已安装的可执行文件:

$ which gjslint
$ which jslint
$ which jsl
$ which jshint
/home/fernando/local/node/bin/jshint
$ 


$ echo $PATH

>/home/fernando/local/bin:/home/fernando/local/node/bin:/home/fernando/node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

$ jshint test.js

test.js: 第3行,第1列,“blbla”未定义。
test.js: 第4行,第1列,“x”未定义。
test.js: 第4行,第5列,“nonono”未定义。
test.js: 第6行,第1列,“a”未定义。
test.js: 第7行,第1列,“b”未定义。
test.js: 第8行,第5列,“a”未定义。
test.js: 第8行,第10列,“b”未定义。
test.js: 第8行,第7列,预期为“===”,实际为“==”。

共8个错误

$ vi test.js -- no error message shown

:SyntasticEnable -- Vim exits and restarts, opening the same file, but still no message

:w -- still no error message

:Errors -- the location list opens but it is empty

jshint和Syntastic都已安装,但可能缺少某些东西。那是什么呢?


非常奇怪!我刚刚在Linux上使用Vim 7.3尝试了Syntastic,没有遇到任何问题。也许应该在GitHub上报告一下这个问题? - a paid nerd
3个回答

68

这里有更更新的信息,可以通过在你的.vimrc中配置将文件扩展名与检查器相关联。

let g:syntastic_javascript_checkers = ['jshint']

另外,要获取有关正在进行的信息,请在vim中运行此命令

:SyntasticInfo

你将得到类似于这样的输出:

Syntastic info for filetype: javascript  
Available checkers: gjslint jshint  
Currently active checker(s): gjslint  
Press ENTER or type command to continue

5
你是不是指问题中应该使用jshint而不是jslint - Ehtesh Choudhury
我在这里确认这个代码是可行的:let g:syntastic_javascript_checkers = ['jshint'] - Fernando Correia
仅供参考,在2018年... jshint似乎是Syntastic的默认选择。我从未完成过第一部分,但在2018年,当我运行info命令后,我看到可用检查器:jshint。当前启用的检查器:jshint - Tommy

6
我刚遇到了这个同样的问题。结果发现,Syntastic 在查找 jshint 之前会先查找 jsl (JSLint)。如果你两个都安装了,你可能会被误导。我将 jsl 移到了不在 PATH 中的地方,然后 jshint 就被正确检测到了。
来源:
https://github.com/scrooloose/syntastic/pull/47

这是有用的建议,但在我的情况下,我不认为我将jsl添加到路径中,因为我使用“which jsl”命令来查找它。 - Fernando Correia
我在PATH中拥有“jslint”和“jshint”两个工具。但由于某些原因,Syntastic没有展示jslint的反馈信息。将jslint从PATH中移除后,Syntastic便使用了jshint,这对我来说解决了问题。 - Niels Bom

0

我为这个问题苦苦挣扎了好几天,即使我在我的.vimrc中添加了let g:syntastic_javascript_checkers['jshint']SyntasticInfo仍然无法识别jshint作为可用的检查器。我正在使用Ubuntu 15.04,并遵循this教程在Ubuntu上安装nvm、nodejs和jshint。之后,我发现如果我从我的.vimrc中删除filetype plugin indent on这一行,然后打开一个.js文件,一切都工作得非常完美!总结一下...

  1. 请按照上面链接的教程进行操作
  2. .bashrc中将~/.nvm/<version_number>/bin添加到路径中
  3. .vimrc中删除filetype plugin indent on
  4. .vimrc中添加let g:syntastic_javascript_checkers['jshint']
  5. 在vim中输入:so %
  6. 打开一个.js文件
  7. filetype plugin indent on重新添加到您的.vimrc
  8. 现在一切都神奇地工作了

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接