【问题标题】:Have jshint ignore certain files when building Twitter Bootstrap在构建 Twitter Bootstrap 时让 jshint 忽略某些文件
【发布时间】:2013-01-22 08:41:09
【问题描述】:

在将 Twitter Bootstrap 与其他 3rd-party JS 库一起使用时,我经常遇到这个问题,例如来自 WordPress 的“二十十二”主题的 html5.js,其中构建失败是因为 jshint(或以前版本中的 jslint我认为是 TB)由于第三方 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,因为这会导致以后升级出现问题;是我将第 3 方 JS 文件放入单独文件夹的唯一选择。

有没有办法让 jshint 或 TB 的构建过程忽略某些 JS 文件(也许通过一些我不知道的 .jshintrc 配置?)?

【问题讨论】:

    标签: twitter-bootstrap build makefile jslint jshint


    【解决方案1】:

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

    所以要解决我的问题/.jshintignore中的问题需要包含:

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

    就是这样!

    【讨论】:

    • 显然,一些通配符类型的魔法也有效,例如js/vendor/* - 如果您希望忽略供应商文件夹下的所有内容。
    • 感谢您发布此消息——我很惊讶.jshintignore is not currently documented
    • @HolisticDeveloper 确实,它似乎会成为现实应用程序中相当广泛使用的功能。
    • 这主要对我有用,但实际上我必须将我的 .jshintignore 文件放在项目根目录中的 .jshintrc 旁边
    • github项目中有一个示例文件:github.com/jshint/jshint/blob/master/examples/.jshintignore
    【解决方案2】:

    作为对 Lèse majesté 的answer 的补充,也可以在你的 JS 中编写这些 cmets,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 docs

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2018-05-30
        • 2016-07-28
        • 1970-01-01
        • 2011-03-21
        • 2015-02-17
        • 2012-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多