Skip to content
← Back to rules

eslint/no-with Correctness

This rule is turned on by default.

作用

禁止使用 with 语句。

为什么这不好?

with 语句可能有问题,因为它会将对象的成员添加到当前作用域中,从而使得无法判断块内的变量实际指代什么。

通常认为这是一种不好的做法,并且在严格模式下是被禁止的。

如果启用了 alwaysStrict,那么在 TypeScript 代码中不需要此规则。

示例

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

javascript
with (point) {
  r = Math.sqrt(x * x + y * y); // r 是 point 的成员吗?
}

如何使用

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

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

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

版本

此规则是在 v0.2.14 中添加的。

参考资料