Skip to content
← Back to rules

eslint/no-implied-eval Suspicious

This rule is turned on by default.

它的作用

不允许向 setTimeout()setInterval()execScript() 传递字符串。

为什么这不好?

向这些 API 传递字符串时,会在运行时将该字符串作为 JavaScript 源代码求值。 这与 eval() 有许多相同的安全性、可读性和 性能问题。请改为传递函数。

示例

此规则的错误代码示例:

js
setTimeout("alert('Hi!')", 100);
setInterval("doWork()", 1000);
window.setTimeout("doWork()", 100);

此规则的正确代码示例:

js
setTimeout(() => alert("Hi!"), 100);
setInterval(doWork, 1000);
window.setTimeout(doWork, 100);

如何使用

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

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

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

版本

此规则已在 vnext 中添加。

参考资料