Skip to content

Commit 6507a7a

Browse files
authored
chore: simplify getNodeName (#536)
* chore: use accessor functions * chore: remove duplicate return in switch * chore: move `joinNames` function out of `getNodeName`
1 parent 7becf4a commit 6507a7a

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

src/rules/utils.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -586,29 +586,24 @@ export type JestFunctionCallExpression<
586586
| JestFunctionCallExpressionWithMemberExpressionCallee<FunctionName>
587587
| JestFunctionCallExpressionWithIdentifierCallee<FunctionName>;
588588

589+
const joinNames = (a: string | null, b: string | null): string | null =>
590+
a && b ? `${a}.${b}` : null;
591+
589592
export function getNodeName(
590593
node:
591594
| JestFunctionMemberExpression<JestFunctionName>
592595
| JestFunctionIdentifier<JestFunctionName>,
593596
): string;
594597
export function getNodeName(node: TSESTree.Node): string | null;
595598
export function getNodeName(node: TSESTree.Node): string | null {
596-
function joinNames(a?: string | null, b?: string | null): string | null {
597-
return a && b ? `${a}.${b}` : null;
599+
if (isSupportedAccessor(node)) {
600+
return getAccessorValue(node);
598601
}
599602

600603
switch (node.type) {
601-
case AST_NODE_TYPES.Identifier:
602-
return node.name;
603-
case AST_NODE_TYPES.Literal:
604-
return `${node.value}`;
605-
case AST_NODE_TYPES.TemplateLiteral:
606-
if (node.expressions.length === 0) return node.quasis[0].value.cooked;
607-
break;
608604
case AST_NODE_TYPES.MemberExpression:
609605
return joinNames(getNodeName(node.object), getNodeName(node.property));
610606
case AST_NODE_TYPES.NewExpression:
611-
return getNodeName(node.callee);
612607
case AST_NODE_TYPES.CallExpression:
613608
return getNodeName(node.callee);
614609
}

0 commit comments

Comments
 (0)