Skip to content

Commit

Permalink
fix: code modified to a cleaner way
Browse files Browse the repository at this point in the history
  • Loading branch information
yqiaoxu committed Aug 19, 2022
1 parent c847de9 commit ac7ccb4
Showing 1 changed file with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,33 +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;
let isRight = true;
while (current && isRight) {
switch (current.type) {
case AST_NODE_TYPES.MemberExpression:
if (current.optional) {
// found an optional chain! stop traversing
isRight = false;
}
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
isRight = false;
}
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

0 comments on commit ac7ccb4

Please sign in to comment.