当构建Twitter Bootstrap时,如何让jshint忽略特定的文件?

39

当我在使用Twitter Bootstrap与其他第三方JS库(例如来自WordPress“ Twenty Twelve”主题的html5.js)时,经常会遇到这个问题,构建失败,因为jshint(或者我认为在以前的TB版本中是jslint)由于第三方JS库而抛出错误,例如:

\n##################################################
Building Bootstrap...
##################################################\n
js/html5.js: line 3, col 122, Expected ')' to match '(' from line 3 and instead
  saw ','.
js/html5.js: line 3, col 140, Expected an identifier and instead saw ')'.
js/html5.js: line 4, col 101, eval is evil.
js/html5.js: line 6, col 369, Confusing use of '!'.
js/html5.js: line 6, col 422, Confusing use of '!'.
js/html5.js: line 7, col 61, 'b' is already defined.
js/theme-customizer.js: line 26, col 26, Use '===' to compare with ''.

7 errors
make: *** [build] Error 1

我希望避免修改第三方库或修改TB的Makefile,因为这会在未来升级时造成问题;我唯一的选择是将第三方JS文件放入单独的文件夹中。

是否有办法让jshint或TB的构建过程忽略某些JS文件(可能是通过某些我不知道的.jshintrc配置)?

3个回答

99

因此,在jshint中有一个忽略文件的选项,但它不是在.jshintrc中设置的,而是一个单独的文件.jshintignore,这很合理。但是,虽然jshint会查找项目子目录中的.jshintrc,但.jshintignore需要放在项目根目录中。所以对于Twitter Bootstrap来说,.jshintrc/js中,而.jshintignore需要放置在/中。

因此,为了解决我问题中的问题,/.jshintignore需要包含:

js/html5.js
js/theme-customizer.js

就是这样了!


5
显然,一些通配符类型的魔法也能奏效,例如“js/vendor/*”——如果你想忽略vendor文件夹下的所有内容。 - demaniak
2
谢谢您发布这个 -- 我很惊讶 .jshintignore 目前没有被记录在文档中 - Holistic Developer
1
在Github项目中有一个示例文件:https://github.com/jshint/jshint/blob/master/examples/.jshintignore。 - ThorSummoner
尝试忽略整个项目(使用eslint代替),但*, ./***/* 都没有起作用。 - Mild Fuzz
@MildFuzz 你把这些放在项目根目录下的.eslintignore里了吗?根据文档,应该可以工作。 - Lèse majesté
显示剩余2条评论

35

作为对Lèse majesté的回答的补充,你还可以在JS中编写这些注释,并且jshint将忽略它们之间的代码:

// Code here will be linted with JSHint.
/* jshint ignore:start */
// Code here will be linted with ignored by JSHint.
/* jshint ignore:end */

或者,如果你只想让 JSHint 不检查一行代码:

 // jshint ignore:line

参考资料:jshint文档


15

对我来说最简单的解决方案就是在我想要忽略的任何文件的顶部添加// jshint ignore: start


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