简体   繁体   English

ESLint 的“no-undef”规则将我对 Underscore 的使用称为未定义变量

[英]ESLint's "no-undef" rule is calling my use of Underscore an undefined variable

I am using Grunt as my Build Tool and ESLint as my linting tool for an app I am working on.我使用 Grunt 作为我的构建工具,使用 ESLint 作为我正在开发的应用程序的 linting 工具。 I am also using the Underscore Node package, and have made use of it in my app.我也在使用 Underscore Node 包,并在我的应用程序中使用了它。 Unfortunately, when I run ESLint on my code, it thinks that _ is an undefined variable in the following line:不幸的是,当我在我的代码上运行 ESLint 时,它认为 _ 是以下行中的未定义变量:

return _.pluck(objects, nameColumn);

This is the error it is giving me:这是它给我的错误:

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.我不想禁用 ESLint 的 no-undef 规则,我已经尝试安装 Underscore 插件,但我仍然收到此错误。 If anyone else has any ideas for what to try with this, I would be very appreciative!如果其他人对此有任何想法,我将不胜感激!

If there is any further information I can give that would help anyone with helping me get this figured out, just let me know!如果有任何我可以提供的进一步信息可以帮助任何人帮助我解决这个问题,请告诉我!

The official documentation should give you an idea on how to fix this.官方文档应该让您了解如何解决此问题。

Any reference to an undeclared variable causes a warning, unless the variable is explicitly mentioned in a /*global...*/ comment, or specified in the globals key in the configuration file .任何对未声明变量的引用都会导致警告,除非该变量在/*global...*/注释中明确提及,或在配置文件的globals键中指定。

The easiest fix would be to add最简单的解决方法是添加

/* global _ */

at the top of your file.在你的文件的顶部。

Or better, explicitly specify that the variable is read-only, to disallow overwriting the variable:或者更好的是,显式指定变量是只读的,以禁止覆盖变量:

/* global _:readonly */

But since you'll have to do that for each new js file, it can get annoying.但是由于您必须为每个新的 js 文件执行此操作,所以它会变得很烦人。 If you are using underscore often, I'd suggest to add globals to your .eslintrc file , for example:如果您经常使用下划线,我建议将全局变量添加到您的.eslintrc文件中,例如:

{
    "globals": {
        "_": "readonly"
    }
}

And save this as .eslintrc in your project root, or optionally in your user home directory.并将其保存为项目根目录中的.eslintrc ,或者可选地保存在用户主目录中。 Although some say the latter not recommended, it can sometimes be convenient, but you have to remember that you have it there:)虽然有人说不推荐后者,但有时会很方便,但你必须记住你有它:)


Explanation of the above rule : "_": "readonly" (used to be "_": false , now deprecated) 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.上面规则的解释"_": "readonly" (以前是"_": false ,现在不推荐使用)意味着一个名为_的变量告诉 eslint 这个变量是全局定义的,它不会发出任何no-undef错误对于这个变量。 As @sebastian pointed out, "readonly" (or false - deprecated) means that the variable can't be overwritten, so the code _ = 'something else' would yield an error no-global-assign .正如@sebastian 指出的那样, "readonly" (或false - 已弃用)意味着变量不能被覆盖,因此代码_ = 'something else'会产生错误no-global-assign If you were to instead use "_": "writable" (or "_": true - deprecated), this means that the value can be re-assigned and the previously mentioned error will not occur.如果您改为使用"_": "writable" (或"_": true - 已弃用),这意味着可以重新分配该值并且不会发生前面提到的错误。

But keep in mind that this will only happen if you assign directly to the global variable as I have shown in the example.但请记住,这只有在您直接分配给全局变量时才会发生,如我在示例中所示。 You can still shadow it and eslint won't say anything.你仍然可以隐藏它并且 eslint 不会说什么。 For example, these snippets wouldn't yield the no-global-assign :例如,这些片段不会产生no-global-assign

const _ = 'haha I broke your _' 

or as function argument name, eg或作为函数参数名称,例如

function (_) {
  console.log(_, 'might not be the _ you were looking for') 
}

If you are using jest for testing - in your environment - in eslintrc.json如果您使用 jest 进行测试 - 在您的环境中 - 在 eslintrc.json

"env":{
    "jest":true
}

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

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