javascript JSLint 忽略未定义的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7866898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:34:17  来源:igfitidea点击:

JSLint to ignore undefined variables

javascriptjslint

提问by Randomblue

I have chopped my js program into many pieces for development. Now when I run one piece through JSLint, I get a lot of errors of the type:

我已经将我的 js 程序切成了许多用于开发的部分。现在,当我通过 JSLint 运行一个片段时,我得到了很多类型的错误:

Problem at line 48 character 42: 'XXXXXXX' was used before it was defined.

第 48 行字符 42 的问题:在定义之前使用了“XXXXXXX”。

I've looked for an option "Tolerate undefined variables" but haven't found any such option. What can I do so that JSLint ignores undefined variables?

我一直在寻找一个选项“容忍未定义的变量”,但没有找到任何这样的选项。我该怎么做才能让 JSLint 忽略未定义的变量?

采纳答案by Red Orca

JSHintis a nice alternative to JSLint, and it has an option to turn on/off warnings for allundefined variables at once.

JSHint是 JSLint 的一个很好的替代品,它有一个选项可以同时打开/关闭所有未定义变量的警告。

回答by stivlo

From JSLint documentation:

JSLint 文档

JSLint also recognizes a /*global */ directive that can indicate to JSLint that variables used in this file were defined in other files. The comment can contain a comma separated list of names. Each name can optionally be followed by a colon and either true or false, true indicating that the variable may be assigned to by this file, and false indicating that assignment is not allowed (which is the default).

JSLint 还识别 /*global */ 指令,该指令可以向 JSLint 指示此文件中使用的变量已在其他文件中定义。注释可以包含逗号分隔的名称列表。每个名称都可以选择后跟一个冒号和 true 或 false,true 表示该变量可以由该文件分配,false 表示不允许分配(这是默认值)。

Example:

例子:

/*global var1, var2, var3 */

However I'd advise you to not do that, and instead write a simple script that re-assembles all the files and check the resulting file with JSLint.

但是我建议您不要这样做,而是编写一个简单的脚本来重新组装所有文件并使用 JSLint 检查生成的文件。

回答by reergymerej

The answer "use a different program" is not acceptable.

“使用不同的程序”的答案是不可接受的。

Go to Preferences > Package Settings > JSLint > Advanced Build Settings.

转至首选项 > 包设置 > JSLint > 高级构建设置。

Within the array for "cmd", add "--undef". It should look something like

在“cmd”的数组中,添加“--undef”。它应该看起来像

{
    "cmd": [
      "node", 
      "${packages}/JSLint/linter.js",
      // tolerate missing 'use strict' pragma
      "--sloppy",
      // suggest an indent level of two spaces
      "--indent", "2",
      // assume node.js to predefine node globals
      "--node",
      // tolerate unfiltered for in
      //"--forin",
      // tolerate dangling _ in identifiers
      "--nomen",
      // tolerate many var statements per function
      "--vars",
      // tolerate ++ and --
      "--plusplus",
      // tolerate Douglas Crockford
      "--stupid",
      "--todo",
      // -----------------------------------------------
      // tolerate undefined variables
      "--undef",
      // -----------------------------------------------
      "$file"
    ],
    "file_regex": "^\/.*\/([^\/]*)$",
    "line_regex": ".*\/\/ Line ([0-9]*), Pos ([0-9]*)$",
    "selector": "source.js, source.css, source.json, source.sass, source.less, source.html"
}

回答by haltersweb

use the predefined option in your settings. You will see I added "NAMESP" at the end of the array.

使用设置中的预定义选项。您将看到我在数组末尾添加了“NAMESP”。

Note also that I fixed the typo for jQuery (from JQuery to jQuery).

另请注意,我修复了 jQuery 的拼写错误(从 JQuery 到 jQuery)。

    // examples using predef flag.
    "--predef", "['angular', 'document', '\$', '_', 'jQuery', 'NAMESP']"