【问题标题】:JSHint globals option is not working in .jshintrcJSHint 全局选项在 .jshintrc 中不起作用
【发布时间】:2015-04-21 16:46:21
【问题描述】:

我正在使用Brackets 1.2 和扩展名brackets-jshint。这是我的项目根目录中的.jshintrc

{
    'bitwise': true,
    'boss': true,
    'camelcase': true,
    'curly': true,
    'devel': true,
    'eqeqeq': true,
    'eqnull': true,
    'expr': true,
    'forin': true,
    'iterator': false,
    'latedef': true,
    'multistr': false,
    'nocomma': true,
    'noarg': true,
    'noempty': true,
    'nonbsp': true,
    'nonew': true,
    'quotmark': 'single',
    'undef': true,
    'unused': true,
    'globals': {
        '$': true,
        'document': true,
        'jQuery': true,
        'window': true
    }
}

globals 选项不起作用,JSHint 仍在警告全局变量的白名单。

我也试过这些:

globals: true
jquery: true
devel: true 

但没有成功,$jquerywindowdocumentalert 仍然被警告。

【问题讨论】:

    标签: javascript jshint adobe-brackets


    【解决方案1】:

    您必须将.jshintrc 中的single 引号替换为double :) 这个答案很简短,所以我将添加一些解释......

    只需打开 Debug » Developers tools 尝试验证一些 JavaScript 文件 - 您可以在调试控制台中看到

    JSHint:解析 /project/path/.jshintrc 时出错。详细信息:SyntaxError: Unexpected token '

    负责读取 .jshintrc 的方法如下:

    try {
        config = JSON.parse(removeComments(content));
    } catch (e) {
        console.error("JSHint: error parsing " + file.fullPath + ". Details: " + e);
        result.reject(e);
        return;
    }
    

    JSON.parse 实现http://www.ietf.org/rfc/rfc4627.txt - 根据ECMAScript Specification 并且没有' 的位置。

    来自 rfc 4627 描述 JSON 的唯一有效结构是

    string = quotation-mark *char quotation-mark
    
    char = unescaped /
           escape (
               %x22 /          ; "    quotation mark  U+0022
               %x5C /          ; \    reverse solidus U+005C
               %x2F /          ; /    solidus         U+002F
               %x62 /          ; b    backspace       U+0008
               %x66 /          ; f    form feed       U+000C
               %x6E /          ; n    line feed       U+000A
               %x72 /          ; r    carriage return U+000D
               %x74 /          ; t    tab             U+0009
               %x75 4HEXDIG )  ; uXXXX                U+XXXX
    
    escape = %x5C              ; \
    
    quotation-mark = %x22      ; "
    

    【讨论】:

    • 对于为什么该代码在遇到单引号时会引发错误,可能值得添加更多解释。
    • 哦,是的。谢谢你。在此之前,我尝试在选项名称上不加引号,但它也不起作用。但是,它不仅适用于那些变量设置,其他选项也很好。
    • @Sithu 这些配置文件使用Json格式,只支持双引号,不支持完整的JS语法。
    【解决方案2】:

    请确保您已禁用 JSLint,这是默认情况下与 Brackets 一起提供的 linter。 你可以通过将这个 sn-p 添加到你的 brackets.json 来做到这一点(通过Debug > Show Preferences File 打开):

    "language": {
        "javascript": {
            "linting.prefer": "JSHint",
            "linting.usePreferredOnly": true
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-31
      • 1970-01-01
      • 2016-01-16
      • 2021-04-25
      • 2018-09-30
      • 1970-01-01
      • 2019-12-15
      • 1970-01-01
      相关资源
      最近更新 更多