Skip to content
Open
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
30 changes: 19 additions & 11 deletions crates/emmylua_code_analysis/src/check/checker/duplicate_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use std::collections::HashMap;

use emmylua_parser::{
LuaAssignStat, LuaAstNode, LuaClosureExpr, LuaSyntaxId, LuaSyntaxNode, LuaVarExpr,
LuaAssignStat, LuaAstNode, LuaClosureExpr, LuaSyntaxId, LuaSyntaxNode, LuaSyntaxTree,
LuaVarExpr,
};

use crate::DiagnosticCode;
Expand Down Expand Up @@ -87,9 +88,9 @@ impl Checker for DuplicateFieldChecker {
if dupes.len() <= 1 {
continue;
}
let all_function = dupes
.iter()
.all(|member| is_function_like(semantic_model, member));
let all_function = dupes.iter().all(|member| {
is_function_like(semantic_model, semantic_model.syntax_tree(), member)
});
// `@field a fun()` may be declared repeatedly (function overloads);
// ordinary repeated non-function field writes are also intentional
// (for example a global config table assigning the same key twice).
Expand Down Expand Up @@ -152,23 +153,25 @@ fn check_cross_file_member_assign(
let Some(module_facts) = semantic_model.file_facts_of(module_file) else {
continue;
};
// A field set to `nil` in the module (`{ to = nil }`) holds no function to reimplement.
let module_tree = semantic_model.syntax_tree_of(module_file);
let same_path = resolved.receiver.as_ref().is_some_and(|receiver| {
semantic_model
.members_of_receiver_key(receiver, &resolved.key)
.iter()
.any(|member| {
member.file_id == module_file
&& module_facts
.member_by_id(&member.id)
.is_some_and(|member| member.value_syntax.is_some())
&& module_facts.member_by_id(&member.id).is_some_and(|member| {
is_function_like(semantic_model, module_tree, member)
})
})
});
let original_definition = resolved.file_id == Some(module_file)
&& resolved
.member_id
.as_ref()
.and_then(|id| module_facts.member_by_id(id))
.is_some_and(|member| member.value_syntax.is_some());
.is_some_and(|member| is_function_like(semantic_model, module_tree, member));
if !same_path && !original_definition {
continue;
}
Expand Down Expand Up @@ -219,10 +222,15 @@ fn required_module_file(
semantic_model.module_file_of(&module_name)
}

/// Whether a member resolves to a function or has a closure value.
fn is_function_like(semantic_model: &SemanticModel<'_>, member: &Member) -> bool {
/// Whether a member resolves to a function or has a closure value. `tree` is the syntax tree of
/// the file declaring `member`.
fn is_function_like(
semantic_model: &SemanticModel<'_>,
tree: Option<&LuaSyntaxTree>,
member: &Member,
) -> bool {
if let Some(value_syntax) = member.value_syntax
&& let Some(tree) = semantic_model.syntax_tree()
&& let Some(tree) = tree
&& let Some(node) = value_syntax.to_node_from_root(&tree.get_red_root())
&& LuaClosureExpr::cast(node).is_some()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,47 @@ mod test {
"{diagnostics:?}"
);
}

#[test]
fn test_same_key_on_different_nested_tables_is_not_duplicate() {
for (source, count) in [
(
"local M = { A = {}, B = {} }\nfunction M.A:init() end\nfunction M.B:init() end\n",
0,
),
(
"local M = { a = {}, b = {} }\nM.a.to = function() end\nM.b.to = function() end\n",
0,
),
// The same nested table twice still is a duplicate.
(
"local M = { A = {} }\nfunction M.A:init() end\nfunction M.A:init() end\n",
2,
),
] {
let diagnostics = check_source(source);
assert_eq!(
count_by_code(&diagnostics, DiagnosticCode::DuplicateSetField),
count,
"{source}: {diagnostics:?}"
);
}
}

#[test]
fn test_setting_a_nil_field_of_a_required_module_is_not_duplicate() {
let mut ws = VirtualWorkspace::new();
ws.def_file(
"reg.lua",
"local M = { a = { to = nil }, b = { to = nil } }\nreturn M\n",
);
assert!(ws.has_no_diagnostic(
DiagnosticCode::DuplicateSetField,
r#"
local reg = require("reg")
reg.a.to = function() end
reg.b.to = function() end
"#
));
}
}
Loading