ESLint 的“no-undef"规则将我对 Underscore 的使用称为未定义变量 [英] ESLint's "no-undef" rule is calling my use of Underscore an undefined variable

查看:64
本文介绍了ESLint 的“no-undef"规则将我对 Underscore 的使用称为未定义变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Grunt 作为我的构建工具,使用 ESLint 作为我正在开发的应用程序的 linting 工具.我也在使用 Underscore Node 包,并在我的应用程序中使用了它.不幸的是,当我在我的代码上运行 ESLint 时,它认为 _ 是以下行中的未定义变量:

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:

return _.pluck(objects, nameColumn);

这是它给我的错误:

78:21 error "_" is not defined no-undef

我不想禁用 ESLint 的 no-undef 规则,并且我已经尝试安装 Underscore 插件,但我仍然收到此错误.如果其他人对如何尝试这个有任何想法,我将不胜感激!

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!

如果我可以提供任何进一步的信息来帮助任何人帮助我解决这个问题,请告诉我!

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.

对未声明变量的任何引用都会导致警告,除非该变量在 /*global ...*/ 注释中明确提及,或在 globals 配置文件中的键.

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.

最简单的解决方法是添加

The easiest fix would be to add

/* global _ */

在文件的顶部.

或者更好的是,明确指定该变量是只读的,以禁止覆盖该变量:

Or better, explicitly specify that the variable is read-only, to disallow overwriting the variable:

/* global _:readonly */

但是由于您必须为每个新的 js 文件执行此操作,因此可能会很烦人.如果您经常使用下划线,我建议将全局变量添加到您的 .eslintrc 文件,例如:

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 to add globals to your .eslintrc file, for example:

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

并将其保存为 .eslintrc 在您的项目根目录中,或者在您的用户主目录中保存.虽然有人说后者不推荐,它有时会很方便,但你必须记住你有它:)

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 :)

以上规则的解释:_":readonly"(曾经是_":false,now deprecated) 意味着一个名为 _ 的变量告诉 eslint 这个变量是全局定义的,它不会为这个变量发出任何 no-undef 错误.正如@sebastian 指出的那样,"readonly"(或 false - 已弃用)意味着该变量不能被覆盖,因此代码 _ = 'somethingelse' 会产生一个错误 no-global-assign.如果您改为使用 "_":"writable"(或 "_": true - 已弃用),这意味着该值可以重新分配,不会出现前面提到的错误.

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. 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. 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.

但请记住,只有像我在示例中显示的那样直接分配给全局变量时,才会发生这种情况.你仍然可以隐藏它并且 eslint 不会说什么.例如,这些片段不会产生 no-global-assign:

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. For example, these snippets wouldn't yield the 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') 
}

这篇关于ESLint 的“no-undef"规则将我对 Underscore 的使用称为未定义变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆