Skip to content

Commit 7e1ce6b

Browse files
committed
fix false postive issues
1 parent 339b919 commit 7e1ce6b

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

src/rules/valid-expect.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ function getParentIfThenified(node: TSESTree.Node): TSESTree.Node {
7575

7676
const findPromiseCallExpressionNode = (node: TSESTree.Node) =>
7777
node.parent?.parent
78-
&& [AST_NODE_TYPES.CallExpression, AST_NODE_TYPES.ArrayExpression].includes(
79-
node.parent.type
80-
)
78+
&& [AST_NODE_TYPES.CallExpression, AST_NODE_TYPES.ArrayExpression].includes(
79+
node.parent.type
80+
)
8181
? getPromiseCallExpressionNode(node.parent)
8282
: null
8383

@@ -94,10 +94,10 @@ const isAcceptableReturnNode = (
9494
node: TSESTree.Node,
9595
allowReturn: boolean
9696
): node is
97-
| TSESTree.ConditionalExpression
98-
| TSESTree.ArrowFunctionExpression
99-
| TSESTree.AwaitExpression
100-
| TSESTree.ReturnStatement => {
97+
| TSESTree.ConditionalExpression
98+
| TSESTree.ArrowFunctionExpression
99+
| TSESTree.AwaitExpression
100+
| TSESTree.ReturnStatement => {
101101
if (allowReturn && node.type === AST_NODE_TYPES.ReturnStatement)
102102
return true
103103

@@ -248,6 +248,8 @@ export default createEslintRule<[
248248
}
249249
else if (vitestFnCall?.type !== 'expect') {
250250
return
251+
} else if (vitestFnCall.modifiers.some(mod => mod.type === AST_NODE_TYPES.Identifier && mod.name == "to")) {
252+
return
251253
}
252254

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

374376
if (alwaysAwait && returnStatement) {
375377
const sourceCodeText
376-
= context.sourceCode.getText(returnStatement)
378+
= context.sourceCode.getText(returnStatement)
377379
const replacedText = sourceCodeText.replace('return', 'await')
378380

379381
fixes.push(fixer.replaceText(returnStatement, replacedText))

src/utils/parse-vitest-fn-call.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,28 +155,26 @@ const findModifiersAndMatcher = (
155155

156156
if (modifiers.length === 0) {
157157
// the first modifier can be any of the three modifiers
158-
159158
if (!ModifierName.hasOwnProperty(name))
160159
return 'modifier-unknown'
161160
}
162161
else if (modifiers.length === 1) {
163-
// the second modifier can only be "not"
164-
if (name !== ModifierName.not)
162+
// the second modifier can only either be "not" or "have"
163+
if (name !== ModifierName.not && name != ModifierName.have)
165164
return 'modifier-unknown'
166165

167166
const firstModifier = getAccessorValue(modifiers[0])
168167

169-
// and the first modifier has to be either "resolves" or "rejects"
168+
// and the first modifier has to be either "resolves" or "rejects" or "to"
170169
if (
171170
firstModifier !== ModifierName.resolves
172171
&& firstModifier !== ModifierName.rejects
172+
&& firstModifier !== ModifierName.to
173173
)
174174
return 'modifier-unknown'
175-
}
176-
else {
175+
} else {
177176
return 'modifier-unknown'
178177
}
179-
180178
modifiers.push(member)
181179
}
182180

@@ -544,5 +542,5 @@ const isTypeCastExpression = <Expression extends TSESTree.Expression>(
544542
export const followTypeAssertionChain = <Expression extends TSESTree.Expression>(
545543
expression: MaybeTypeCast<Expression>
546544
): Expression => isTypeCastExpression(expression)
547-
? followTypeAssertionChain(expression.expression)
548-
: expression
545+
? followTypeAssertionChain(expression.expression)
546+
: expression

src/utils/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export enum HookName {
2323
}
2424

2525
export enum ModifierName {
26+
to = 'to',
27+
have = 'have',
2628
not = 'not',
2729
rejects = 'rejects',
2830
resolves = 'resolves',

tests/valid-expect.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ ruleTester.run(RULE_NAME, rule, {
8686
'expect(value, "message").toBe(1);',
8787
'expect(value, `message`).toBe(1);',
8888
'const message = "message"; expect(value, `${message}`).toBe(1);',
89+
`it('example', () => {
90+
expect("foo bar").to.include("foo");
91+
});
92+
`,
93+
`it('example', () => {
94+
expect("hey").to.have.property("foo", "bar")
95+
});
96+
`,
8997
{
9098
code: 'expect(1).toBe(2);',
9199
options: [{ maxArgs: 2 }]

0 commit comments

Comments
 (0)