Skip to content
← Back to rules

eslint/no-caller 正确性

This rule is turned on by default.

作用

禁止使用 arguments.callerarguments.callee

为什么不好?

使用 arguments.callerarguments.callee 会使多种代码优化变得不可能。它们在 JavaScript 中已被弃用,并且在严格模式下禁止使用。

js
function foo() {
  var callee = arguments.callee;
}

本规则旨在通过禁止使用 arguments.callerarguments.callee 来不鼓励使用已弃用和次优的代码。因此,当使用 arguments.callerarguments.callee 时,它会发出警告。

请参阅 MDN 文档 以获取更多信息。

示例

本规则 错误 代码示例:

js
function foo(n) {
  if (n <= 0) {
    return;
  }

  arguments.callee(n - 1);
}

[1, 2, 3, 4, 5].map(function (n) {
  return !(n > 1) ? 1 : arguments.callee(n - 1) * n;
});

本规则 正确 代码示例:

js
function foo(n) {
  if (n <= 0) {
    return;
  }

  foo(n - 1);
}

[1, 2, 3, 4, 5].map(function factorial(n) {
  return !(n > 1) ? 1 : factorial(n - 1) * n;
});

如何使用

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

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

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

版本

此规则在 v0.0.3 中添加。

参考资料