Skip to content

Commit

Permalink
chore: simplify getNodeName (#536)
Browse files Browse the repository at this point in the history
* chore: use accessor functions

* chore: remove duplicate return in switch

* chore: move `joinNames` function out of `getNodeName`
  • Loading branch information
G-Rath committed Feb 23, 2020
1 parent 7becf4a commit 6507a7a
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/rules/utils.ts
Expand Up @@ -586,29 +586,24 @@ export type JestFunctionCallExpression<
| JestFunctionCallExpressionWithMemberExpressionCallee<FunctionName>
| JestFunctionCallExpressionWithIdentifierCallee<FunctionName>;

const joinNames = (a: string | null, b: string | null): string | null =>
a && b ? `${a}.${b}` : null;

export function getNodeName(
node:
| JestFunctionMemberExpression<JestFunctionName>
| JestFunctionIdentifier<JestFunctionName>,
): string;
export function getNodeName(node: TSESTree.Node): string | null;
export function getNodeName(node: TSESTree.Node): string | null {
function joinNames(a?: string | null, b?: string | null): string | null {
return a && b ? `${a}.${b}` : null;
if (isSupportedAccessor(node)) {
return getAccessorValue(node);
}

switch (node.type) {
case AST_NODE_TYPES.Identifier:
return node.name;
case AST_NODE_TYPES.Literal:
return `${node.value}`;
case AST_NODE_TYPES.TemplateLiteral:
if (node.expressions.length === 0) return node.quasis[0].value.cooked;
break;
case AST_NODE_TYPES.MemberExpression:
return joinNames(getNodeName(node.object), getNodeName(node.property));
case AST_NODE_TYPES.NewExpression:
return getNodeName(node.callee);
case AST_NODE_TYPES.CallExpression:
return getNodeName(node.callee);
}
Expand Down

0 comments on commit 6507a7a

Please sign in to comment.