no-unsafe-optional-chaining

不允许在不允许 undefined 值的上下文中使用可选链接

配置文件 中的 "extends": "eslint:recommended" 属性启用了该规则

可选链接 (?.) 表达式可以短路,返回值 undefined。因此,将评估的可选链接表达式视为函数、对象、数字等,可能会导致 TypeError 或意外结果。例如:

var obj = undefined;

1 in obj?.foo;  // TypeError
with (obj?.foo);  // TypeError
for (bar of obj?.foo);  // TypeError
bar instanceof obj?.foo;  // TypeError
const { bar } = obj?.foo;  // TypeError

此外,括号限制了链中短路的范围。例如:

var obj = undefined;

(obj?.foo)(); // TypeError
(obj?.foo).bar; // TypeError

规则详情

此规则旨在检测使用可选链接无法防止运行时错误的某些情况。特别是,它在短路到 undefined 导致之后抛出 TypeError 的位置标记可选链接表达式。

LgSGhS4Eu5BaRrV1M6DRh/7mOjSCWEn2QA4GeCAQN0eL78v76+7pl7PMZusoamcN

/*eslint no-unsafe-optional-chaining: "error"*/

(obj?.foo)();

(obj?.foo).bar;

(foo?.()).bar;

(foo?.()).bar();

(obj?.foo ?? obj?.bar)();

(foo || obj?.foo)();

(obj?.foo && foo)();

(foo ? obj?.foo : bar)();

(foo, obj?.bar).baz;

(obj?.foo)`template`;

new (obj?.foo)();

[...obj?.foo];

bar(...obj?.foo);

1 in obj?.foo;

bar instanceof obj?.foo;

for (bar of obj?.foo);

const { bar } = obj?.foo;

[{ bar } = obj?.foo] = [];

with (obj?.foo);

class A extends obj?.foo {}

var a = class A extends obj?.foo {};

async function foo () {
    const { bar } = await obj?.foo;
   (await obj?.foo)();
   (await obj?.foo).bar;
}

9yoNpjr6gd6zh0cwZMnzCpDrCM6/zTdrRqXIvlUhfaHsSeCWilcYpPgpMZ09hE80

/*eslint no-unsafe-optional-chaining: "error"*/

(obj?.foo)?.();

obj?.foo();

(obj?.foo ?? bar)();

obj?.foo.bar;

obj.foo?.bar;

foo?.()?.bar;

(obj?.foo ?? bar)`template`;

new (obj?.foo ?? bar)();

var baz = {...obj?.foo};

const { bar } = obj?.foo || baz;

async function foo () {
  const { bar } = await obj?.foo || baz;
   (await obj?.foo)?.();
   (await obj?.foo)?.bar;
}

选项

EqbG4lIeke3aUluVVd96kxNax4D0ktjjRISIsMOdoUqEXdzn63l7ZBotwJT0SnUK

    oFbDAgjQsKL58IfJCWRuO4UTV8MqsvfyfZH5/h5AxVWdbHi0DN0wNGtNYCTxVIHkwNp/EEL32aQqA8J9AhSwReWeS/vgnusRKWMmILzoryxGDwdSadGNAFSwx92uTFNtdauXCktLTO0O4ePwZDUqV8JRYmI9cdACRiqE8UrdeyfsnlmRss0L1FVkrUjbzlkPi5ItyAHiRrMbEyw1LjXbe8f93ZZe5SPbFNWgZ3uCozr6vZOx0ilALIUa6cJFIvcMKEPyR6bUlBzw5LJnOXymTFB7FoM+jYgvp0O4Nc6oEhmiS4/8KdP+gU6eP9dXqzbq2OL0adS6G8f6a73/ABu5kZb0RLbrgaT1S8NCI0U5nvA=

disallowArithmeticOperators

xZ+A4Fw/Hknk3Rm9fufX9wv2NKx6k7o0WbbixxTp0PZ7dDP4O4OVKIop9jKPj2/N4SCbFdU1XbQrS/PcfSCi0WFNX/dq9ts1DU59AgaNRdmYKIWpzTVnD9QFyoxZ2cnz

    9ZpWZxmWXIlsT0lA7jh0vKm0ATOnpKtaUiKKarI+V7Bn+xIxI+x/UpxXY2p1hy1N3fmdWQIMb4BOd5+V5sJqULFSaDJSpsM6R38lvZxyCW34haLhDIZU27WSo7aAH99taqb5MnCQZW4ZNS/cEX97aNviOkIsZklhCGLKfTktP5+6vHClLeaTTjyugeh0U1kC4i+Zs+FVD5lbwGF91YiuDbrEeAbk3PQ7y+icqPHAebZRPYuDdWj9Ru3rG/5IzrbxdIbmYQGtgZZATVGwu4h04EkZHfmlqBk+9QmaE5JQ6FEWoX4F7W5RItaXFzVnsQAa3XExp/O9QI4CsQ8PE+frdSS+k5h7O50Yu/5YydDUK22rg+QLzSPIK+puupjTAbpwNP1igitaTQ2T2TZJiejp1eMjPALk20Vsx+HqhSxjO1HJY3PlH0+4gZ8D7FfVg/uP

+WNthHaG78UucD9Wr3O8vwpT70OivdmmaCRkmrbnK+UKmfsV6ozpu8G/v7TY4ND7w1JyIn5Q/uAsZCI96TS6zOVWRdgXWI7BMnj77yHXay7NkJ4gxm9CaVJRfTlgQ26sDr0PRpW5Qu10l8Y4gXR+Pw==

/*eslint no-unsafe-optional-chaining: ["error", { "disallowArithmeticOperators": true }]*/

+obj?.foo;
-obj?.foo;

obj?.foo + bar;
obj?.foo - bar;
obj?.foo / bar;
obj?.foo * bar;
obj?.foo % bar;
obj?.foo ** bar;

baz += obj?.foo;
baz -= obj?.foo;
baz /= obj?.foo;
baz *= obj?.foo;
baz %= obj?.foo;
baz **= obj?.foo;

async function foo () {
  +await obj?.foo;
  await obj?.foo + bar;
  baz += await obj?.foo;
}