-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(prefer-vi-mocked): Add new prefer-vi-mocked rule #547
Conversation
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export type MaybeTypeCast<Expression extends TSESTree.Expression> = | ||
| TSTypeCastExpression<Expression> | ||
| Expression; | ||
|
||
export type TSTypeCastExpression< | ||
Expression extends TSESTree.Expression = TSESTree.Expression | ||
> = AsExpressionChain<Expression> | TypeAssertionChain<Expression>; | ||
|
||
interface AsExpressionChain< | ||
Expression extends TSESTree.Expression = TSESTree.Expression | ||
> extends TSESTree.TSAsExpression { | ||
expression: AsExpressionChain<Expression> | Expression; | ||
} | ||
|
||
interface TypeAssertionChain< | ||
Expression extends TSESTree.Expression = TSESTree.Expression | ||
> extends TSESTree.TSTypeAssertion { | ||
expression: TypeAssertionChain<Expression> | Expression; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
], | ||
invalid: [ | ||
{ | ||
code: "(foo as Mock).mockReturnValue(1);", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are almost exactly the same as the test cases as in https://github.com/jest-community/eslint-plugin-jest/blob/main/src/rules/__tests__/prefer-jest-mocked.test.ts
But without the jest.
prefix for the types
-code: "(foo as jest.Mock).mockReturnValue(1);",
+code: "(foo as Mock).mockReturnValue(1);",
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
if (typeName.type !== AST_NODE_TYPES.Identifier) return; | ||
|
||
if (!mockTypes.includes(typeName.name)) return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost the same as the jest version but only checking for Mock
instead of jest.Mock
for readme documentation, we really rely on https://github.com/bmish/eslint-doc-generator, which still needs to be updated to support eslint v9. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks
This is a port of prefer-jest-mocked
Resolves #443
Wasn't sure how to autogenerate the docs, so I took the output from
eslint-doc-generator --init-rule-docs
and manually modified it so that theprefer-vi-mocked
docs looked like the rest of the docs.Let me know if there's a preferred sorting order to the docs.
Known Limitations
The
Mock
import fromvitest
will still be hanging around after all of its usages are removed, not sure if there's a good way to handle this.Another problem is that if
Mock
is coming from somewhere else that would still be highlighted as an issue and autofixed since I'm not checking thatMock
is coming fromvitest
.