Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { DescribeAlias, TestCaseName, createRule } from './tsUtils';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot to export these new ones

image

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O.o I feel like I need to commit sudoku - how did I miss that...?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually no it just needs to be merged w/ reapply-ts :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, you've cherry-picked. Makes sense 🙂

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it got lost in the shuffle. The whole guard thing + timezones threw me for a loop 😂


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 });
}
},
}),
});