From d03bcf49e9e4f068bead25a4bc4c962762d56c02 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sun, 1 Dec 2019 09:24:10 +1300 Subject: [PATCH] fix(no-focused-tests): detect table format uage of `.only.each` (#489) * refactor(no-focused-tests): use `isSupportedAccessor` utility function * fix(no-focused-tests): detect table format usage of `.only.each` --- src/rules/__tests__/no-focused-tests.test.ts | 12 ++++++++ src/rules/no-focused-tests.ts | 31 ++++++++------------ 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/rules/__tests__/no-focused-tests.test.ts b/src/rules/__tests__/no-focused-tests.test.ts index e76b9738e..52155e473 100644 --- a/src/rules/__tests__/no-focused-tests.test.ts +++ b/src/rules/__tests__/no-focused-tests.test.ts @@ -34,6 +34,10 @@ ruleTester.run('no-focused-tests', rule, { code: 'describe.only.each()', errors: [{ messageId: 'focusedTest', column: 10, line: 1 }], }, + { + code: 'describe.only.each`table`()', + errors: [{ messageId: 'focusedTest', column: 10, line: 1 }], + }, { code: 'describe["only"]()', errors: [{ messageId: 'focusedTest', column: 10, line: 1 }], @@ -46,6 +50,10 @@ ruleTester.run('no-focused-tests', rule, { code: 'it.only.each()', errors: [{ messageId: 'focusedTest', column: 4, line: 1 }], }, + { + code: 'it.only.each`table`()', + errors: [{ messageId: 'focusedTest', column: 4, line: 1 }], + }, { code: 'it["only"]()', errors: [{ messageId: 'focusedTest', column: 4, line: 1 }], @@ -58,6 +66,10 @@ ruleTester.run('no-focused-tests', rule, { code: 'test.only.each()', errors: [{ messageId: 'focusedTest', column: 6, line: 1 }], }, + { + code: 'test.only.each`table`()', + errors: [{ messageId: 'focusedTest', column: 6, line: 1 }], + }, { code: 'test["only"]()', errors: [{ messageId: 'focusedTest', column: 6, line: 1 }], diff --git a/src/rules/no-focused-tests.ts b/src/rules/no-focused-tests.ts index 0ab3837c0..7d1d93154 100644 --- a/src/rules/no-focused-tests.ts +++ b/src/rules/no-focused-tests.ts @@ -2,7 +2,12 @@ import { AST_NODE_TYPES, TSESTree, } from '@typescript-eslint/experimental-utils'; -import { DescribeAlias, TestCaseName, createRule } from './utils'; +import { + DescribeAlias, + TestCaseName, + createRule, + isSupportedAccessor, +} from './utils'; const testFunctions = new Set([ DescribeAlias.describe, @@ -17,14 +22,9 @@ const matchesTestFunction = (object: TSESTree.LeftHandSideExpression) => const isCallToFocusedTestFunction = (object: TSESTree.Identifier) => object.name.startsWith('f') && testFunctions.has(object.name.substring(1)); -const isPropertyNamedOnly = ( - property: TSESTree.Expression | TSESTree.Identifier, -) => - ('name' in property && property.name === 'only') || - ('value' in property && property.value === 'only'); - const isCallToTestOnlyFunction = (callee: TSESTree.MemberExpression) => - matchesTestFunction(callee.object) && isPropertyNamedOnly(callee.property); + matchesTestFunction(callee.object) && + isSupportedAccessor(callee.property, 'only'); export default createRule({ name: __filename, @@ -44,7 +44,10 @@ export default createRule({ defaultOptions: [], create: context => ({ CallExpression(node) { - const { callee } = node; + const callee = + node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression + ? node.callee.tag + : node.callee; if (callee.type === AST_NODE_TYPES.MemberExpression) { if ( @@ -78,16 +81,6 @@ export default createRule({ ) { context.report({ messageId: 'focusedTest', node: callee }); } - - if ( - callee.type === AST_NODE_TYPES.TaggedTemplateExpression && - callee.tag.type === AST_NODE_TYPES.MemberExpression && - callee.tag.object && - callee.tag.object.type === AST_NODE_TYPES.Identifier && - isCallToFocusedTestFunction(callee.tag.object) - ) { - context.report({ messageId: 'focusedTest', node: callee }); - } }, }), });