Skip to content
← Back to rules

eslint/no-inner-declarations Pedantic

作用 ​

禁止在嵌套块中声明变量或函数。

为什么这不好? ​

变量声明可以出现在语句可以出现的任何地方,即使是深度嵌套在其他块内部。 由于变量提升,这通常并不理想,而且将声明移动到程序或函数体的根部可以提高清晰度。 请注意,块级绑定(let、const)不会被提升,因此它们不受此规则影响。

示例 ​

以下是此规则的错误代码示例:

javascript
if (test) {
  function doSomethingElse() {}
}

以下是此规则的正确代码示例:

javascript
function doSomethingElse() {}
if (test) {
  // 你的代码在这里
}

配置 ​

第 1 个选项 ​

type: "functions" | "both"

决定要检查哪种类型的声明。

"functions" ​

禁止在嵌套块中声明函数。

"both" ​

禁止在嵌套块中声明函数和 var。

第 2 个选项 ​

此选项是一个具有以下属性的对象:

blockScopedFunctions ​

type: "allow" | "disallow"

控制在严格模式下是否允许在嵌套块中声明函数(ES6+ 行为)。

"allow" ​

在严格模式下允许在嵌套块中声明函数(ES6+ 行为)。

"disallow" ​

无论是否为严格模式,都禁止在嵌套块中声明函数。

namespaces ​

type: "allow" | "disallow"

控制是否允许直接在 TypeScript 命名空间或模块体内声明。

"allow" ​

允许直接在 TypeScript 命名空间或模块体内声明。

"disallow" ​

禁止直接在 TypeScript 命名空间或模块体内声明。

使用方法 ​

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "no-inner-declarations": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "no-inner-declarations": "error",
  },
});
bash
oxlint --deny no-inner-declarations

版本 ​

此规则是在 v0.0.5 中添加的。

参考资料 ​