Skip to content

Commit 874e8fd

Browse files
committed
fix(native): compute complexity and CFG for prototype method definitions
emit_js_prototype_method and the method_definition arm of extract_js_prototype_object_literal both emitted complexity: None and cfg: None. Call compute_all_metrics and build_function_cfg on the function node, matching the pattern used by handle_function_decl, handle_method_def, and handle_var_decl.
1 parent 0c26030 commit 874e8fd

1 file changed

Lines changed: 41 additions & 4 deletions

File tree

crates/codegraph-core/src/extractors/javascript.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ fn emit_js_prototype_method(class_name: &str, method_name: &str, rhs: &Node, sou
368368
line: start_line(rhs),
369369
end_line: Some(end_line(rhs)),
370370
decorators: None,
371-
complexity: None,
372-
cfg: None,
371+
complexity: compute_all_metrics(rhs, source, "javascript"),
372+
cfg: build_function_cfg(rhs, "javascript", source),
373373
children: None,
374374
});
375375
}
@@ -398,8 +398,8 @@ fn extract_js_prototype_object_literal(class_name: &str, obj_node: &Node, source
398398
line: start_line(&child),
399399
end_line: Some(end_line(&child)),
400400
decorators: None,
401-
complexity: None,
402-
cfg: None,
401+
complexity: compute_all_metrics(&child, source, "javascript"),
402+
cfg: build_function_cfg(&child, "javascript", source),
403403
children: None,
404404
});
405405
}
@@ -2504,4 +2504,41 @@ mod tests {
25042504
let def = s.definitions.iter().find(|d| d.name.contains("Array"));
25052505
assert!(def.is_none(), "built-in prototype assignment should be ignored; got: {:?}", def);
25062506
}
2507+
2508+
#[test]
2509+
fn prototype_direct_method_has_complexity_and_cfg() {
2510+
let s = parse_js(
2511+
"function C() {}\n\
2512+
C.prototype.foo = function() { if (true) { return 1; } return 0; };",
2513+
);
2514+
let def = s.definitions.iter().find(|d| d.name == "C.foo").expect("C.foo missing");
2515+
assert!(def.complexity.is_some(), "C.foo should have complexity metrics");
2516+
assert!(def.cfg.is_some(), "C.foo should have CFG data");
2517+
}
2518+
2519+
#[test]
2520+
fn prototype_object_literal_method_definition_has_complexity_and_cfg() {
2521+
let s = parse_js(
2522+
"function C() {}\n\
2523+
C.prototype = {\n\
2524+
greet() { if (true) { return 'hi'; } return ''; },\n\
2525+
};",
2526+
);
2527+
let def = s.definitions.iter().find(|d| d.name == "C.greet").expect("C.greet missing");
2528+
assert!(def.complexity.is_some(), "C.greet should have complexity metrics");
2529+
assert!(def.cfg.is_some(), "C.greet should have CFG data");
2530+
}
2531+
2532+
#[test]
2533+
fn prototype_object_literal_pair_fn_has_complexity_and_cfg() {
2534+
let s = parse_js(
2535+
"function C() {}\n\
2536+
C.prototype = {\n\
2537+
bar: function() { if (true) { return 1; } return 0; },\n\
2538+
};",
2539+
);
2540+
let def = s.definitions.iter().find(|d| d.name == "C.bar").expect("C.bar missing");
2541+
assert!(def.complexity.is_some(), "C.bar should have complexity metrics");
2542+
assert!(def.cfg.is_some(), "C.bar should have CFG data");
2543+
}
25072544
}

0 commit comments

Comments
 (0)