From 430de17abc453da833a697c6ca425f2cc50febcc Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Mon, 30 May 2022 07:55:38 +1200 Subject: [PATCH] fix: don't consider template tags in the middle of a possible jest function chain to be valid (#1133) --- src/rules/utils/__tests__/parseJestFnCall.test.ts | 3 +++ src/rules/utils/parseJestFnCall.ts | 15 +++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/rules/utils/__tests__/parseJestFnCall.test.ts b/src/rules/utils/__tests__/parseJestFnCall.test.ts index dba29a592..b4872e24c 100644 --- a/src/rules/utils/__tests__/parseJestFnCall.test.ts +++ b/src/rules/utils/__tests__/parseJestFnCall.test.ts @@ -118,6 +118,9 @@ ruleTester.run('nonexistent methods', rule, { '"something".describe()', '[].describe()', 'new describe().only()', + '``.test()', + 'test.only``()', + 'test``.only()', ], invalid: [], }); diff --git a/src/rules/utils/parseJestFnCall.ts b/src/rules/utils/parseJestFnCall.ts index 4824547ba..15fd18aa2 100644 --- a/src/rules/utils/parseJestFnCall.ts +++ b/src/rules/utils/parseJestFnCall.ts @@ -160,21 +160,21 @@ export const parseJestFnCall = ( return null; } - // ensure that the only call expression in the chain is at the end + // check that every link in the chain except the last is a member expression if ( chain .slice(0, chain.length - 1) - .some(nod => nod.parent?.type === AST_NODE_TYPES.CallExpression) + .some(nod => nod.parent?.type !== AST_NODE_TYPES.MemberExpression) ) { return null; } const [first, ...rest] = chain; - const lastNode = chain[chain.length - 1]; + const lastLink = getAccessorValue(chain[chain.length - 1]); // if we're an `each()`, ensure we're the outer CallExpression (i.e `.each()()`) - if (isSupportedAccessor(lastNode, 'each')) { + if (lastLink === 'each') { if ( node.callee.type !== AST_NODE_TYPES.CallExpression && node.callee.type !== AST_NODE_TYPES.TaggedTemplateExpression @@ -183,6 +183,13 @@ export const parseJestFnCall = ( } } + if ( + node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression && + lastLink !== 'each' + ) { + return null; + } + const resolved = resolveToJestFn(scope, getAccessorValue(first)); // we're not a jest function