From bfa0bb4f7e0d7a813985881748cdc36e5a505ef5 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 20 Jul 2019 23:03:47 +1200 Subject: [PATCH 1/3] chore(no-if): convert to typescript --- .../__tests__/{no-if.js => no-if.test.ts} | 4 +- src/rules/{no-if.js => no-if.ts} | 40 ++++++++++++++----- 2 files changed, 31 insertions(+), 13 deletions(-) rename src/rules/__tests__/{no-if.js => no-if.test.ts} (98%) rename src/rules/{no-if.js => no-if.ts} (59%) diff --git a/src/rules/__tests__/no-if.js b/src/rules/__tests__/no-if.test.ts similarity index 98% rename from src/rules/__tests__/no-if.js rename to src/rules/__tests__/no-if.test.ts index 680ce8c55..f6e59fee4 100644 --- a/src/rules/__tests__/no-if.js +++ b/src/rules/__tests__/no-if.test.ts @@ -1,7 +1,7 @@ -import { RuleTester } from 'eslint'; +import { TSESLint } from '@typescript-eslint/experimental-utils'; import rule from '../no-if'; -const ruleTester = new RuleTester({ +const ruleTester = new TSESLint.RuleTester({ parserOptions: { ecmaVersion: 6, }, diff --git a/src/rules/no-if.js b/src/rules/no-if.ts similarity index 59% rename from src/rules/no-if.js rename to src/rules/no-if.ts index 320729711..9d1c1ffaf 100644 --- a/src/rules/no-if.js +++ b/src/rules/no-if.ts @@ -1,28 +1,44 @@ -import { getDocsUrl, isTestCase } from './util'; +import { TestCaseName, createRule, getNodeName, isTestCase } from './tsUtils'; +import { + AST_NODE_TYPES, + TSESTree, +} from '@typescript-eslint/experimental-utils'; -const isTestArrowFunction = node => +const testCaseNames = new Set([ + ...Object.keys(TestCaseName), + 'it.only', + 'it.skip', + 'test.only', + 'test.skip', +]); + +const isTestArrowFunction = (node: TSESTree.Node) => node !== undefined && - node.type === 'ArrowFunctionExpression' && - isTestCase(node.parent); + node.type === AST_NODE_TYPES.ArrowFunctionExpression && + node.parent !== undefined && + node.parent.type === AST_NODE_TYPES.CallExpression && + testCaseNames.has(getNodeName(node.parent.callee)); -export default { +export default createRule({ + name: __filename, meta: { docs: { description: 'Disallow conditional logic', category: 'Best Practices', recommended: false, - uri: getDocsUrl('jest/no-if'), }, messages: { noIf: 'Tests should not contain if statements.', noConditional: 'Tests should not contain conditional statements.', }, + schema: [], + type: 'suggestion', }, - + defaultOptions: [], create(context) { - const stack = []; + const stack: Array = []; - function validate(node) { + function validate(node: TSESTree.Node) { const lastElementInStack = stack[stack.length - 1]; if (stack.length === 0 || lastElementInStack === false) { @@ -30,7 +46,9 @@ export default { } const messageId = - node.type === 'ConditionalExpression' ? 'noConditional' : 'noIf'; + node.type === AST_NODE_TYPES.ConditionalExpression + ? 'noConditional' + : 'noIf'; context.report({ messageId, @@ -67,4 +85,4 @@ export default { }, }; }, -}; +}); From e2d8fa2a78e1c2eab55b750bc47bb19800c86e9b Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 20 Jul 2019 23:16:54 +1200 Subject: [PATCH 2/3] chore(no-if): narrow parameter type of `validate` --- src/rules/no-if.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/rules/no-if.ts b/src/rules/no-if.ts index 9d1c1ffaf..74ac5abb8 100644 --- a/src/rules/no-if.ts +++ b/src/rules/no-if.ts @@ -38,7 +38,9 @@ export default createRule({ create(context) { const stack: Array = []; - function validate(node: TSESTree.Node) { + function validate( + node: TSESTree.ConditionalExpression | TSESTree.IfStatement, + ) { const lastElementInStack = stack[stack.length - 1]; if (stack.length === 0 || lastElementInStack === false) { From 9ff0da25eddb76d13d4662408876d494c5c59abf Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 20 Jul 2019 23:18:57 +1200 Subject: [PATCH 3/3] chore(no-if): remove unneeded checks from `isTestArrowFunction` fn --- src/rules/no-if.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/rules/no-if.ts b/src/rules/no-if.ts index 74ac5abb8..f08daa375 100644 --- a/src/rules/no-if.ts +++ b/src/rules/no-if.ts @@ -12,9 +12,7 @@ const testCaseNames = new Set([ 'test.skip', ]); -const isTestArrowFunction = (node: TSESTree.Node) => - node !== undefined && - node.type === AST_NODE_TYPES.ArrowFunctionExpression && +const isTestArrowFunction = (node: TSESTree.ArrowFunctionExpression) => node.parent !== undefined && node.parent.type === AST_NODE_TYPES.CallExpression && testCaseNames.has(getNodeName(node.parent.callee));