[英]ESLint rule to prevent overriding function by creating a const of the same name (especially during assignment)
[英]ESlint rule that detects that a variable and a function has the same name in Javascript?
我碰巧在 Node.js 中命名了一個與 function 相同的變量。 這個沒有 go 很好,節點 10 不喜歡它。 由於這是一個鈎子 function(不是從應用程序的 UI 調用),所以我花了一些時間才發現出了什么問題。
是否有一些 ESLint 規則可以發現這些類型的錯誤? 這是在 Firebase 下,並且 ESLint 在部署到生產服務器期間運行。
變量名的實際沖突使用在同一個塊中,如下所示: const a = a(x)
。
我認為 JavaScript不存在這樣的工具,因為 JavaScript 並沒有真正消除分配給變量的 object 的類型。
function a() {
}
...基本上相當於:
var a = function () {
};
此外,此示例中的a
值可以稍后重新分配。
linter 可能對您有所幫助,並且在某些 IDE 中可能會有一些幫助,但它們不會真正了解程序員的意圖。
你可以在ESLint中設置一個no-redeclare規則,它會告訴你代碼中有問題的行。
/*eslint no-redeclare: "error"*/
function a() {}
const a = a();
=> 4:5 error 'a' is already defined no-redeclare
ESLint也會在標准配置中引發錯誤。
function a() {}
var a = a();
=> 4:5 error Parsing error: Identifier 'a' has already been declared
當然,使用const
時,如果您嘗試運行腳本,也會收到該行的語法錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.