Skip to content

Commit

Permalink
chore(utils): improve followTypeAssertionChain typing (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored and SimenB committed Aug 21, 2019
1 parent 41d44d0 commit 0cb1df5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/rules/prefer-to-contain.ts
Expand Up @@ -6,6 +6,7 @@ import {
import {
CallExpressionWithSingleArgument,
KnownCallExpression,
MaybeTypeCast,
ModifierName,
NotNegatableParsedModifier,
ParsedEqualityMatcherCall,
Expand All @@ -27,7 +28,7 @@ const isBooleanLiteral = (node: TSESTree.Node): node is BooleanLiteral =>
node.type === AST_NODE_TYPES.Literal && typeof node.value === 'boolean';

type ParsedBooleanEqualityMatcherCall = ParsedEqualityMatcherCall<
BooleanLiteral
MaybeTypeCast<BooleanLiteral>
>;

/**
Expand Down Expand Up @@ -104,7 +105,7 @@ const getNegationFixes = (
const negationPropertyDot = findPropertyDotToken(modifier.node, sourceCode);

const toContainFunc = buildToContainFuncExpectation(
matcher.arguments[0].value,
followTypeAssertionChain(matcher.arguments[0]).value,
);

/* istanbul ignore if */
Expand Down Expand Up @@ -218,7 +219,7 @@ export default createRule({
}

const toContainFunc = buildToContainFuncExpectation(
!matcher.arguments[0].value,
!followTypeAssertionChain(matcher.arguments[0]).value,
);

const [containArg] = includesCall.arguments;
Expand Down
19 changes: 13 additions & 6 deletions src/rules/utils.ts
Expand Up @@ -33,14 +33,21 @@ interface TypeAssertionChain<
Expression extends TSESTree.Expression = TSESTree.Expression
> extends TSESTree.TSTypeAssertion {
// expression: TypeAssertionChain<Expression> | Expression;
expression: any; // https://github.com/typescript-eslint/typescript-eslint/issues/802
expression: any; // todo: replace w/ above once typescript-eslint is updated to v2.0.0
}

export const followTypeAssertionChain = (
expression: TSESTree.Expression | TSTypeCastExpression,
): TSESTree.Expression =>
expression.type === AST_NODE_TYPES.TSAsExpression ||
expression.type === AST_NODE_TYPES.TSTypeAssertion
const isTypeCastExpression = <Expression extends TSESTree.Expression>(
node: MaybeTypeCast<Expression>,
): node is TSTypeCastExpression<Expression> =>
node.type === AST_NODE_TYPES.TSAsExpression ||
node.type === AST_NODE_TYPES.TSTypeAssertion;

export const followTypeAssertionChain = <
Expression extends TSESTree.Expression
>(
expression: MaybeTypeCast<Expression>,
): Expression =>
isTypeCastExpression(expression)
? followTypeAssertionChain(expression.expression)
: expression;

Expand Down

0 comments on commit 0cb1df5

Please sign in to comment.