Skip to content

Commit

Permalink
chore(prefer-todo): use common guards (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored and SimenB committed Aug 11, 2019
1 parent 2ec3f12 commit 3a7a691
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
27 changes: 6 additions & 21 deletions src/rules/prefer-todo.ts
Expand Up @@ -6,19 +6,15 @@ import {
import {
FunctionExpression,
JestFunctionCallExpression,
StringLiteral,
TestCaseName,
createRule,
getNodeName,
hasOnlyOneArgument,
isFunction,
isStringNode,
isTestCase,
} from './tsUtils';

function isOnlyTestTitle(node: TSESTree.CallExpression) {
return node.arguments.length === 1;
}

function isFunctionBodyEmpty(node: FunctionExpression) {
/* istanbul ignore next https://github.com/typescript-eslint/typescript-eslint/issues/734 */
if (!node.body) {
Expand Down Expand Up @@ -49,16 +45,6 @@ function addTodo(
return fixer.replaceText(node.callee, `${testName}.todo`);
}

interface CallExpressionWithStringArgument extends TSESTree.CallExpression {
arguments: [StringLiteral | TSESTree.TemplateLiteral];
}

function isFirstArgString(
node: TSESTree.CallExpression,
): node is CallExpressionWithStringArgument {
return node.arguments[0] && isStringNode(node.arguments[0]);
}

const isTargetedTestCase = (
node: TSESTree.CallExpression,
): node is JestFunctionCallExpression<TestCaseName> =>
Expand Down Expand Up @@ -88,7 +74,9 @@ export default createRule({
create(context) {
return {
CallExpression(node) {
if (!isTargetedTestCase(node) || !isFirstArgString(node)) {
const [firstArg, secondArg] = node.arguments;

if (!isTargetedTestCase(node) || !isStringNode(firstArg)) {
return;
}

Expand All @@ -97,16 +85,13 @@ export default createRule({
messageId: 'todoOverEmpty',
node,
fix: fixer => [
fixer.removeRange([
node.arguments[0].range[1],
node.arguments[1].range[1],
]),
fixer.removeRange([firstArg.range[1], secondArg.range[1]]),
addTodo(node, fixer),
],
});
}

if (isOnlyTestTitle(node)) {
if (hasOnlyOneArgument(node)) {
context.report({
messageId: 'todoOverUnimplemented',
node,
Expand Down
21 changes: 21 additions & 0 deletions src/rules/tsUtils.ts
Expand Up @@ -108,6 +108,27 @@ export const isStringNode = <V extends string>(
export const getStringValue = <S extends string>(node: StringNode<S>): S =>
isTemplateLiteral(node) ? node.quasis[0].value.raw : node.value;

/**
* Represents a `CallExpression` with a single argument.
*/
export interface CallExpressionWithSingleArgument<
Argument extends TSESTree.Expression = TSESTree.Expression
> extends TSESTree.CallExpression {
arguments: [Argument];
}

/**
* Guards that the given `call` has only one `argument`.
*
* @param {CallExpression} call
*
* @return {call is CallExpressionWithSingleArgument}
*/
export const hasOnlyOneArgument = (
call: TSESTree.CallExpression,
): call is CallExpressionWithSingleArgument =>
call.arguments && call.arguments.length === 1;

/**
* Gets the value of the given `AccessorNode`,
* account for the different node types.
Expand Down

0 comments on commit 3a7a691

Please sign in to comment.