Skip to content

Commit 2d88675

Browse files
committed
fix #1110
1 parent 4f14925 commit 2d88675

2 files changed

Lines changed: 57 additions & 16 deletions

File tree

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

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use emmylua_parser::{LuaAstNode, LuaIndexMemberExpr, LuaTableExpr, LuaVarExpr};
44

55
use crate::{
66
DbIndex, InferFailReason, InferGuard, InferGuardRef, LuaDocParamInfo, LuaDocReturnInfo,
7-
LuaFunctionType, LuaInferCache, LuaSignature, LuaType, SignatureReturnStatus, TypeOps,
8-
get_real_type, infer_call_expr_func, infer_expr, infer_table_should_be,
7+
LuaFunctionType, LuaInferCache, LuaMemberId, LuaSignature, LuaType, SignatureReturnStatus,
8+
TypeOps, get_real_type, infer_call_expr_func, infer_expr, infer_table_should_be,
99
};
1010

1111
use super::{
@@ -205,7 +205,7 @@ pub fn try_resolve_closure_parent_params(
205205
if !signature.param_docs.is_empty() {
206206
return Ok(());
207207
}
208-
let self_type;
208+
let mut self_type = None;
209209
let member_type = match &closure_params.parent_ast {
210210
UnResolveParentAst::LuaFuncStat(func_stat) => {
211211
let func_name = func_stat.get_func_name().ok_or(InferFailReason::None)?;
@@ -227,19 +227,36 @@ pub fn try_resolve_closure_parent_params(
227227
}
228228
}
229229
UnResolveParentAst::LuaTableField(table_field) => {
230-
let parnet_table_expr = table_field
231-
.get_parent::<LuaTableExpr>()
232-
.ok_or(InferFailReason::None)?;
233-
let parent_table_type = infer_table_should_be(db, cache, parnet_table_expr)?;
234-
self_type = Some(parent_table_type.clone());
235-
find_best_function_type(
236-
db,
237-
cache,
238-
&parent_table_type,
239-
LuaIndexMemberExpr::TableField(table_field.clone()),
240-
signature,
241-
)
242-
.ok_or(InferFailReason::None)?
230+
let parent_member_type = if let Some(parent_table_expr) =
231+
table_field.get_parent::<LuaTableExpr>()
232+
{
233+
if let Ok(parent_table_type) = infer_table_should_be(db, cache, parent_table_expr) {
234+
self_type = Some(parent_table_type.clone());
235+
find_best_function_type(
236+
db,
237+
cache,
238+
&parent_table_type,
239+
LuaIndexMemberExpr::TableField(table_field.clone()),
240+
signature,
241+
)
242+
} else {
243+
None
244+
}
245+
} else {
246+
None
247+
};
248+
249+
if let Some(parent_member_type) = parent_member_type {
250+
parent_member_type
251+
} else {
252+
let member_id =
253+
LuaMemberId::new(table_field.get_syntax_id(), closure_params.file_id);
254+
db.get_type_index()
255+
.get_type_cache(&member_id.into())
256+
.filter(|type_cache| type_cache.is_doc())
257+
.map(|type_cache| type_cache.as_type().clone())
258+
.ok_or(InferFailReason::None)?
259+
}
243260
}
244261
UnResolveParentAst::LuaAssignStat(assign) => {
245262
let (vars, exprs) = assign.get_var_and_expr_list();

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,4 +514,28 @@ mod test {
514514
let expected = ws.ty("integer");
515515
assert_eq!(ty, expected);
516516
}
517+
518+
#[test]
519+
fn test_table_field_doc_type_alias_closure_param() {
520+
let mut ws = VirtualWorkspace::new();
521+
ws.def(
522+
r#"
523+
---@alias test fun(a:int,b:string)
524+
525+
---@type test
526+
local test
527+
528+
local t = {
529+
---@type test
530+
A = function(a, b)
531+
ParamA = a
532+
ParamB = b
533+
end
534+
}
535+
"#,
536+
);
537+
538+
assert_eq!(ws.expr_ty("ParamA"), ws.ty("integer"));
539+
assert_eq!(ws.expr_ty("ParamB"), ws.ty("string"));
540+
}
517541
}

0 commit comments

Comments
 (0)