noUnsafeOptionalChaining
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/correctness/noUnsafeOptionalChaining¥Diagnostic Category:
lint/correctness/noUnsafeOptionalChaining -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 error。
¥The default severity of this rule is error.
-
来源:
¥Sources:
-
与
no-unsafe-optional-chaining相同¥Same as
no-unsafe-optional-chaining
-
¥How to configure
{ "linter": { "rules": { "correctness": { "noUnsafeOptionalChaining": "error" } } }}¥Description
禁止在未允许使用未定义值的上下文中使用可选链接。
¥Disallow the use of optional chaining in contexts where the undefined value is not allowed.
可选链 (?.) 表达式可以短路,返回值为未定义。因此,将已评估的可选链表达式视为函数、对象、数字等可能会导致 TypeError 或意外结果。此外,括号限制了链中短路的范围。
¥The optional chaining (?.) expression can short-circuit with a return value of undefined. Therefore, treating an evaluated optional chaining expression as a function, object, number, etc., can cause TypeError or unexpected results. Also, parentheses limit the scope of short-circuiting in chains.
¥Examples
¥Invalid
1 in obj?.foo;code-block.js:1:9 lint/correctness/noUnsafeOptionalChaining ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unsafe usage of optional chaining.
> 1 │ 1 in obj?.foo;
│ ^^
2 │
ℹ If it short-circuits with ‘undefined’ the evaluation will throw TypeError here:
> 1 │ 1 in obj?.foo;
│ ^^^^^^^^^^^^^
2 │
with (obj?.foo);code-block.cjs:1:10 lint/correctness/noUnsafeOptionalChaining ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unsafe usage of optional chaining.
> 1 │ with (obj?.foo);
│ ^^
2 │
ℹ If it short-circuits with ‘undefined’ the evaluation will throw TypeError here:
> 1 │ with (obj?.foo);
│ ^^^^^^^^^^^^^^^^
2 │
for (bar of obj?.foo);code-block.js:1:16 lint/correctness/noUnsafeOptionalChaining ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unsafe usage of optional chaining.
> 1 │ for (bar of obj?.foo);
│ ^^
2 │
ℹ If it short-circuits with ‘undefined’ the evaluation will throw TypeError here:
> 1 │ for (bar of obj?.foo);
│ ^^^^^^^^^^^^^^^^^^^^^^
2 │
bar instanceof obj?.foo;code-block.js:1:19 lint/correctness/noUnsafeOptionalChaining ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unsafe usage of optional chaining.
> 1 │ bar instanceof obj?.foo;
│ ^^
2 │
ℹ If it short-circuits with ‘undefined’ the evaluation will throw TypeError here:
> 1 │ bar instanceof obj?.foo;
│ ^^^^^^^^^^^^^^^^^^^^^^^
2 │
const { bar } = obj?.foo;code-block.js:1:20 lint/correctness/noUnsafeOptionalChaining ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unsafe usage of optional chaining.
> 1 │ const { bar } = obj?.foo;
│ ^^
2 │
ℹ If it short-circuits with ‘undefined’ the evaluation will throw TypeError here:
> 1 │ const { bar } = obj?.foo;
│ ^^^^^^^^^^^^^^^^^^
2 │
(obj?.foo)();code-block.js:1:5 lint/correctness/noUnsafeOptionalChaining ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unsafe usage of optional chaining.
> 1 │ (obj?.foo)();
│ ^^
2 │
ℹ If it short-circuits with ‘undefined’ the evaluation will throw TypeError here:
> 1 │ (obj?.foo)();
│ ^^
2 │
(baz?.bar).foo;code-block.js:1:5 lint/correctness/noUnsafeOptionalChaining ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unsafe usage of optional chaining.
> 1 │ (baz?.bar).foo;
│ ^^
2 │
ℹ If it short-circuits with ‘undefined’ the evaluation will throw TypeError here:
> 1 │ (baz?.bar).foo;
│ ^^^
2 │
¥Valid
(obj?.foo)?.();obj?.foo();(obj?.foo ?? bar)();obj?.foo.bar;obj.foo?.bar;foo?.()?.bar;¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号