Skip to content

Commit

Permalink
chore: use enum values instead of string literals (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored and SimenB committed Sep 30, 2019
1 parent ac7d826 commit 2ef1615
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
13 changes: 11 additions & 2 deletions src/rules/no-empty-title.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { createRule, isDescribe, isStringNode, isTestCase } from './utils';
import {
DescribeAlias,
TestCaseName,
createRule,
isDescribe,
isStringNode,
isTestCase,
} from './utils';

export default createRule({
name: __filename,
Expand Down Expand Up @@ -28,7 +35,9 @@ export default createRule({
}

context.report({
messageId: isDescribe(node) ? 'describe' : 'test',
messageId: isDescribe(node)
? DescribeAlias.describe
: TestCaseName.test,
node,
});
},
Expand Down
6 changes: 5 additions & 1 deletion src/rules/no-focused-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {
} from '@typescript-eslint/experimental-utils';
import { DescribeAlias, TestCaseName, createRule } from './utils';

const testFunctions = new Set(['describe', 'it', 'test']);
const testFunctions = new Set<string>([
DescribeAlias.describe,
TestCaseName.test,
TestCaseName.it,
]);

const matchesTestFunction = (
object: TSESTree.LeftHandSideExpression | undefined,
Expand Down
15 changes: 8 additions & 7 deletions src/rules/no-standalone-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
TSESTree,
} from '@typescript-eslint/experimental-utils';
import {
DescribeAlias,
TestCaseName,
createRule,
isDescribe,
Expand All @@ -13,7 +14,7 @@ import {

const getBlockType = (
stmt: TSESTree.BlockStatement,
): 'function' | 'describe' | null => {
): 'function' | DescribeAlias.describe | null => {
const func = stmt.parent;

/* istanbul ignore if */
Expand All @@ -34,7 +35,7 @@ const getBlockType = (
}
// if it's not a variable, it will be callExpr, we only care about describe
if (expr.type === AST_NODE_TYPES.CallExpression && isDescribe(expr)) {
return 'describe';
return DescribeAlias.describe;
}
}
return null;
Expand All @@ -60,9 +61,9 @@ const isEach = (node: TSESTree.CallExpression): boolean => {
};

type callStackEntry =
| 'test'
| TestCaseName.test
| 'function'
| 'describe'
| DescribeAlias.describe
| 'arrowFunc'
| 'template';

Expand All @@ -88,13 +89,13 @@ export default createRule({
CallExpression(node) {
if (isExpectCall(node)) {
const parent = callStack[callStack.length - 1];
if (!parent || parent === 'describe') {
if (!parent || parent === DescribeAlias.describe) {
context.report({ node, messageId: 'unexpectedExpect' });
}
return;
}
if (isTestCase(node)) {
callStack.push('test');
callStack.push(TestCaseName.test);
}
if (node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression) {
callStack.push('template');
Expand All @@ -106,7 +107,7 @@ export default createRule({
(((isTestCase(node) &&
node.callee.type !== AST_NODE_TYPES.MemberExpression) ||
isEach(node)) &&
top === 'test') ||
top === TestCaseName.test) ||
(node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression &&
top === 'template')
) {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/prefer-todo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const isTargetedTestCase = (
node: TSESTree.CallExpression,
): node is JestFunctionCallExpression<TestCaseName> =>
isTestCase(node) &&
(['it', 'test', 'it.skip', 'test.skip'] as Array<string | null>).includes(
[TestCaseName.it, TestCaseName.test, 'it.skip', 'test.skip'].includes(
getNodeName(node.callee),
);

Expand Down

0 comments on commit 2ef1615

Please sign in to comment.