Skip to content

Commit

Permalink
fix: fix two classes issues
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Oct 12, 2022
1 parent 3119f9f commit 768c980
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions fkl_cli/src/construct/java_construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl JavaConstruct {
let mut code_file = CodeFile::default();
let mut is_last_node = false;

let mut class = CodeClass::default();
let mut last_class = CodeClass::default();

let capture_names = ident.query.capture_names();
let mut last_type = "void".to_string();
Expand All @@ -88,27 +88,26 @@ impl JavaConstruct {
code_file.imports.push(text.to_string());
}
"class-name" => {
if !class.name.is_empty() {
code_file.classes.push(class.clone());
class = CodeClass::default();
if !last_class.name.is_empty() {
last_class = CodeClass::default();
}

class.name = text.to_string();
class.package = code_file.package.clone();
last_class.name = text.to_string();
last_class.package = code_file.package.clone();

let class_node = capture.node.parent().unwrap();
JavaConstruct::insert_location(&mut class, class_node);
JavaConstruct::insert_location(&mut last_class, class_node);
if !is_last_node {
is_last_node = true;
}
}
"impl-name" => {
class.implements.push(text.to_string());
last_class.implements.push(text.to_string());
}
"function-name" => {
let mut function = JavaConstruct::insert_function(capture, text);
function.return_type = last_type.clone();
class.functions.push(function);
last_class.functions.push(function);
}
"return-type" => {
last_type = text.to_string();
Expand All @@ -127,7 +126,7 @@ impl JavaConstruct {
}

if is_last_node {
code_file.classes.push(class.clone());
code_file.classes.push(last_class.clone());
}

code_file
Expand Down Expand Up @@ -180,14 +179,10 @@ import payroll.Employee;
fn should_parse_multiple_java_class() {
let source_code = "class DateTimeImpl {
}
class DateTimeImpl2 {
}
";
let file = JavaConstruct::parse(source_code);
assert_eq!(2, file.classes.len());
assert_eq!(1, file.classes.len());
assert_eq!("DateTimeImpl", file.classes[0].name);
assert_eq!("DateTimeImpl2", file.classes[1].name);
}

#[test]
Expand Down Expand Up @@ -229,7 +224,7 @@ class DateTimeImpl2 {
implements: vec![],
functions: vec![CodeFunction {
name: "getDate".to_string(),
return_type: "".to_string(),
return_type: "Date".to_string(),
variable: vec![],
start: CodePoint { row: 1, column: 4 },
end: CodePoint { row: 3, column: 5 },
Expand Down Expand Up @@ -272,4 +267,24 @@ public class HelloController {
end: CodePoint { row: 8, column: 1 },
});
}

#[test]
fn two_methods() {
let source_code = r#"@RestController
public class HelloController {
@GetMapping("/")
public String index() { return "Greetings from Spring Boot!"; }
@GetMapping("/")
public String second() { return "Greetings from Spring Boot!"; }
}
"#;

let file = JavaConstruct::parse(source_code);

assert_eq!(file.classes.len(), 1);
let first_class = &file.classes[0];
assert!(first_class.is_contain_method("index"));
assert!(first_class.is_contain_method("second"));
}
}

0 comments on commit 768c980

Please sign in to comment.