Skip to content

noWith

诊断类别:lint/complexity/noWith

¥Diagnostic Category: lint/complexity/noWith

自从:v1.0.0

¥Since: v1.0.0

来源:

¥Sources:

禁止在非严格上下文中使用 with 语句。

¥Disallow with statements in non-strict contexts.

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.

¥Examples

¥Invalid

function f() {
with (point) {
r = Math.sqrt(x * x + y * y); // is r a member of point?
}
}
code-block.cjs:2:3 lint/complexity/noWith ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected use of with statement.

1 │ function f() {
> 2 │ with (point) {
^^^^^^^^^^^^^^
> 3 │ r = Math.sqrt(x * x + y * y); // is r a member of point?
> 4 │ }
^
5 │ }
6 │

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.

¥Related links