Skip to content

Commit

Permalink
fix: Only consider argument types for matching (#33)
Browse files Browse the repository at this point in the history
In the initial version of this rule, the name of the arguments were also
taken into account when calculating matching score for the signature of
the method. This is not precise and may lead into incorrect results
because argument names are not part of a method's signature, only the
argument types. So for instance: `void cadastrar(Pessoa umaPessoa)` and
`void cadastrar(Pessoa pessoa)` should match regardless of the Pessoa
argument name
  • Loading branch information
jpedroh authored Feb 27, 2024
1 parent 75de1d9 commit 100c5f1
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions matching_handlers/src/java/method_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,39 @@ pub fn compute_matching_score_for_method_declaration<'a>(
find_child_of_kind(children_right, "identifier").map(|node| node.contents());

// We also need to take method arguments into account because of overloading
let arguments_left =
find_child_of_kind(children_left, "formal_parameters").map(|node| node.contents());
let arguments_right =
find_child_of_kind(children_right, "formal_parameters").map(|node| node.contents());
let type_of_left_arguments = find_child_of_kind(children_left, "formal_parameters")
.map(|node| extract_argument_types_from_formal_parameters(node));
let type_of_right_arguments = find_child_of_kind(children_right, "formal_parameters")
.map(|node| extract_argument_types_from_formal_parameters(node));

let identifiers_are_equal =
identifier_left.is_some() && identifier_left == identifier_right;
let arguments_are_equal = arguments_left.is_some() && arguments_left == arguments_right;
let arguments_are_equal = type_of_left_arguments.is_some()
&& type_of_left_arguments == type_of_right_arguments;

(identifiers_are_equal && arguments_are_equal).into()
}
(_, _) => 0,
}
}

fn extract_argument_types_from_formal_parameters(node: &CSTNode) -> Vec<String> {
match node {
CSTNode::Terminal(_) => vec![],
CSTNode::NonTerminal(non_terminal) => non_terminal
.children
.iter()
.filter(|inner_node| inner_node.kind() == "formal_parameter")
.filter_map(|inner_node| match inner_node {
CSTNode::Terminal(_) => None,
CSTNode::NonTerminal(non_terminal) => {
non_terminal.children.first().map(|v| v.contents())
}
})
.collect(),
}
}

#[cfg(test)]
mod tests {
use model::{
Expand Down Expand Up @@ -74,8 +93,10 @@ mod tests {
fn it_returns_one_if_methods_have_equal_identifiers_and_equal_parameters_list() {
let left =
make_method_declaration_node("an_identifier", make_method_parameter("String", "name"));
let right =
make_method_declaration_node("an_identifier", make_method_parameter("String", "name"));
let right = make_method_declaration_node(
"an_identifier",
make_method_parameter("String", "another_name"),
);
let matching_score = compute_matching_score_for_method_declaration(&left, &right);
assert_eq!(1, matching_score);
}
Expand Down

0 comments on commit 100c5f1

Please sign in to comment.