Skip to content

Commit 76835d1

Browse files
committed
fix closure param infer
1 parent 8c5624a commit 76835d1

File tree

4 files changed

+63
-22
lines changed

4 files changed

+63
-22
lines changed

crates/emmylua_code_analysis/src/compilation/analyzer/unresolve/resolve_closure_param.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,46 @@ pub fn try_resolve_closure_params(
1717
let call_doc_func = infer_call_expr_func(
1818
db,
1919
config,
20-
call_expr,
20+
call_expr.clone(),
2121
call_expr_type,
2222
&mut InferGuard::new(),
2323
None,
2424
)?;
2525

26-
let expr_closure_params =
27-
if let Some(param_type) = call_doc_func.get_params().get(closure_params.param_idx) {
28-
if let Some(LuaType::DocFunction(func)) = &param_type.1 {
29-
if func.is_async() {
30-
let file_id = closure_params.file_id;
31-
let property_owner = LuaPropertyOwnerId::Signature(closure_params.signature_id);
32-
db.get_property_index_mut()
33-
.add_async(file_id, property_owner);
34-
}
26+
let colon_call = call_expr.is_colon_call();
27+
let colon_define = call_doc_func.is_colon_define();
3528

36-
func.get_params()
37-
} else {
29+
let mut param_idx = closure_params.param_idx;
30+
match (colon_call, colon_define) {
31+
(true, false) => {
32+
param_idx += 1;
33+
}
34+
(false, true) => {
35+
if param_idx == 0 {
3836
return Some(true);
3937
}
38+
39+
param_idx -= 1;
40+
}
41+
_ => {}
42+
}
43+
44+
let expr_closure_params = if let Some(param_type) = call_doc_func.get_params().get(param_idx) {
45+
if let Some(LuaType::DocFunction(func)) = &param_type.1 {
46+
if func.is_async() {
47+
let file_id = closure_params.file_id;
48+
let property_owner = LuaPropertyOwnerId::Signature(closure_params.signature_id);
49+
db.get_property_index_mut()
50+
.add_async(file_id, property_owner);
51+
}
52+
53+
func.get_params()
4054
} else {
4155
return Some(true);
42-
};
56+
}
57+
} else {
58+
return Some(true);
59+
};
4360

4461
let signature = db
4562
.get_signature_index_mut()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#[cfg(test)]
2+
mod test {
3+
use crate::VirtualWorkspace;
4+
5+
#[test]
6+
fn test_closure_param_infer() {
7+
let mut ws = VirtualWorkspace::new();
8+
9+
ws.def(
10+
r#"
11+
12+
---@class EventData
13+
---@field name string
14+
15+
---@class EventDispatcher
16+
---@field pre fun(self:EventDispatcher,callback:fun(context:EventData))
17+
local EventDispatcher = {}
18+
19+
EventDispatcher:pre(function(context)
20+
b = context
21+
end)
22+
"#,
23+
);
24+
25+
let ty = ws.expr_ty("b");
26+
let expected = ws.ty("EventData");
27+
assert_eq!(ty, expected);
28+
}
29+
}

crates/emmylua_code_analysis/src/compilation/test/closure_return_test.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ mod test {
66
fn test_flow() {
77
let mut ws = VirtualWorkspace::new_with_init_std_lib();
88

9-
ws.def(
10-
r#"
11-
12-
"#,
13-
);
14-
159
ws.def(
1610
r#"
1711
--- @return string[] stdout

crates/emmylua_code_analysis/src/compilation/test/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
mod out_of_order;
2-
mod multi_return;
1+
mod closure_param_infer_test;
32
mod closure_return_test;
3+
mod flow;
4+
mod multi_return;
5+
mod out_of_order;
46
mod overload_field;
5-
mod flow;

0 commit comments

Comments
 (0)