Skip to content
← Back to rules

unicorn/no-accessor-recursion Suspicious

作用

禁止在 getter 和 setter 中递归访问 this

为什么这不好?

该规则会阻止在对象和类的 getter 与 setter 方法中对 this 的递归访问,从而避免无限递归和堆栈溢出错误。

示例

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

js
const foo = {
  get bar() {
    return this.bar;
  },
};

const baz = {
  set bar(value) {
    this.bar = value;
  },
};

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

js
const foo = {
  get bar() {
    return this.qux;
  },
};

const baz = {
  set bar(value) {
    this._bar = value;
  },
};

如何使用

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

json
{
  "rules": {
    "unicorn/no-accessor-recursion": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/no-accessor-recursion": "error",
  },
});
bash
oxlint --deny unicorn/no-accessor-recursion

版本

此规则已在 v0.16.5 中添加。

参考资料