简体   繁体   English

检测到一个变量和一个 function 在 Javascript 中具有相同名称的 ESlint 规则?

[英]ESlint rule that detects that a variable and a function has the same name in Javascript?

I happened to name a variable the same as a function in Node.js.我碰巧在 Node.js 中命名了一个与 function 相同的变量。 This did not go very well, Node 10 did not like it.这个没有 go 很好,节点 10 不喜欢它。 And since this was a hook function (not called from the UI of the app) it took some time before I discovered what went wrong.由于这是一个钩子 function(不是从应用程序的 UI 调用),所以我花了一些时间才发现出了什么问题。

Is there some ESLint rule that can discover these type of bugs?是否有一些 ESLint 规则可以发现这些类型的错误? This is under Firebase and and ESLint is run during the deploy to production server.这是在 Firebase 下,并且 ESLint 在部署到生产服务器期间运行。

The actual conflicting use of the variable name was in the same block, something like this: const a = a(x) .变量名的实际冲突使用在同一个块中,如下所示: const a = a(x)

I don't think a tool like this could exist for JavaScript, as JavaScript doesn't really disambiguate the type of object assigned to the variable.我认为 JavaScript存在这样的工具,因为 JavaScript 并没有真正消除分配给变量的 object 的类型。

function a() {

}

... is basically equivalent to: ...基本上相当于:

var a = function () {

};

Additionally, the value of a in this example can be reassigned later.此外,此示例中的a值可以稍后重新分配。

A linter may help you, and there may be some help in some IDEs, but they won't truly know the intention of the programmer. linter 可能对您有所帮助,并且在某些 IDE 中可能会有一些帮助,但它们不会真正了解程序员的意图。

There is a no-redeclare rule you can set in ESLint , which tells you the problematic line in your code.你可以在ESLint中设置一个no-redeclare规则,它会告诉你代码中有问题的行。

/*eslint no-redeclare: "error"*/

function a() {}
const a = a();

=> 4:5  error  'a' is already defined  no-redeclare

Also ESLint will raises an error with the standard configuration. ESLint也会在标准配置中引发错误。

function a() {}
var a = a();

=> 4:5  error  Parsing error: Identifier 'a' has already been declared

Of course, with const , you will get a syntax error with the line as well if you try to run your script.当然,使用const时,如果您尝试运行脚本,也会收到该行的语法错误。

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

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