Skip to content
← Back to rules

eslint/no-func-assign Correctness

This rule is turned on by default.

它的作用

禁止重新赋值 function 声明。

由于 TypeScript 编译器会强制执行此检查,因此可以在 TypeScript 代码中禁用此规则。

为什么这不好?

覆盖/重新赋值一个以 FunctionDeclaration 形式编写的函数,通常表明存在 错误或问题。

示例

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

javascript
function foo() {}
foo = bar;
javascript
function foo() {
  foo = bar;
}
javascript
let a = function hello() {
  hello = 123;
};

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

javascript
let foo = function () {};
foo = bar;
javascript
function baz(baz) {
  // `baz` 被遮蔽了。
  baz = bar;
}
function qux() {
  const qux = bar;  // `qux` 被遮蔽了。
}

如何使用

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

json
{
  "rules": {
    "no-func-assign": "error"
  }
}
ts
import { defineConfig } from "oxlint";

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

版本

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

参考资料