eslint/no-caller 正确性
作用
禁止使用 arguments.caller 或 arguments.callee。
为什么不好?
使用 arguments.caller 和 arguments.callee 会使多种代码优化变得不可能。它们在 JavaScript 中已被弃用,并且在严格模式下禁止使用。
js
function foo() {
var callee = arguments.callee;
}本规则旨在通过禁止使用 arguments.caller 和 arguments.callee 来不鼓励使用已弃用和次优的代码。因此,当使用 arguments.caller 和 arguments.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 中添加。
