javascript 如何告诉 JSHint 忽略一个文件中的所有未定义变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17359232/
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
How to tell JSHint to ignore all undefined variables in one file?
提问by Dan Ross
In Karma tests, there are a lot of global variables and functions, which JSHint complains about (it is integrated into my editor).
在 Karma 测试中,有很多 JSHint 抱怨的全局变量和函数(它已集成到我的编辑器中)。
How can I tell JSHint to ignore all undefined variables in this one specific file? I would expect /* jshint undef: false */
to turn off these warning, but it doesn't.
如何告诉 JSHint 忽略这个特定文件中的所有未定义变量?我希望/* jshint undef: false */
关闭这些警告,但事实并非如此。
回答by James Allardice
The correct way to tell JSHint about globals is to use the globals
directive. For example:
告诉 JSHint 关于全局变量的正确方法是使用globals
指令。例如:
/*globals globalFunction, anotherGlobal, oneMore */
This will prevent "{a} is not defined" warnings when JSHint encounters any of the listed identifiers.
当 JSHint 遇到任何列出的标识符时,这将防止“{a} 未定义”警告。
Alternatively, if you really want to ignore all"not defined" warnings in that file, and you're using JSHint 1.0.0 or above, you can simply turn off that specific warning:
或者,如果您真的想忽略该文件中的所有“未定义”警告,并且您使用的是 JSHint 1.0.0 或更高版本,则只需关闭该特定警告:
/*jshint -W117 */
回答by Balasubramani M
Just add this rule in your .jshintrc file.
只需在您的 .jshintrc 文件中添加此规则。
"-W117": true
This will ignore all the warnings which say, '* is not defined.'
这将忽略所有警告,这些警告说“* 未定义”。
回答by zero_cool
Ran into this problem using jshint this afternoon. This following fix worked for me. Instead of using "globals", try using "predef". For example:
今天下午使用 jshint 遇到了这个问题。以下修复对我有用。不要使用“全局变量”,而是尝试使用“预定义”。例如:
{
/*
* RELAXING OPTIONS
* =================
*/
// Suppress warnings about == null comparisons.
"eqnull": true,
"predef" : ["describe", "expect", "it", "inject", "beforeEach", "angular"]
}
回答by ken
I've found myself using jshint ignore:line
as a way of addressing this need:
我发现自己使用jshint ignore:line
了一种方式来满足这种需求:
var unusedVar; // jshint ignore:line
变量未使用变量;// jshint 忽略:行
This allows jshint to continue its useful checking for this condition but where there are explicit reasons to ignore a specific declaration than adding this both addresses the issue and does it in a way that is immediately apparent to anyone looking at the code.
这允许 jshint 继续对这种情况进行有用的检查,但是在有明确的理由忽略特定声明的情况下,而不是添加此声明既可以解决问题,又可以以任何查看代码的人都可以立即看到的方式进行处理。
A good example (at least for me), is when using ES6's destructuring to illicit a set of shortcuts that you may or may not use all the time. In Ember, I often use many of the methods that hang off of it such as typeOf
and computed
. Rather than always referring to Ember.computed
it's far nicer to just refer to computed
and have something like the following at the top of all my Ember objects:
一个很好的例子(至少对我而言),是当使用 ES6 的解构来非法使用一组你可能会或可能不会一直使用的快捷方式时。在 Ember 中,我经常使用许多与它无关的方法,例如typeOf
和computed
。与其总是引用,不如在我的所有 Ember 对象的顶部Ember.computed
引用computed
并具有如下内容:
const { computed, $, A, run, on, typeOf, debug, get, set } = Ember; // jshint ignore:line