Javascript ESLint 的“no-undef”规则将我对 Underscore 的使用称为未定义变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34820817/
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
ESLint's "no-undef" rule is calling my use of Underscore an undefined variable
提问by turner
I am using Grunt as my Build Tool and ESLint as my linting tool for an app I am working on. I am also using the Underscore Node package, and have made use of it in my app. Unfortunately, when I run ESLint on my code, it thinks that _ is an undefined variable in the following line:
我使用 Grunt 作为我的构建工具,使用 ESLint 作为我正在开发的应用程序的 linting 工具。我也在使用 Underscore Node 包,并在我的应用程序中使用了它。不幸的是,当我在我的代码上运行 ESLint 时,它认为 _ 是以下行中的未定义变量:
return _.pluck(objects, nameColumn);
return _.pluck(objects, nameColumn);
This is the error it is giving me:
这是它给我的错误:
78:21 error "_" is not defined no-undef
78:21 error "_" is not defined no-undef
I would prefer not to disable the no-undef rule for ESLint, and I have tried installing the Underscore plugin, but I am still receiving this error. If anyone else has any ideas for what to try with this, I would be very appreciative!
我不想禁用 ESLint 的 no-undef 规则,并且我尝试安装 Underscore 插件,但我仍然收到此错误。如果其他人对尝试使用此方法有任何想法,我将不胜感激!
If there is any further information I can give that would help anyone with helping me get this figured out, just let me know!
如果我可以提供任何进一步的信息来帮助任何人帮助我解决这个问题,请告诉我!
回答by Marko Gre?ak
The official documentationshould give you an idea on how to fix this.
在官方文件应该给你如何解决这个问题的想法。
The easiest fix would be to add
最简单的解决方法是添加
/* global _ */
at the top of your file.
在文件的顶部。
But since you'll have to do that for each new js file, it can get annoying. If you are using underscore often, I'd suggest you to add globals to your .eslintrc
file, for example:
但是由于您必须为每个新的 js 文件执行此操作,因此可能会很烦人。如果您经常使用下划线,我建议您将全局变量添加到您的.eslintrc
文件中,例如:
{
"globals": {
"_": false
}
}
And save this as .eslintrc
in your project root, or optionally in your user home directory. Although some say the latter not recommended, it can sometimes be convenient, but you have to remember that you have it there :)
并将其保存.eslintrc
在您的项目根目录中,或者可以选择保存在您的用户主目录中。虽然有人说后者不推荐,但它有时会很方便,但你必须记住你有它:)
Explanation of the above rule: "_": false
means that a variable named _
tells eslint that this variable is defined globally and it will not emit any no-undef
errors for this variable. As @sebastian pointed out, false
means that the variable can't be overwritten, so the code _ = 'something else'
would yield an error no-global-assign
. If you were to instead use "_": true
(this was my previous answer), this means that the value can be re-assigned and the previously mentioned error will not occur.
上面规则的解释:"_": false
意味着一个名为的变量_
告诉 eslint 这个变量是全局定义的,它不会no-undef
为这个变量发出任何错误。正如@sebastian 指出的那样,false
意味着变量不能被覆盖,因此代码_ = 'something else'
会产生错误no-global-assign
。如果您改为使用"_": true
(这是我之前的答案),这意味着可以重新分配该值,并且不会发生前面提到的错误。
But keep in mind that this will only happen if you assign directly to the global variable like I have shown in the example. You can still shadow it and eslint won't say anything. For example, these snippets wouldn't yield the no-global-assign
:
但是请记住,只有像我在示例中显示的那样直接分配给全局变量时,才会发生这种情况。你仍然可以隐藏它并且 eslint 不会说什么。例如,这些片段不会产生no-global-assign
:
const _ = 'haha I broke your _'
or as function argument name, e.g.
或作为函数参数名称,例如
function (_) {
console.log(_, 'might not be the _ you were looking for')
}
回答by Leon Africa
If you are using jest for testing - in your environment - in eslintrc.json
如果您使用 jest 进行测试 - 在您的环境中 - 在 eslintrc.json
"env":{
"jest":true
}