Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions crates/emmylua_code_analysis/src/db_index/declaration/decl_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,17 @@ impl LuaDeclarationTree {
{
match scope.get_kind() {
LuaScopeKind::LocalOrAssignStat => {
let parent = scope.get_parent();
if let Some(parent) = parent {
let parent_scope = match self.scopes.get(parent.id as usize) {
Some(scope) => scope,
None => return,
};
self.walk_up(parent_scope, scope.get_position(), level, f);
if level == 0 {
let parent = scope.get_parent();
if let Some(parent) = parent {
let parent_scope = match self.scopes.get(parent.id as usize) {
Some(scope) => scope,
None => return,
};
self.walk_up(parent_scope, scope.get_position(), level, f);
}
} else {
self.base_walk_up(scope, start_pos, level, f);
}
}
LuaScopeKind::Repeat => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ mod tests {
let mut ws = VirtualWorkspace::new();
ws.enable_full_diagnostic();

// TODO: closures are not detected as functions in the semantic model
// assert!(!ws.check_code_for(
// DiagnosticCode::MissingGlobalDoc,
// r#"
// local c = function(x, y)
// return x + y
// end
// "#
// ));
assert!(!ws.check_code_for(
DiagnosticCode::IncompleteSignatureDoc,
r#"
local c = function(x, y)
return x + y
end
"#
));

assert!(!ws.check_code_for(
DiagnosticCode::IncompleteSignatureDoc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,20 @@ mod test {
"#
));
}

#[test]
fn test_globals() {
let mut ws = VirtualWorkspace::new_with_init_std_lib();
assert!(ws.check_code_for(
DiagnosticCode::UndefinedGlobal,
r#"
local fact = function(n)
if n == 0 then
return 1
end
return n * fact(n - 1)
end
"#
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,26 @@ fn infer_closure_expr_semantic_decl(
level,
)
}
LuaStat::LocalStat(local_stat) => {
let local_name =
local_stat.get_local_name_by_value(LuaExpr::ClosureExpr(closure_expr))?;
let name_token = local_name.get_name_token()?;
infer_token_semantic_decl(db, cache, name_token.syntax().clone(), level)
}
LuaStat::AssignStat(assign_stat) => {
let (vars, exprs) = assign_stat.get_var_and_expr_list();
let idx = exprs.iter().position(|expr| {
matches!(expr, LuaExpr::ClosureExpr(ce) if ce.syntax() == closure_expr.syntax())
})?;
let var = vars.get(idx)?;
infer_expr_semantic_decl(
db,
cache,
var.clone().into(),
semantic_guard.next_level()?,
level,
)
}
_ => None,
}
}
Expand Down