Skip to content

Commit 9a17da5

Browse files
committed
Traverse mixed call and member expressions
1 parent 9b5ca27 commit 9a17da5

1 file changed

Lines changed: 19 additions & 58 deletions

File tree

src/index.ts

Lines changed: 19 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -579,37 +579,7 @@ function isSortableTemplateExpression(
579579
| import('ast-types').namedTypes.TaggedTemplateExpression,
580580
functions: Set<string>,
581581
): boolean {
582-
if (node.tag.type === 'Identifier') {
583-
return functions.has(node.tag.name)
584-
}
585-
586-
if (node.tag.type === 'MemberExpression') {
587-
let expr = node.tag.object
588-
589-
// If the tag is a MemberExpression we should traverse all MemberExpression's until we find the leading Identifier
590-
while (expr.type === 'MemberExpression') {
591-
expr = expr.object
592-
}
593-
594-
if (expr.type === 'Identifier') {
595-
return functions.has(expr.name)
596-
}
597-
}
598-
599-
if (node.tag.type === 'CallExpression') {
600-
let expr = node.tag.callee
601-
602-
// If the tag is a CallExpression we should traverse all CallExpression's until we find the leading Identifier
603-
while (expr.type === 'CallExpression') {
604-
expr = expr.callee
605-
}
606-
607-
if (expr.type === 'Identifier') {
608-
return functions.has(expr.name)
609-
}
610-
}
611-
612-
return false
582+
return isSortableExpression(node.tag, functions)
613583
}
614584

615585
function isSortableCallExpression(
@@ -618,38 +588,29 @@ function isSortableCallExpression(
618588
| import('ast-types').namedTypes.CallExpression,
619589
functions: Set<string>,
620590
): boolean {
621-
if (!node.arguments?.length) {
622-
return false
623-
}
624-
625-
if (node.callee.type === 'Identifier') {
626-
return functions.has(node.callee.name)
627-
}
591+
if (!node.arguments?.length) return false
628592

629-
if (node.callee.type === 'MemberExpression') {
630-
let expr = node.callee.object
631-
632-
// If the tag is a MemberExpression we should traverse all MemberExpression's until we find the leading Identifier
633-
while (expr.type === 'MemberExpression') {
634-
expr = expr.object
635-
}
593+
return isSortableExpression(node.callee, functions)
594+
}
636595

637-
if (expr.type === 'Identifier') {
638-
return functions.has(expr.name)
596+
function isSortableExpression(
597+
node:
598+
| import('@babel/types').Expression
599+
| import('@babel/types').V8IntrinsicIdentifier
600+
| import('ast-types').namedTypes.ASTNode,
601+
functions: Set<string>,
602+
): boolean {
603+
// Traverse property accesses and function calls to find the leading ident
604+
while (node.type === 'CallExpression' || node.type === 'MemberExpression') {
605+
if (node.type === 'CallExpression') {
606+
node = node.callee
607+
} else if (node.type === 'MemberExpression') {
608+
node = node.object
639609
}
640610
}
641611

642-
if (node.callee.type === 'CallExpression') {
643-
let expr = node.callee.callee
644-
645-
// If the tag is a CallExpression we should traverse all CallExpression's until we find the leading Identifier
646-
while (expr.type === 'CallExpression') {
647-
expr = expr.callee
648-
}
649-
650-
if (expr.type === 'Identifier') {
651-
return functions.has(expr.name)
652-
}
612+
if (node.type === 'Identifier') {
613+
return functions.has(node.name)
653614
}
654615

655616
return false

0 commit comments

Comments
 (0)