no-with
禁止 with
语句
在 配置文件 中使用来自 @eslint/js
的 recommended
配置可以启用此规则
with
语句可能存在问题,因为它将对象的成员添加到当前作用域,从而无法判断块内的变量实际指的是什么。
¥The with
statement is potentially problematic because it adds members of an object to the current scope, making it impossible to tell what a variable inside the block actually refers to.
规则详情
¥Rule Details
此规则不允许 with
语句。
¥This rule disallows with
statements.
如果 ESLint 在严格模式下解析代码,解析器(而不是这个规则)会报告错误。
¥If ESLint parses code in strict mode, the parser (instead of this rule) reports the error.
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint no-with: "error"*/
with (point) {
r = Math.sqrt(x * x + y * y); // is r a member of point?
}
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint no-with: "error"*/
const r = ({x, y}) => Math.sqrt(x * x + y * y);
何时不使用
¥When Not To Use It
如果你有意使用 with
语句,则可以禁用此规则。
¥If you intentionally use with
statements then you can disable this rule.
版本
此规则是在 ESLint v0.0.2 中引入。