Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [no-non-null-asserted-optional-chain] infinite loop problem #5367

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down