Skip to content

Commit 0cb1df5

Browse files
G-RathSimenB
authored andcommitted
chore(utils): improve followTypeAssertionChain typing (#404)
1 parent 41d44d0 commit 0cb1df5

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/rules/prefer-to-contain.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import {
77
CallExpressionWithSingleArgument,
88
KnownCallExpression,
9+
MaybeTypeCast,
910
ModifierName,
1011
NotNegatableParsedModifier,
1112
ParsedEqualityMatcherCall,
@@ -27,7 +28,7 @@ const isBooleanLiteral = (node: TSESTree.Node): node is BooleanLiteral =>
2728
node.type === AST_NODE_TYPES.Literal && typeof node.value === 'boolean';
2829

2930
type ParsedBooleanEqualityMatcherCall = ParsedEqualityMatcherCall<
30-
BooleanLiteral
31+
MaybeTypeCast<BooleanLiteral>
3132
>;
3233

3334
/**
@@ -104,7 +105,7 @@ const getNegationFixes = (
104105
const negationPropertyDot = findPropertyDotToken(modifier.node, sourceCode);
105106

106107
const toContainFunc = buildToContainFuncExpectation(
107-
matcher.arguments[0].value,
108+
followTypeAssertionChain(matcher.arguments[0]).value,
108109
);
109110

110111
/* istanbul ignore if */
@@ -218,7 +219,7 @@ export default createRule({
218219
}
219220

220221
const toContainFunc = buildToContainFuncExpectation(
221-
!matcher.arguments[0].value,
222+
!followTypeAssertionChain(matcher.arguments[0]).value,
222223
);
223224

224225
const [containArg] = includesCall.arguments;

src/rules/utils.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,21 @@ interface TypeAssertionChain<
3333
Expression extends TSESTree.Expression = TSESTree.Expression
3434
> extends TSESTree.TSTypeAssertion {
3535
// expression: TypeAssertionChain<Expression> | Expression;
36-
expression: any; // https://github.com/typescript-eslint/typescript-eslint/issues/802
36+
expression: any; // todo: replace w/ above once typescript-eslint is updated to v2.0.0
3737
}
3838

39-
export const followTypeAssertionChain = (
40-
expression: TSESTree.Expression | TSTypeCastExpression,
41-
): TSESTree.Expression =>
42-
expression.type === AST_NODE_TYPES.TSAsExpression ||
43-
expression.type === AST_NODE_TYPES.TSTypeAssertion
39+
const isTypeCastExpression = <Expression extends TSESTree.Expression>(
40+
node: MaybeTypeCast<Expression>,
41+
): node is TSTypeCastExpression<Expression> =>
42+
node.type === AST_NODE_TYPES.TSAsExpression ||
43+
node.type === AST_NODE_TYPES.TSTypeAssertion;
44+
45+
export const followTypeAssertionChain = <
46+
Expression extends TSESTree.Expression
47+
>(
48+
expression: MaybeTypeCast<Expression>,
49+
): Expression =>
50+
isTypeCastExpression(expression)
4451
? followTypeAssertionChain(expression.expression)
4552
: expression;
4653

0 commit comments

Comments
 (0)