diff --git a/packages/eslint-plugin/src/rules/no-non-null-asserted-optional-chain.ts b/packages/eslint-plugin/src/rules/no-non-null-asserted-optional-chain.ts index 9e1b3c94656e..41fa2f8e1503 100644 --- a/packages/eslint-plugin/src/rules/no-non-null-asserted-optional-chain.ts +++ b/packages/eslint-plugin/src/rules/no-non-null-asserted-optional-chain.ts @@ -102,32 +102,39 @@ export default util.createRule({ ].join(', ')](child: TSESTree.Node): void { // selector guarantees this assertion const node = child.parent as TSESTree.TSNonNullExpression; + const hasOptionalChain = ((): boolean => { + let current = child; + while (current) { + switch (current.type) { + case AST_NODE_TYPES.MemberExpression: + if (current.optional) { + // found an optional chain! stop traversing + return true; + } - let current = child; - while (current) { - switch (current.type) { - case AST_NODE_TYPES.MemberExpression: - if (current.optional) { - // found an optional chain! stop traversing - break; - } + current = current.object; + continue; - current = current.object; - continue; + case AST_NODE_TYPES.CallExpression: + if (current.optional) { + // found an optional chain! stop traversing + return true; + } - case AST_NODE_TYPES.CallExpression: - if (current.optional) { - // found an optional chain! stop traversing - break; - } + current = current.callee; + continue; - current = current.callee; - continue; - - default: - // something that's not a ChainElement, which means this is not an optional chain we want to check - return; + default: + // something that's not a ChainElement, which means this is not an optional chain we want to check + return false; + } } + + return true; + })(); + + if (!hasOptionalChain) { + return; } context.report({
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: