Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(no-focused-tests): migrate to TS (#314)
- Loading branch information
Showing
3 changed files
with
87 additions
and
65 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
src/rules/__tests__/no-focused-tests.test.js → src/rules/__tests__/no-focused-tests.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
||
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 }); | ||
} | ||
}, | ||
}), | ||
}); |