1
0
Fork 0
Code Issues Pull requests Projects Releases 2 Packages Wiki Activity Actions Pages

Ignore comments in Tree-sitter logic graphs

This commit is contained in:
Andraxion 2026-07-25 22:46:01 -04:00
parent 9161889492
commit 6d659ba381
2 changed files with 46 additions and 0 deletions

View file

@ -20,6 +20,8 @@ from tree_sitter import Language, Node, Parser
from .errors import DocForgeError
from .models import LogicEdge, LogicNode, LogicProjection
_TRIVIA_NODE_TYPES = frozenset({"comment"})
@dataclass(frozen=True)
class TreeSitterLogicOwner:
@ -432,6 +434,8 @@ class _TreeSitterFunctionBuilder:
*,
control: _Control | None,
) -> tuple[_Tail, ...]:
if statement.type in _TRIVIA_NODE_TYPES:
return incoming
if statement.type in self.profile.block_types:
return self._statements(statement.named_children, incoming, control=control)
if statement.type == "if_statement":

View file

@ -11,6 +11,27 @@ from docforge.treesitter_logic import (
class JavaScriptLogicTests(unittest.TestCase):
def test_comments_do_not_become_logic_actions(self) -> None:
source = """
function choose(enabled) {
// Explain the condition.
// Continue the explanation.
/* A block comment is trivia too. */
if (enabled) {
accept();
}
}
""".strip()
projection = analyze_javascript_source(
source,
source_id="source.javascript",
owners=(TreeSitterLogicOwner("js.symbol.choose", "choose", 1),),
)[0]
labels = {node.label for node in projection.nodes}
self.assertFalse(any(label.startswith(("//", "/*")) for label in labels))
self.assertIn("accept();", labels)
def test_branches_short_circuit_and_converge(self) -> None:
source = """
function choose(enabled, ready) {
@ -90,6 +111,27 @@ function process(items, mode) {
class CppLogicTests(unittest.TestCase):
def test_comments_do_not_become_logic_actions(self) -> None:
source = """
int choose(bool enabled) {
// Explain the condition.
/* A block comment is trivia too. */
if (enabled) {
return 1;
}
return 0;
}
""".strip()
projection = analyze_cpp_source(
source,
source_id="source.cpp",
owners=(TreeSitterLogicOwner("cpp.symbol.choose", "choose", 1),),
)[0]
labels = {node.label for node in projection.nodes}
self.assertFalse(any(label.startswith(("//", "/*")) for label in labels))
self.assertIn("return 1", labels)
def test_cpp_function_branches_and_throws(self) -> None:
source = """
int choose(bool enabled) {