[英]ESLint rule to prevent overriding function by creating a const of the same name (especially during assignment)
Is there an ESLint rule that can catch the following? 是否有ESLint规则可以捕获以下内容?
const foo = () => 1
const bar = () => {
const foo = foo() // 1. identifier 'foo' is set to undefined in this context 2. invoking foo() results in an error since `foo` is undefined
return foo
}
> bar()
> Uncaught ReferenceError: foo is not defined
at bar (<anonymous>:4:15)
at <anonymous>:1:1
In other words, I would like to get a linter error when a const is assigned its own value. 换句话说,当给const分配自己的值时,我想得到一个linter错误。
The closest rule that I know of for your usecase is ESLint's no-use-before-define
. 对于您的用例,我所知道的最接近的规则是ESLint的
no-use-before-define
。 It will see this as using foo
before const foo
and it will error. 它会认为这是利用
foo
之前const foo
,它会报错。
The only downside is that this rule doesn't do full code-flow analysis, so it will also error for cases like 唯一的缺点是此规则不进行完整的代码流分析,因此在类似以下情况下也会出错
var getFoo = () => foo;
const foo = 4;
getFoo();
because it still sees foo
as used before const foo
even though at runtime it is only used after. 因为即使在运行时它也仅在之后使用,但它仍将
foo
视为在const foo
之前使用。 This may not be an issue if your codebase doesn't do this or can be fixed, but it may be annoying if you're adding the rule to an existing large codebase. 如果您的代码库不执行此操作或可以将其修复,则这可能不是问题,但如果将规则添加到现有的大型代码库中,则可能会很烦人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.