Skip to content

Commit 4903e39

Browse files
committed
fix(linter/no-standalone-expect): false positive in callback fn (#11940)
closes #11934
1 parent 47626e8 commit 4903e39

File tree

2 files changed

+31
-8
lines changed
  • crates/oxc_linter/src/rules/jest/no_standalone_expect

2 files changed

+31
-8
lines changed

crates/oxc_linter/src/rules/jest/no_standalone_expect/mod.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,25 @@ fn is_var_declarator_or_test_block<'a>(
215215
return true;
216216
}
217217
}
218-
AstKind::Argument(_) => {
219-
if let Some(parent) = ctx.nodes().parent_node(node.id()) {
220-
return is_var_declarator_or_test_block(
221-
parent,
222-
additional_test_block_functions,
223-
id_nodes_mapping,
224-
ctx,
225-
);
218+
AstKind::Argument(_) | AstKind::ArrayExpression(_) | AstKind::ObjectExpression(_) => {
219+
let mut current = node;
220+
while let Some(parent) = ctx.nodes().parent_node(current.id()) {
221+
match parent.kind() {
222+
AstKind::CallExpression(_) | AstKind::VariableDeclarator(_) => {
223+
return is_var_declarator_or_test_block(
224+
parent,
225+
additional_test_block_functions,
226+
id_nodes_mapping,
227+
ctx,
228+
);
229+
}
230+
AstKind::Argument(_)
231+
| AstKind::ArrayExpression(_)
232+
| AstKind::ObjectExpression(_) => {
233+
current = parent;
234+
}
235+
_ => break,
236+
}
226237
}
227238
}
228239
_ => {}

crates/oxc_linter/src/rules/jest/no_standalone_expect/tests/jest.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ fn test() {
6262
",
6363
Some(serde_json::json!([{ "additionalTestBlockFunctions": ["each.test"] }])),
6464
),
65+
(
66+
r"function funcWithCallback(callback) { callback(5); }
67+
describe('testWithCallback', () => {
68+
it('should call the callback', (done) => {
69+
funcWithCallback((result) => {
70+
expect(result).toBe(5);
71+
done();
72+
});
73+
});
74+
});",
75+
None,
76+
),
6577
];
6678

6779
let fail = vec![

0 commit comments

Comments
 (0)