Skip to content
← Back to rules

react/rule-suppression 限制

功能

报告组件或 Hook 内对 React 规则的 ESLint/Oxlint 抑制(例如 eslint-disable-next-line react-hooks/exhaustive-deps)。React Compiler 会跳过包含此类抑制的函数,因为被抑制的违规可能会导致编译不安全。

由 React Compiler 提供支持,该编译器每个文件运行一次,并与其他 React Compiler 规则共享。移植自 react-hooks/rule-suppression

为什么这是个问题?

抑制 React 规则会隐藏编译器必须假定为真实存在的违规;在移除抑制并修复底层错误之前,整个函数都会失去优化。

示例

此规则的错误代码示例:

jsx
function Component({ value }) {
  // eslint-disable-next-line react-hooks/exhaustive-deps
  const doubled = value * 2;
  return <div>{doubled}</div>;
}

此规则的正确代码示例:

jsx
function Component({ value }) {
  const doubled = value * 2;
  return <div>{doubled}</div>;
}

如何使用

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

json
{
  "plugins": ["react"],
  "rules": {
    "react/rule-suppression": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["react"],
  rules: {
    "react/rule-suppression": "error",
  },
});
bash
oxlint --deny react/rule-suppression --react-plugin

版本

此规则在 vnext 中添加。

参考