Skip to content
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

Merged
merged 1 commit into from
Oct 4, 2024

Conversation

phillip-le
Copy link
Contributor

@phillip-le phillip-le commented Oct 4, 2024

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 the prefer-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 from vitest will still be hanging around after all of its usages are removed, not sure if there's a good way to handle this.

import { Mock } from 'vitest'

-(foo as Mock).mockReturnValue()
+(vi.mocked(foo)).mockReturnValue()

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 that Mock is coming from vitest.

import { Mock } from 'node:test'

-(foo as Mock).mockReturnValue()
+(vi.mocked(foo)).mockReturnValue()

Comment on lines +82 to +95
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +46 to +64
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;
}
Copy link
Contributor Author

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);",
Copy link
Contributor Author

@phillip-le phillip-le Oct 4, 2024

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);",

Copy link
Contributor Author

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;
Copy link
Contributor Author

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

@phillip-le phillip-le marked this pull request as ready for review October 4, 2024 11:29
@veritem
Copy link
Member

veritem commented Oct 4, 2024

for readme documentation, we really rely on https://github.com/bmish/eslint-doc-generator, which still needs to be updated to support eslint v9.

Copy link
Member

@veritem veritem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

@veritem veritem merged commit cb45ae7 into vitest-dev:main Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: prefer-vi-mocked
2 participants