ESLint的“no-undef”规则是调用我使用Underscore一个未定义的变量 [英] ESLint's "no-undef" rule is calling my use of Underscore an undefined variable

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

问题描述

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

return _.pluck(objects ,



78:21 error_is not defined no-undef



我不想禁用no-对于ESLint的undef规则,我已经尝试安装Underscore插件,但我仍然收到此错误。如果任何人有任何想法尝试这个,我会非常感激!



如果有任何进一步的信息我可以给,这将帮助任何人帮助我得到这个想法,只是让我知道!

解决方案

The 官方文档应该给你一个关于如何解决这个问题的想法。



最简单的修复将添加

  / *全局_ * / 

放在文件顶部。

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

  {
globals:{
_:false
}
}

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






<上述规则的解释:_:false 表示名为 _ 告诉eslint这个变量是全局定义的,它不会为这个变量发出任何 no-undef 错误。正如@sebastian指出的那样, false 表示变量不能被覆盖,所以代码 _ ='something else'会产生错误 no-global-assign 。如果您改为使用_:true (这是我以前的回答),这意味着该值可以重新分配,并且不会出现前面提到的错误。不过请记住,这只会发生,如果直接分配给全局变量,就像我在示例中所示。你仍然可以遮蔽它,埃斯林不会说任何话。例如,这些片段不会产生 no-global-assign

  const _ ='哈哈我打破了你的_'

或函数参数名称,例如

 函数(_){
console.log(_,'可能不是您正在查找的' )
}


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

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

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:

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


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.

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:

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天全站免登陆