简体   繁体   English

如何告诉JSHint忽略一个文件中的所有未定义变量?

[英]How to tell JSHint to ignore all undefined variables in one file?

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? 如何告诉JSHint忽略这个特定文件中的所有未定义变量? I would expect /* jshint undef: false */ to turn off these warning, but it doesn't. 我希望/* jshint undef: false */关闭这些警告,但事实并非如此。

The correct way to tell JSHint about globals is to use the globals directive. 告诉JSHint关于全局变量的正确方法是使用globals指令。 For example: 例如:

/*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 */

Ran into this problem using jshint this afternoon. 今天下午使用jshint来解决这个问题。 This following fix worked for me. 以下修复对我有用。 Instead of using "globals", try using "predef". 而不是使用“全局”,尝试使用“predef”。 For example: 例如:

{
  /*
   * RELAXING OPTIONS
   * =================
   */

  // Suppress warnings about == null comparisons.
  "eqnull": true,

  "predef" : ["describe", "expect", "it", "inject", "beforeEach", "angular"]
}

Just add this rule in your .jshintrc file. 只需在.jshintrc文件中添加此规则即可。

"-W117": true

This will ignore all the warnings which say, '* is not defined.' 这将忽略所有警告,即“*未定义”。

I've found myself using jshint ignore:line as a way of addressing this need: 我发现自己使用jshint ignore:line来解决这个问题:

var unusedVar; var unusedVar; // jshint ignore:line // jshint ignore:line

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. 一个很好的例子(至少对我而言)是,当使用ES6的解构来违反一组你可能会或可能不会一直使用的快捷方式时。 In Ember, I often use many of the methods that hang off of it such as typeOf and computed . 在Ember中,我经常使用许多挂起它的方法,例如typeOfcomputed 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: 而不是总是指Ember.computed它更好的只是引用computed并在我所有的Ember对象的顶部有类似的东西:

 const { computed, $, A, run, on, typeOf, debug, get, set } = Ember;    // jshint ignore:line

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM