javascript 如何禁用警告“定义”未使用 JSHint 和 RequireJS 定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16260779/
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-27 03:57:16  来源:igfitidea点击:

How to disable the warning 'define' is not defined using JSHint and RequireJS

javascriptrequirejsamdjshint

提问by Fizer Khan

I uses RequireJS AMD in my project. When i run jshint on my project, it throws error like

我在我的项目中使用 RequireJS AMD。当我在我的项目上运行 jshint 时,它会抛出类似的错误

In AMD Scripts

在 AMD 脚本中

 'define' is not defined.

In Mocha test cases

在摩卡测试用例中

 'describe' is not defined.
 'it' is not defined.

How to remove this warning in jshint?

如何在 jshint 中删除此警告?

回答by bendytree

Just to expand a bit, here's a .jshintrcsetup for Mocha:

稍微扩展一下,这是.jshintrcMocha的设置:

{
  ....
  "globals"   : {
    /* MOCHA */
    "describe"   : false,
    "it"         : false,
    "before"     : false,
    "beforeEach" : false,
    "after"      : false,
    "afterEach"  : false
  }
}

From the JSHint Docs- the false (the default) means the variable is read-only.

来自JSHint Docs- false(默认值)表示该变量是只读的。

If you are defining globals only for a specific file, you can do this:

如果您只为特定文件定义全局变量,您可以这样做:

/*global describe, it, before, beforeEach, after, afterEach */

回答by Grant Fong

jshint: {
  options: {
    mocha: true,
  }
}

is what you want

是你想要的

回答by Roland Puntaier

To avoid the not defined warning in jshint for the javascript add comments like:

为了避免在 jshint 中为 javascript 添加未定义的警告,添加如下注释:

/*global describe:true*/

Options

选项

回答by Shital Shah

Add this in your .jshintrc

将此添加到您的 .jshintrc

"predef" : ["define"]   // Custom globals for requirejs

回答by Gilad Peleg

late to the party, but use this option in your jshintrc:

聚会迟到,但在您的jshintrc

"dojo": true

and thou shall rest peacefully without red warnings...

你将安息,没有红色警告......

回答by salihcenap

If you are working on node js. Add these two lines in the beginning of your file

如果您正在处理节点 js。在文件的开头添加这两行

/*jslint node: true */
"use strict";

回答by Paul Grime

Read the docsand search for /*global

阅读文档并搜索/*global

回答by nyarasha

If you're trying to run JSHint in WebStorm with Mocha, as I am, go into:

如果您像我一样尝试在 WebStorm 中使用 Mocha 运行 JSHint,请进入:

WebStorm > Preferences > Languages & Frameworks > JavaScript > Code Quality Tools > JSHint

WebStorm > 首选项 > 语言和框架 > JavaScript > 代码质量工具 > JSHint

Scroll down to "Environments" and make sure you have selected the checkbox to enable "Mocha" which will set up the definitions for JSHint for Mocha for you.

向下滚动到“环境”并确保您已选中复选框以启用“Mocha”,这将为您设置 JSHint for Mocha 的定义。