Skip to content

Commit

Permalink
chore(consistent-test-it): migrate to TS (#327)
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 26ddedd commit e938636
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
@@ -1,7 +1,7 @@
import { RuleTester } from 'eslint';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../consistent-test-it';

const ruleTester = new RuleTester({
const ruleTester = new TSESLint.RuleTester({
parserOptions: {
ecmaVersion: 6,
},
Expand Down
@@ -1,9 +1,13 @@
import { getDocsUrl, getNodeName, isDescribe, isTestCase } from './util';
import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils';
import { createRule, getNodeName, isDescribe, isTestCase } from './tsUtils';

export default {
export default createRule({
name: __filename,
meta: {
docs: {
url: getDocsUrl(__filename),
category: 'Best Practices',
description: 'Have control over `test` and `it` usages',
recommended: false,
},
fixable: 'code',
messages: {
Expand All @@ -26,7 +30,14 @@ export default {
additionalProperties: false,
},
],
type: 'suggestion',
},
defaultOptions: [
{ fn: 'test', withinDescribe: 'it' } as {
fn?: 'it' | 'test';
withinDescribe?: 'it' | 'test';
},
],
create(context) {
const configObj = context.options[0] || {};
const testKeyword = configObj.fn || 'test';
Expand All @@ -39,6 +50,10 @@ export default {
CallExpression(node) {
const nodeName = getNodeName(node.callee);

if (!nodeName) {
return;
}

if (isDescribe(node)) {
describeNestingLevel++;
}
Expand All @@ -56,7 +71,7 @@ export default {
data: { testKeyword, oppositeTestKeyword },
fix(fixer) {
const nodeToReplace =
node.callee.type === 'MemberExpression'
node.callee.type === AST_NODE_TYPES.MemberExpression
? node.callee.object
: node.callee;

Expand All @@ -81,7 +96,7 @@ export default {
data: { testKeywordWithinDescribe, oppositeTestKeyword },
fix(fixer) {
const nodeToReplace =
node.callee.type === 'MemberExpression'
node.callee.type === AST_NODE_TYPES.MemberExpression
? node.callee.object
: node.callee;

Expand All @@ -101,9 +116,9 @@ export default {
},
};
},
};
});

function getPreferredNodeName(nodeName, preferredTestKeyword) {
function getPreferredNodeName(nodeName: string, preferredTestKeyword: string) {
switch (nodeName) {
case 'fit':
return 'test.only';
Expand All @@ -114,7 +129,7 @@ function getPreferredNodeName(nodeName, preferredTestKeyword) {
}
}

function getOppositeTestKeyword(test) {
function getOppositeTestKeyword(test: string) {
if (test === 'test') {
return 'it';
}
Expand Down

0 comments on commit e938636

Please sign in to comment.