From 99e8c30a2638cef6d6994bf497553bbf4f447184 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 20 Jul 2019 23:52:43 +1200 Subject: [PATCH 1/3] chore(consistent-test-it): convert to typescript --- ...-it.test.js => consistent-test-it.test.ts} | 4 +-- ...stent-test-it.js => consistent-test-it.ts} | 26 ++++++++++++++----- 2 files changed, 22 insertions(+), 8 deletions(-) rename src/rules/__tests__/{consistent-test-it.test.js => consistent-test-it.test.ts} (99%) rename src/rules/{consistent-test-it.js => consistent-test-it.ts} (84%) diff --git a/src/rules/__tests__/consistent-test-it.test.js b/src/rules/__tests__/consistent-test-it.test.ts similarity index 99% rename from src/rules/__tests__/consistent-test-it.test.js rename to src/rules/__tests__/consistent-test-it.test.ts index ca842d97a..50eeecfbb 100644 --- a/src/rules/__tests__/consistent-test-it.test.js +++ b/src/rules/__tests__/consistent-test-it.test.ts @@ -1,7 +1,7 @@ -import { RuleTester } from 'eslint'; +import { TSESLint } from '@typescript-eslint/experimental-utils'; import rule from '../consistent-test-it'; -const ruleTester = new RuleTester({ +const ruleTester = new TSESLint.RuleTester({ parserOptions: { ecmaVersion: 6, }, diff --git a/src/rules/consistent-test-it.js b/src/rules/consistent-test-it.ts similarity index 84% rename from src/rules/consistent-test-it.js rename to src/rules/consistent-test-it.ts index 94a52f717..81823c9f2 100644 --- a/src/rules/consistent-test-it.js +++ b/src/rules/consistent-test-it.ts @@ -1,9 +1,12 @@ -import { getDocsUrl, getNodeName, isDescribe, isTestCase } from './util'; +import { createRule, getNodeName, isDescribe, isTestCase } from './tsUtils'; -export default { +export default createRule({ + name: __filename, meta: { docs: { - url: getDocsUrl(__filename), + category: 'Best Practices', + description: 'Have control over `test` and `it` usages', + recommended: false, }, fixable: 'code', messages: { @@ -26,7 +29,14 @@ export default { additionalProperties: false, }, ], + type: 'suggestion', }, + defaultOptions: [ + { fn: 'test', withinDescribe: 'it' } as { + fn?: string; + withinDescribe?: string; + }, + ], create(context) { const configObj = context.options[0] || {}; const testKeyword = configObj.fn || 'test'; @@ -39,6 +49,10 @@ export default { CallExpression(node) { const nodeName = getNodeName(node.callee); + if (!nodeName) { + return; + } + if (isDescribe(node)) { describeNestingLevel++; } @@ -101,9 +115,9 @@ export default { }, }; }, -}; +}); -function getPreferredNodeName(nodeName, preferredTestKeyword) { +function getPreferredNodeName(nodeName: string, preferredTestKeyword: string) { switch (nodeName) { case 'fit': return 'test.only'; @@ -114,7 +128,7 @@ function getPreferredNodeName(nodeName, preferredTestKeyword) { } } -function getOppositeTestKeyword(test) { +function getOppositeTestKeyword(test: string) { if (test === 'test') { return 'it'; } From 35785d79b1bd24e1ac099ede58be49913a8bb686 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 20 Jul 2019 23:54:50 +1200 Subject: [PATCH 2/3] chore(consistent-test-it): use `AST_NODE_TYPES` enum --- src/rules/consistent-test-it.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/rules/consistent-test-it.ts b/src/rules/consistent-test-it.ts index 81823c9f2..6de9854ff 100644 --- a/src/rules/consistent-test-it.ts +++ b/src/rules/consistent-test-it.ts @@ -1,3 +1,4 @@ +import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils'; import { createRule, getNodeName, isDescribe, isTestCase } from './tsUtils'; export default createRule({ @@ -70,7 +71,7 @@ export default createRule({ data: { testKeyword, oppositeTestKeyword }, fix(fixer) { const nodeToReplace = - node.callee.type === 'MemberExpression' + node.callee.type === AST_NODE_TYPES.MemberExpression ? node.callee.object : node.callee; @@ -95,7 +96,7 @@ export default createRule({ data: { testKeywordWithinDescribe, oppositeTestKeyword }, fix(fixer) { const nodeToReplace = - node.callee.type === 'MemberExpression' + node.callee.type === AST_NODE_TYPES.MemberExpression ? node.callee.object : node.callee; From 60479fb1f7eb18a1c9212e45aaaddb2da2e69b41 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sun, 21 Jul 2019 00:45:42 +1200 Subject: [PATCH 3/3] chore(consistent-test-it): use union instead of string --- src/rules/consistent-test-it.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rules/consistent-test-it.ts b/src/rules/consistent-test-it.ts index 6de9854ff..9b0294f36 100644 --- a/src/rules/consistent-test-it.ts +++ b/src/rules/consistent-test-it.ts @@ -34,8 +34,8 @@ export default createRule({ }, defaultOptions: [ { fn: 'test', withinDescribe: 'it' } as { - fn?: string; - withinDescribe?: string; + fn?: 'it' | 'test'; + withinDescribe?: 'it' | 'test'; }, ], create(context) {