Skip to content

Commit f5dac8c

Browse files
authored
Merge pull request #102 from StefanZoerner/fix/leaf-node-identifier-filter
Keep identifiers like handleInvalidInput in filter_leaf_nodes
2 parents bb4ded0 + a849a98 commit f5dac8c

3 files changed

Lines changed: 96 additions & 13 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ tests/*
4343
!tests/test_module_tree_validation.py
4444
!tests/test_ruby_analyzer.py
4545
!tests/test_processing_order_update.py
46+
!tests/test_leaf_selection.py
4647

4748
# Jupyter
4849
*.ipynb

‎codewiki/src/be/dependency_analyzer/leaf_selection.py‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
from typing import Dict, List, Set
1+
import logging
22

33
from codewiki.src.be.dependency_analyzer.models.core import Node
44

5-
import logging
65
logger = logging.getLogger(__name__)
76

87
# Below this many class/interface/struct components, a repo is considered
@@ -17,7 +16,7 @@
1716
OOP_TYPES = {"class", "interface", "struct"}
1817

1918

20-
def compute_valid_leaf_types(components: Dict[str, Node]) -> Set[str]:
19+
def compute_valid_leaf_types(components: dict[str, Node]) -> set[str]:
2120
"""
2221
Determine which component types qualify as leaf nodes.
2322
@@ -54,19 +53,23 @@ def compute_valid_leaf_types(components: Dict[str, Node]) -> Set[str]:
5453

5554
def filter_leaf_nodes(
5655
leaf_nodes,
57-
components: Dict[str, Node],
58-
valid_types: Set[str],
59-
) -> List[str]:
60-
"""Keep leaf nodes that are known components of a valid type."""
56+
components: dict[str, Node],
57+
valid_types: set[str],
58+
) -> list[str]:
59+
"""Keep leaf nodes that are known components of a valid type.
60+
61+
Anything that is not a known component id (None, empty strings, error
62+
messages that occasionally reach leaf-node selection) is dropped by the
63+
membership check alone. Do not add keyword-based filtering on top of it:
64+
it would also reject identifiers such as `handleInvalidInput` or `ErrorLog`.
65+
"""
6166
keep_leaf_nodes = []
6267
for leaf_node in leaf_nodes:
63-
# Skip any leaf nodes that are clearly error strings or invalid identifiers
64-
if not isinstance(leaf_node, str) or leaf_node.strip() == "" or any(err_keyword in leaf_node.lower() for err_keyword in ['error', 'exception', 'failed', 'invalid']):
65-
logger.debug(f"Skipping invalid leaf node identifier: '{leaf_node}'")
68+
if not isinstance(leaf_node, str) or leaf_node not in components:
69+
logger.debug(f"Skipping unknown leaf node identifier: '{leaf_node}'")
6670
continue
6771

68-
if leaf_node in components:
69-
if components[leaf_node].component_type in valid_types:
70-
keep_leaf_nodes.append(leaf_node)
72+
if components[leaf_node].component_type in valid_types:
73+
keep_leaf_nodes.append(leaf_node)
7174

7275
return keep_leaf_nodes

‎tests/test_leaf_selection.py‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""Tests for leaf-node identifier filtering.
2+
3+
Covers filter_leaf_nodes: identifiers that merely contain a word like
4+
"invalid" must survive, while strings that are not known component ids
5+
(error messages reaching leaf-node selection, malformed entries) are dropped.
6+
"""
7+
8+
from __future__ import annotations
9+
10+
from codewiki.src.be.dependency_analyzer.leaf_selection import filter_leaf_nodes
11+
from codewiki.src.be.dependency_analyzer.models.core import Node
12+
13+
14+
def _component(component_id: str, component_type: str = "function") -> Node:
15+
file_path = component_id.split("::")[0]
16+
return Node(
17+
id=component_id,
18+
name=component_id.split("::")[-1],
19+
component_type=component_type,
20+
file_path=file_path,
21+
relative_path=file_path,
22+
)
23+
24+
25+
def _components(*component_ids: str) -> dict[str, Node]:
26+
return {cid: _component(cid) for cid in component_ids}
27+
28+
29+
def test_identifiers_containing_error_words_are_kept() -> None:
30+
components = _components(
31+
"src/input.cpp::handleInvalidInput",
32+
"src/game.cpp::gameFailedCheck",
33+
"src/log.cpp::ErrorLog",
34+
"src/parser.cpp::parseExceptionTable",
35+
)
36+
37+
kept = filter_leaf_nodes(list(components), components, {"function"})
38+
39+
assert set(kept) == set(components)
40+
41+
42+
def test_error_messages_are_dropped() -> None:
43+
components = _components("src/player.cpp::playerMove")
44+
candidates = [
45+
"src/player.cpp::playerMove",
46+
"Error: could not parse file",
47+
"invalid syntax at line 3",
48+
"Analysis failed for this component",
49+
]
50+
51+
kept = filter_leaf_nodes(candidates, components, {"function"})
52+
53+
assert kept == ["src/player.cpp::playerMove"]
54+
55+
56+
def test_unknown_and_malformed_entries_are_dropped() -> None:
57+
components = _components("src/player.cpp::playerMove")
58+
candidates = [
59+
"src/player.cpp::playerMove",
60+
"src/player.cpp::doesNotExist",
61+
"",
62+
" ",
63+
None,
64+
]
65+
66+
kept = filter_leaf_nodes(candidates, components, {"function"})
67+
68+
assert kept == ["src/player.cpp::playerMove"]
69+
70+
71+
def test_components_of_other_types_are_dropped() -> None:
72+
components = {
73+
"src/game.cpp::runGame": _component("src/game.cpp::runGame", "function"),
74+
"src/game.h::GameState": _component("src/game.h::GameState", "struct"),
75+
}
76+
77+
kept = filter_leaf_nodes(list(components), components, {"function"})
78+
79+
assert kept == ["src/game.cpp::runGame"]

0 commit comments

Comments
 (0)