Ignore comments in Tree-sitter logic graphs
This commit is contained in:
parent
9161889492
commit
6d659ba381
2 changed files with 46 additions and 0 deletions
|
|
@ -20,6 +20,8 @@ from tree_sitter import Language, Node, Parser
|
||||||
from .errors import DocForgeError
|
from .errors import DocForgeError
|
||||||
from .models import LogicEdge, LogicNode, LogicProjection
|
from .models import LogicEdge, LogicNode, LogicProjection
|
||||||
|
|
||||||
|
_TRIVIA_NODE_TYPES = frozenset({"comment"})
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class TreeSitterLogicOwner:
|
class TreeSitterLogicOwner:
|
||||||
|
|
@ -432,6 +434,8 @@ class _TreeSitterFunctionBuilder:
|
||||||
*,
|
*,
|
||||||
control: _Control | None,
|
control: _Control | None,
|
||||||
) -> tuple[_Tail, ...]:
|
) -> tuple[_Tail, ...]:
|
||||||
|
if statement.type in _TRIVIA_NODE_TYPES:
|
||||||
|
return incoming
|
||||||
if statement.type in self.profile.block_types:
|
if statement.type in self.profile.block_types:
|
||||||
return self._statements(statement.named_children, incoming, control=control)
|
return self._statements(statement.named_children, incoming, control=control)
|
||||||
if statement.type == "if_statement":
|
if statement.type == "if_statement":
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,27 @@ from docforge.treesitter_logic import (
|
||||||
|
|
||||||
|
|
||||||
class JavaScriptLogicTests(unittest.TestCase):
|
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:
|
def test_branches_short_circuit_and_converge(self) -> None:
|
||||||
source = """
|
source = """
|
||||||
function choose(enabled, ready) {
|
function choose(enabled, ready) {
|
||||||
|
|
@ -90,6 +111,27 @@ function process(items, mode) {
|
||||||
|
|
||||||
|
|
||||||
class CppLogicTests(unittest.TestCase):
|
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:
|
def test_cpp_function_branches_and_throws(self) -> None:
|
||||||
source = """
|
source = """
|
||||||
int choose(bool enabled) {
|
int choose(bool enabled) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue