Skip to content

Commit

Permalink
chore(no-focused-tests): migrate to TS (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored and SimenB committed Jul 22, 2019
1 parent c455100 commit f3c654c
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 65 deletions.
@@ -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: [
Expand Down
63 changes: 0 additions & 63 deletions src/rules/no-focused-tests.js

This file was deleted.

85 changes: 85 additions & 0 deletions 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 });
}
},
}),
});

0 comments on commit f3c654c

Please sign in to comment.