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

元气小坏坏 提交于 2019-12-03 01:02:28
James Allardice

The correct way to tell JSHint about globals is to use the globals directive. For example:

/*globals globalFunction, anotherGlobal, oneMore */

This will prevent "{a} is not defined" warnings when JSHint encounters any of the listed identifiers.

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

Just add this rule in your .jshintrc file.

"-W117": true

This will ignore all the warnings which say, '* is not defined.'

Ran into this problem using jshint this afternoon. This following fix worked for me. Instead of using "globals", try using "predef". For example:

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

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

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

I've found myself using jshint ignore:line as a way of addressing this need:

var unusedVar; // 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.

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:

 const { computed, $, A, run, on, typeOf, debug, get, set } = Ember;    // jshint ignore:line
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!