Skip to content

Commit

Permalink
chore(no-if): migrate to TS (#324)
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 4270fca commit 3a22ef1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
@@ -1,7 +1,7 @@
import { RuleTester } from 'eslint';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../no-if';

const ruleTester = new RuleTester({
const ruleTester = new TSESLint.RuleTester({
parserOptions: {
ecmaVersion: 6,
},
Expand Down
42 changes: 30 additions & 12 deletions src/rules/no-if.js → src/rules/no-if.ts
@@ -1,36 +1,54 @@
import { getDocsUrl, isTestCase } from './util';
import { TestCaseName, createRule, getNodeName, isTestCase } from './tsUtils';
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';

const isTestArrowFunction = node =>
node !== undefined &&
node.type === 'ArrowFunctionExpression' &&
isTestCase(node.parent);
const testCaseNames = new Set<string | null>([
...Object.keys(TestCaseName),
'it.only',
'it.skip',
'test.only',
'test.skip',
]);

export default {
const isTestArrowFunction = (node: TSESTree.ArrowFunctionExpression) =>
node.parent !== undefined &&
node.parent.type === AST_NODE_TYPES.CallExpression &&
testCaseNames.has(getNodeName(node.parent.callee));

export default createRule({
name: __filename,
meta: {
docs: {
description: 'Disallow conditional logic',
category: 'Best Practices',
recommended: false,
uri: getDocsUrl('jest/no-if'),
},
messages: {
noIf: 'Tests should not contain if statements.',
noConditional: 'Tests should not contain conditional statements.',
},
schema: [],
type: 'suggestion',
},

defaultOptions: [],
create(context) {
const stack = [];
const stack: Array<boolean> = [];

function validate(node) {
function validate(
node: TSESTree.ConditionalExpression | TSESTree.IfStatement,
) {
const lastElementInStack = stack[stack.length - 1];

if (stack.length === 0 || lastElementInStack === false) {
return;
}

const messageId =
node.type === 'ConditionalExpression' ? 'noConditional' : 'noIf';
node.type === AST_NODE_TYPES.ConditionalExpression
? 'noConditional'
: 'noIf';

context.report({
messageId,
Expand Down Expand Up @@ -67,4 +85,4 @@ export default {
},
};
},
};
});

0 comments on commit 3a22ef1

Please sign in to comment.