如何在 JavaScript/TypeScript 中检测具有相同索引的嵌套循环

ist*_*ist 3 javascript syntax-checking typescript eslint tslint

我正在尝试检测具有相同索引的嵌套循环,如下所示:

for(let i = 0; i < 10; i++) {
    for(let i = 0; i < 10; i++) {
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经搜索了 Eslint 规则,但到目前为止还没有找到任何解决方案。有没有办法检测这种糟糕的代码味道?非常感谢。

T.J*_*der 6

ESLint 有一个no-shadow规则可以标记它,以及其他你用内部作用域变量遮蔽外部作用域变量的地方。例如:

{
    "no-shadow": ["error", { "builtinGlobals": false, "hoist": "functions", "allow": [] }]
}
Run Code Online (Sandbox Code Playgroud)

ESLint 站点上的演示

for(let i = 0; i < 10; i++) {
    for(let i = 0; i < 10; i++) {
//          ^????? 'i' is already declared in the upper scope on line 1 column 9.
        console.log(i);
    }
}
Run Code Online (Sandbox Code Playgroud)