From b0cfdca91afc6476445bc45347dd5823c66535d2 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 19 Jul 2019 18:48:35 +1200 Subject: [PATCH] chore: convert `no-focused-tests` rule to TypeScript --- ...tests.test.js => no-focused-tests.test.ts} | 4 +- src/rules/no-focused-tests.js | 63 -------------- src/rules/no-focused-tests.ts | 85 +++++++++++++++++++ 3 files changed, 87 insertions(+), 65 deletions(-) rename src/rules/__tests__/{no-focused-tests.test.js => no-focused-tests.test.ts} (93%) delete mode 100644 src/rules/no-focused-tests.js create mode 100644 src/rules/no-focused-tests.ts diff --git a/src/rules/__tests__/no-focused-tests.test.js b/src/rules/__tests__/no-focused-tests.test.ts similarity index 93% rename from src/rules/__tests__/no-focused-tests.test.js rename to src/rules/__tests__/no-focused-tests.test.ts index 45d030de5..d97be69df 100644 --- a/src/rules/__tests__/no-focused-tests.test.js +++ b/src/rules/__tests__/no-focused-tests.test.ts @@ -1,7 +1,7 @@ -import { RuleTester } from 'eslint'; +import { TSESLint } from '@typescript-eslint/experimental-utils'; import rule from '../no-focused-tests'; -const ruleTester = new RuleTester(); +const ruleTester = new TSESLint.RuleTester(); ruleTester.run('no-focused-tests', rule, { valid: [ diff --git a/src/rules/no-focused-tests.js b/src/rules/no-focused-tests.js deleted file mode 100644 index 96d982a2c..000000000 --- a/src/rules/no-focused-tests.js +++ /dev/null @@ -1,63 +0,0 @@ -import { getDocsUrl } from './util'; - -const testFunctions = new Set(['describe', 'it', 'test']); - -const matchesTestFunction = object => object && testFunctions.has(object.name); - -const isCallToFocusedTestFunction = object => - object && - object.name[0] === 'f' && - testFunctions.has(object.name.substring(1)); - -const isPropertyNamedOnly = property => - property && (property.name === 'only' || property.value === 'only'); - -const isCallToTestOnlyFunction = callee => - matchesTestFunction(callee.object) && isPropertyNamedOnly(callee.property); - -export default { - meta: { - docs: { - url: getDocsUrl(__filename), - }, - messages: { - focusedTest: 'Unexpected focused test.', - }, - schema: [], - }, - create: context => ({ - CallExpression(node) { - const { callee } = node; - - if (callee.type === 'MemberExpression') { - if ( - callee.object.type === 'Identifier' && - isCallToFocusedTestFunction(callee.object) - ) { - context.report({ messageId: 'focusedTest', node: callee.object }); - return; - } - - if ( - callee.object.type === 'MemberExpression' && - isCallToTestOnlyFunction(callee.object) - ) { - context.report({ - messageId: 'focusedTest', - node: callee.object.property, - }); - return; - } - - if (isCallToTestOnlyFunction(callee)) { - context.report({ messageId: 'focusedTest', node: callee.property }); - return; - } - } - - if (callee.type === 'Identifier' && isCallToFocusedTestFunction(callee)) { - context.report({ messageId: 'focusedTest', node: callee }); - } - }, - }), -}; diff --git a/src/rules/no-focused-tests.ts b/src/rules/no-focused-tests.ts new file mode 100644 index 000000000..cb72c04a5 --- /dev/null +++ b/src/rules/no-focused-tests.ts @@ -0,0 +1,85 @@ +import { + AST_NODE_TYPES, + TSESTree, +} from '@typescript-eslint/experimental-utils'; +import { DescribeAlias, TestCaseName, createRule } from './tsUtils'; + +const testFunctions = new Set(['describe', 'it', 'test']); + +const matchesTestFunction = ( + object: TSESTree.LeftHandSideExpression | undefined, +) => + object && + 'name' in object && + (object.name in TestCaseName || object.name in DescribeAlias); + +const isCallToFocusedTestFunction = (object: TSESTree.Identifier | undefined) => + object && + object.name[0] === 'f' && + testFunctions.has(object.name.substring(1)); + +const isPropertyNamedOnly = ( + property: TSESTree.Expression | TSESTree.Identifier | undefined, +) => + property && + (('name' in property && property.name === 'only') || + ('value' in property && property.value === 'only')); + +const isCallToTestOnlyFunction = (callee: TSESTree.MemberExpression) => + matchesTestFunction(callee.object) && isPropertyNamedOnly(callee.property); + +export default createRule({ + name: __filename, + meta: { + docs: { + category: 'Best Practices', + description: 'Disallow focused tests', + recommended: false, + }, + messages: { + focusedTest: 'Unexpected focused test.', + }, + fixable: 'code', + schema: [], + type: 'suggestion', + }, + defaultOptions: [], + create: context => ({ + CallExpression(node) { + const { callee } = node; + + if (callee.type === AST_NODE_TYPES.MemberExpression) { + if ( + callee.object.type === AST_NODE_TYPES.Identifier && + isCallToFocusedTestFunction(callee.object) + ) { + context.report({ messageId: 'focusedTest', node: callee.object }); + return; + } + + if ( + callee.object.type === AST_NODE_TYPES.MemberExpression && + isCallToTestOnlyFunction(callee.object) + ) { + context.report({ + messageId: 'focusedTest', + node: callee.object.property, + }); + return; + } + + if (isCallToTestOnlyFunction(callee)) { + context.report({ messageId: 'focusedTest', node: callee.property }); + return; + } + } + + if ( + callee.type === AST_NODE_TYPES.Identifier && + isCallToFocusedTestFunction(callee) + ) { + context.report({ messageId: 'focusedTest', node: callee }); + } + }, + }), +});