是否有可以捕获以下内容的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
换句话说,当const被赋予自己的值时,我想得到一个linter错误。
答案 0 :(得分:2)
我知道的最接近的用例是ESLint的MapView
。它会在no-use-before-define
之前将其视为使用foo
,并且会出错。
唯一的缺点是此规则不会进行完整的代码流分析,因此对于
等情况也会出错const foo
因为它仍然在var getFoo = () => foo;
const foo = 4;
getFoo();
之前看到foo
,即使在运行时它只在之后使用。如果您的代码库没有这样做或者可以修复,这可能不是问题,但如果您将规则添加到现有的大型代码库,则可能会很烦人。