Skip to content

Commit

Permalink
fix false postive issues
Browse files Browse the repository at this point in the history
  • Loading branch information
veritem committed Dec 26, 2024
1 parent 339b919 commit 7e1ce6b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
18 changes: 10 additions & 8 deletions src/rules/valid-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ function getParentIfThenified(node: TSESTree.Node): TSESTree.Node {

const findPromiseCallExpressionNode = (node: TSESTree.Node) =>
node.parent?.parent
&& [AST_NODE_TYPES.CallExpression, AST_NODE_TYPES.ArrayExpression].includes(
node.parent.type
)
&& [AST_NODE_TYPES.CallExpression, AST_NODE_TYPES.ArrayExpression].includes(
node.parent.type
)
? getPromiseCallExpressionNode(node.parent)
: null

Expand All @@ -94,10 +94,10 @@ const isAcceptableReturnNode = (
node: TSESTree.Node,
allowReturn: boolean
): node is
| TSESTree.ConditionalExpression
| TSESTree.ArrowFunctionExpression
| TSESTree.AwaitExpression
| TSESTree.ReturnStatement => {
| TSESTree.ConditionalExpression
| TSESTree.ArrowFunctionExpression
| TSESTree.AwaitExpression
| TSESTree.ReturnStatement => {
if (allowReturn && node.type === AST_NODE_TYPES.ReturnStatement)
return true

Expand Down Expand Up @@ -248,6 +248,8 @@ export default createEslintRule<[
}
else if (vitestFnCall?.type !== 'expect') {
return
} else if (vitestFnCall.modifiers.some(mod => mod.type === AST_NODE_TYPES.Identifier && mod.name == "to")) {
return
}

const { parent: expect } = vitestFnCall.head.node
Expand Down Expand Up @@ -373,7 +375,7 @@ export default createEslintRule<[

if (alwaysAwait && returnStatement) {
const sourceCodeText
= context.sourceCode.getText(returnStatement)
= context.sourceCode.getText(returnStatement)
const replacedText = sourceCodeText.replace('return', 'await')

fixes.push(fixer.replaceText(returnStatement, replacedText))
Expand Down
16 changes: 7 additions & 9 deletions src/utils/parse-vitest-fn-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,28 +155,26 @@ const findModifiersAndMatcher = (

if (modifiers.length === 0) {
// the first modifier can be any of the three modifiers

if (!ModifierName.hasOwnProperty(name))
return 'modifier-unknown'
}
else if (modifiers.length === 1) {
// the second modifier can only be "not"
if (name !== ModifierName.not)
// the second modifier can only either be "not" or "have"
if (name !== ModifierName.not && name != ModifierName.have)
return 'modifier-unknown'

const firstModifier = getAccessorValue(modifiers[0])

// and the first modifier has to be either "resolves" or "rejects"
// and the first modifier has to be either "resolves" or "rejects" or "to"
if (
firstModifier !== ModifierName.resolves
&& firstModifier !== ModifierName.rejects
&& firstModifier !== ModifierName.to
)
return 'modifier-unknown'
}
else {
} else {
return 'modifier-unknown'
}

modifiers.push(member)
}

Expand Down Expand Up @@ -544,5 +542,5 @@ const isTypeCastExpression = <Expression extends TSESTree.Expression>(
export const followTypeAssertionChain = <Expression extends TSESTree.Expression>(
expression: MaybeTypeCast<Expression>
): Expression => isTypeCastExpression(expression)
? followTypeAssertionChain(expression.expression)
: expression
? followTypeAssertionChain(expression.expression)
: expression
2 changes: 2 additions & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export enum HookName {
}

export enum ModifierName {
to = 'to',
have = 'have',
not = 'not',
rejects = 'rejects',
resolves = 'resolves',
Expand Down
8 changes: 8 additions & 0 deletions tests/valid-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ ruleTester.run(RULE_NAME, rule, {
'expect(value, "message").toBe(1);',
'expect(value, `message`).toBe(1);',
'const message = "message"; expect(value, `${message}`).toBe(1);',
`it('example', () => {
expect("foo bar").to.include("foo");
});
`,
`it('example', () => {
expect("hey").to.have.property("foo", "bar")
});
`,
{
code: 'expect(1).toBe(2);',
options: [{ maxArgs: 2 }]
Expand Down

0 comments on commit 7e1ce6b

Please sign in to comment.