codegraphx 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. codegraph/__init__.py +37 -0
  2. codegraph/cli.py +106 -0
  3. codegraph/commands/__init__.py +1 -0
  4. codegraph/commands/conflicts.py +154 -0
  5. codegraph/commands/diff.py +189 -0
  6. codegraph/commands/export.py +141 -0
  7. codegraph/commands/graph.py +200 -0
  8. codegraph/commands/index.py +924 -0
  9. codegraph/commands/serve.py +62 -0
  10. codegraph/commands/why.py +645 -0
  11. codegraph/decisions/__init__.py +18 -0
  12. codegraph/decisions/conflicts.py +238 -0
  13. codegraph/decisions/extractor.py +698 -0
  14. codegraph/decisions/llm_extractor.py +161 -0
  15. codegraph/decisions/sources.py +298 -0
  16. codegraph/git/__init__.py +5 -0
  17. codegraph/git/diff.py +115 -0
  18. codegraph/git/history.py +156 -0
  19. codegraph/git/incremental.py +75 -0
  20. codegraph/git/refs.py +70 -0
  21. codegraph/models.py +211 -0
  22. codegraph/parser/__init__.py +1 -0
  23. codegraph/parser/base.py +164 -0
  24. codegraph/parser/common.py +147 -0
  25. codegraph/parser/parallel.py +96 -0
  26. codegraph/parser/registry.py +83 -0
  27. codegraph/parser/treesitter_cpp.py +102 -0
  28. codegraph/parser/treesitter_go.py +113 -0
  29. codegraph/parser/treesitter_java.py +79 -0
  30. codegraph/parser/treesitter_js.py +15 -0
  31. codegraph/parser/treesitter_py.py +163 -0
  32. codegraph/parser/treesitter_rust.py +120 -0
  33. codegraph/parser/treesitter_ts.py +178 -0
  34. codegraph/storage.py +767 -0
  35. codegraph/web/__init__.py +1 -0
  36. codegraph/web/app.py +271 -0
  37. codegraph/web/static/app.js +356 -0
  38. codegraph/web/static/index.html +59 -0
  39. codegraph/web/static/styles.css +92 -0
  40. codegraphx-0.1.0.dist-info/METADATA +262 -0
  41. codegraphx-0.1.0.dist-info/RECORD +44 -0
  42. codegraphx-0.1.0.dist-info/WHEEL +4 -0
  43. codegraphx-0.1.0.dist-info/entry_points.txt +2 -0
  44. codegraphx-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,147 @@
1
+ """Shared helpers for tree-sitter language parsers."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from datetime import datetime
6
+ from pathlib import Path
7
+
8
+ from tree_sitter import Node
9
+
10
+ from codegraph.models import CodeNode, make_uid, normalize_location_path
11
+
12
+
13
+ def rel_path(path: Path) -> str:
14
+ """Return a repo-relative forward-slash path."""
15
+ try:
16
+ return normalize_location_path(str(path.resolve().relative_to(Path.cwd())))
17
+ except ValueError:
18
+ return normalize_location_path(str(path))
19
+
20
+
21
+ def decl_name(node: Node, raw: bytes) -> str | None:
22
+ """Extract the name field from a declaration node."""
23
+ name_node = node.child_by_field_name("name")
24
+ if name_node is None:
25
+ return None
26
+ return _text(name_node, raw)
27
+
28
+
29
+ def _text(node: Node, raw: bytes) -> str:
30
+ """Decode the source slice covered by an AST node."""
31
+ return raw[node.start_byte : node.end_byte].decode("utf-8", errors="replace")
32
+
33
+
34
+ # Identifiers that can hold a declaration's name in the grammars we support.
35
+ _NAME_TYPES = frozenset(
36
+ {
37
+ "identifier",
38
+ "field_identifier",
39
+ "type_identifier",
40
+ "namespace_identifier",
41
+ "operator_name",
42
+ "destructor_name",
43
+ }
44
+ )
45
+
46
+ _FUNCTION_DECLARATOR_TYPES = frozenset(
47
+ {"function_declarator", "function_definition", "method_declaration"}
48
+ )
49
+
50
+
51
+ def declarator_name(node: Node, raw: bytes, depth: int = 0) -> str | None:
52
+ """Resolve a C/C++ style declarator chain to the declared name.
53
+
54
+ `int Calculator::compute(int)` nests a qualified identifier inside a
55
+ function declarator, and an in-class method nests a `field_identifier`;
56
+ looking only for a `name` field misses both.
57
+ """
58
+ if depth > 8:
59
+ return None
60
+ name_node = node.child_by_field_name("name")
61
+ if name_node is not None:
62
+ return _text(name_node, raw)
63
+ for field in ("declarator", "type"):
64
+ child = node.child_by_field_name(field)
65
+ if child is not None:
66
+ found = declarator_name(child, raw, depth + 1)
67
+ if found:
68
+ return found
69
+ for child in node.children:
70
+ if child.type in _NAME_TYPES:
71
+ return _text(child, raw)
72
+ return None
73
+
74
+
75
+ def declares_a_function(node: Node) -> bool:
76
+ """True when a declaration's declarator chain declares a function.
77
+
78
+ Covers `int helper(int);` (a `declaration`), `int spin(int n);` inside a
79
+ class (a `field_declaration`) and rejects `int limit = 3;`.
80
+ """
81
+ current = node
82
+ for _ in range(8):
83
+ if current.type in _FUNCTION_DECLARATOR_TYPES:
84
+ return True
85
+ nxt = current.child_by_field_name("declarator")
86
+ if nxt is None:
87
+ return False
88
+ current = nxt
89
+ return False
90
+
91
+
92
+ def type_name(node: Node, raw: bytes, depth: int = 0) -> str | None:
93
+ """Return the bare type name from a type node, unwrapping pointers/generics.
94
+
95
+ Used for Go receivers (`*Calculator`) and Rust `impl` blocks.
96
+ """
97
+ if depth > 6:
98
+ return None
99
+ if node.type in _NAME_TYPES:
100
+ return _text(node, raw)
101
+ for field in ("type", "name"):
102
+ child = node.child_by_field_name(field)
103
+ if child is not None:
104
+ found = type_name(child, raw, depth + 1)
105
+ if found:
106
+ return found
107
+ for child in node.children:
108
+ if child.type == "type_identifier":
109
+ return _text(child, raw)
110
+ return None
111
+
112
+
113
+ def simple_callee(fn_node: Node, raw: bytes) -> str | None:
114
+ """Extract a simple callee name from a call function expression."""
115
+ if fn_node.type in {"identifier", "name", "field_identifier", "type_identifier"}:
116
+ return raw[fn_node.start_byte : fn_node.end_byte].decode("utf-8", errors="replace")
117
+ for field in ("property", "attribute", "field", "name"):
118
+ child = fn_node.child_by_field_name(field)
119
+ if child is not None:
120
+ return raw[child.start_byte : child.end_byte].decode("utf-8", errors="replace")
121
+ return None
122
+
123
+
124
+ def make_node(
125
+ node: Node,
126
+ rel: str,
127
+ kind: str,
128
+ name: str,
129
+ parent_uid: str | None,
130
+ language: str,
131
+ commit_sha: str = "",
132
+ valid_from: datetime | None = None,
133
+ ) -> CodeNode:
134
+ """Build a CodeNode from an AST node."""
135
+ return CodeNode(
136
+ uid=make_uid(rel, kind, name, node.start_point[0] + 1, commit_sha),
137
+ kind=kind,
138
+ name=name,
139
+ file_path=rel,
140
+ line_start=node.start_point[0] + 1,
141
+ line_end=node.end_point[0] + 1,
142
+ language=language,
143
+ parent_uid=parent_uid,
144
+ commit_sha=commit_sha,
145
+ valid_from=valid_from or datetime.fromtimestamp(0),
146
+ valid_to=None,
147
+ )
@@ -0,0 +1,96 @@
1
+ """Optional process-pool parsing for large working trees."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ from concurrent.futures import ProcessPoolExecutor, as_completed
7
+ from datetime import datetime
8
+ from pathlib import Path
9
+
10
+ from codegraph.models import CodeNode, Edge
11
+ from codegraph.parser.registry import get_parser_for_path, is_supported_source
12
+
13
+ logger = logging.getLogger(__name__)
14
+
15
+
16
+ def _parse_one(
17
+ args: tuple[str, str, str, str]
18
+ ) -> tuple[list[CodeNode], list[tuple[str, str, int, str]]]:
19
+ """Parse a single file in a worker process.
20
+
21
+ Args:
22
+ args: (rel_path, text, commit_sha, valid_from_iso)
23
+
24
+ Returns:
25
+ Nodes and call-site tuples for that file.
26
+ """
27
+ rel, text, commit_sha, valid_from_iso = args
28
+ parser = get_parser_for_path(Path(rel))
29
+ if parser is None:
30
+ return [], []
31
+ valid_from = datetime.fromisoformat(valid_from_iso) if valid_from_iso else None
32
+ nodes = parser.parse_text(rel, text, commit_sha=commit_sha, valid_from=valid_from)
33
+ return nodes, list(parser._calls)
34
+
35
+
36
+ def _edges_from(
37
+ nodes: list[CodeNode], calls: list[tuple[str, str, int, str]]
38
+ ) -> list[Edge]:
39
+ """Build hierarchy + call edges from aggregated parse results."""
40
+ helper = get_parser_for_path(Path("dummy.py"))
41
+ if helper is None:
42
+ return []
43
+ helper._calls = calls
44
+ return helper.extract_edges(nodes)
45
+
46
+
47
+ def parse_files_parallel(
48
+ items: list[tuple[str, str]],
49
+ *,
50
+ commit_sha: str = "",
51
+ valid_from: datetime | None = None,
52
+ jobs: int = 0,
53
+ ) -> tuple[list[CodeNode], list[Edge]]:
54
+ """Parse many (rel_path, text) pairs, optionally in parallel.
55
+
56
+ Args:
57
+ items: File payloads to parse.
58
+ commit_sha: Commit stamp for node uids.
59
+ valid_from: Validity start for produced nodes.
60
+ jobs: Worker count; 0 means CPU count - 1.
61
+
62
+ Returns:
63
+ Aggregated nodes and edges.
64
+ """
65
+ if not items:
66
+ return [], []
67
+ workers = jobs if jobs > 0 else max(1, (__import__("os").cpu_count() or 2) - 1)
68
+ vf = valid_from.isoformat() if valid_from else ""
69
+ payloads = [
70
+ (rel, text, commit_sha, vf) for rel, text in items if is_supported_source(Path(rel))
71
+ ]
72
+ if not payloads:
73
+ return [], []
74
+
75
+ nodes: list[CodeNode] = []
76
+ calls: list[tuple[str, str, int, str]] = []
77
+
78
+ if workers == 1 or len(payloads) < 4:
79
+ for payload in payloads:
80
+ n, c = _parse_one(payload)
81
+ nodes.extend(n)
82
+ calls.extend(c)
83
+ else:
84
+ payloads.sort(key=lambda p: len(p[1]), reverse=True)
85
+ with ProcessPoolExecutor(max_workers=workers) as pool:
86
+ futures = [pool.submit(_parse_one, payload) for payload in payloads]
87
+ for fut in as_completed(futures):
88
+ try:
89
+ n, c = fut.result()
90
+ except Exception as exc: # noqa: BLE001
91
+ logger.warning("Parallel parse failed: %s", exc)
92
+ continue
93
+ nodes.extend(n)
94
+ calls.extend(c)
95
+
96
+ return nodes, _edges_from(nodes, calls)
@@ -0,0 +1,83 @@
1
+ """Select a language parser from the file extension."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from pathlib import Path
6
+
7
+ from codegraph.parser.base import BaseParser
8
+ from codegraph.parser.treesitter_cpp import CppParser
9
+ from codegraph.parser.treesitter_go import GoParser
10
+ from codegraph.parser.treesitter_java import JavaParser
11
+ from codegraph.parser.treesitter_js import JavaScriptParser
12
+ from codegraph.parser.treesitter_py import PythonParser
13
+ from codegraph.parser.treesitter_rust import RustParser
14
+ from codegraph.parser.treesitter_ts import TypeScriptParser
15
+
16
+ TS_SUFFIXES = {".ts", ".tsx"}
17
+ JS_SUFFIXES = {".js", ".mjs", ".cjs"}
18
+ PY_SUFFIXES = {".py"}
19
+ JAVA_SUFFIXES = {".java"}
20
+ GO_SUFFIXES = {".go"}
21
+ RUST_SUFFIXES = {".rs"}
22
+ CPP_SUFFIXES = {".cpp", ".cc", ".cxx", ".hpp", ".hh", ".hxx", ".h"}
23
+ SUPPORTED_SUFFIXES = (
24
+ TS_SUFFIXES
25
+ | JS_SUFFIXES
26
+ | PY_SUFFIXES
27
+ | JAVA_SUFFIXES
28
+ | GO_SUFFIXES
29
+ | RUST_SUFFIXES
30
+ | CPP_SUFFIXES
31
+ )
32
+
33
+
34
+ def get_parser_for_path(path: Path) -> BaseParser | None:
35
+ """Return the parser for a path, or None when unsupported.
36
+
37
+ Args:
38
+ path: Source file path.
39
+
40
+ Returns:
41
+ A BaseParser instance, or None.
42
+ """
43
+ suffix = path.suffix.lower()
44
+ if suffix in JS_SUFFIXES:
45
+ return JavaScriptParser()
46
+ if suffix in TS_SUFFIXES:
47
+ return TypeScriptParser()
48
+ if suffix in PY_SUFFIXES:
49
+ return PythonParser()
50
+ if suffix in JAVA_SUFFIXES:
51
+ return JavaParser()
52
+ if suffix in GO_SUFFIXES:
53
+ return GoParser()
54
+ if suffix in RUST_SUFFIXES:
55
+ return RustParser()
56
+ if suffix in CPP_SUFFIXES:
57
+ return CppParser()
58
+ return None
59
+
60
+
61
+ def is_supported_source(path: Path) -> bool:
62
+ """Return True when the path is a supported source file."""
63
+ return path.suffix.lower() in SUPPORTED_SUFFIXES
64
+
65
+
66
+ def language_of(path: Path) -> str:
67
+ """Return the logical language name for a path."""
68
+ suffix = path.suffix.lower()
69
+ if suffix in JS_SUFFIXES:
70
+ return "javascript"
71
+ if suffix in TS_SUFFIXES:
72
+ return "typescript"
73
+ if suffix in PY_SUFFIXES:
74
+ return "python"
75
+ if suffix in JAVA_SUFFIXES:
76
+ return "java"
77
+ if suffix in GO_SUFFIXES:
78
+ return "go"
79
+ if suffix in RUST_SUFFIXES:
80
+ return "rust"
81
+ if suffix in CPP_SUFFIXES:
82
+ return "cpp"
83
+ return "unknown"
@@ -0,0 +1,102 @@
1
+ """C++ tree-sitter parser."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ from datetime import datetime
7
+ from pathlib import Path
8
+
9
+ import tree_sitter_cpp as tscpp
10
+ from tree_sitter import Language, Parser
11
+
12
+ from codegraph.models import CodeNode
13
+ from codegraph.parser.base import BaseParser
14
+ from codegraph.parser.common import (
15
+ decl_name,
16
+ declarator_name,
17
+ declares_a_function,
18
+ make_node,
19
+ rel_path,
20
+ simple_callee,
21
+ )
22
+
23
+ logger = logging.getLogger(__name__)
24
+ _LANG = Language(tscpp.language())
25
+
26
+ KIND_MAP = {
27
+ "function_definition": "function",
28
+ "class_specifier": "class",
29
+ "struct_specifier": "class",
30
+ }
31
+
32
+
33
+ class CppParser(BaseParser):
34
+ """Parse C++ sources for functions, classes, and calls."""
35
+
36
+ language = "cpp"
37
+
38
+ def parse_file(self, path: Path) -> list[CodeNode]:
39
+ """Parse a C++ file from disk."""
40
+ rel = rel_path(path)
41
+ try:
42
+ source = path.read_text(encoding="utf-8")
43
+ except (OSError, UnicodeDecodeError) as exc:
44
+ logger.warning("Read failed, skip %s: %s", path, exc)
45
+ return []
46
+ return self.parse_text(rel, source)
47
+
48
+ def parse_text(
49
+ self,
50
+ rel_path_: str,
51
+ source: str,
52
+ commit_sha: str = "",
53
+ valid_from: datetime | None = None,
54
+ ) -> list[CodeNode]:
55
+ """Parse C++ source text."""
56
+ rel = rel_path_.replace("\\", "/")
57
+ self._calls.clear()
58
+ raw = source.encode("utf-8")
59
+ tree = Parser(_LANG).parse(raw)
60
+ out: list[CodeNode] = []
61
+ self._walk(tree.root_node, raw, rel, out, None, "", commit_sha, valid_from)
62
+ return out
63
+
64
+ def _walk(self, node, raw, rel, out, parent_uid, current_fn, commit_sha, valid_from):
65
+ kind = KIND_MAP.get(node.type)
66
+ name: str | None = None
67
+ if node.type == "function_definition":
68
+ name = declarator_name(node, raw)
69
+ elif node.type in {"declaration", "field_declaration"}:
70
+ # A prototype declares a function; `int x;` does not.
71
+ if declares_a_function(node):
72
+ kind = "function"
73
+ name = declarator_name(node, raw)
74
+ else:
75
+ name = decl_name(node, raw)
76
+ # `TEST(suite, name) { ... }` is a macro invocation with a body. tree-sitter
77
+ # reports it as a function_definition whose name is the macro, so every
78
+ # TEST in a file became a node called "TEST" — and since call edges match on
79
+ # name, those nodes then fanned out to each other. On fmt that turned 1500
80
+ # nodes into 416k edges. C and C++ write macros in full capitals, so an
81
+ # all-caps name is a macro, not a function.
82
+ if kind == "function" and name and name.isupper():
83
+ kind, name = None, None
84
+ next_parent, next_fn = parent_uid, current_fn
85
+ if kind and name:
86
+ code_node = make_node(
87
+ node, rel, kind, name, parent_uid, self.language, commit_sha, valid_from
88
+ )
89
+ out.append(code_node)
90
+ if kind in {"class", "function"}:
91
+ next_parent = code_node.uid
92
+ if kind == "function":
93
+ next_fn = name
94
+ if node.type == "call_expression":
95
+ fn = node.child_by_field_name("function")
96
+ callee = simple_callee(fn, raw) if fn is not None else None
97
+ if callee:
98
+ self._calls.append(
99
+ (current_fn or name or "", callee, node.start_point[0] + 1, rel)
100
+ )
101
+ for child in node.children:
102
+ self._walk(child, raw, rel, out, next_parent, next_fn, commit_sha, valid_from)
@@ -0,0 +1,113 @@
1
+ """Go tree-sitter parser."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ from datetime import datetime
7
+ from pathlib import Path
8
+
9
+ import tree_sitter_go as tsgo
10
+ from tree_sitter import Language, Parser
11
+
12
+ from codegraph.models import CodeNode
13
+ from codegraph.parser.base import BaseParser
14
+ from codegraph.parser.common import decl_name, make_node, rel_path, simple_callee, type_name
15
+
16
+ logger = logging.getLogger(__name__)
17
+ _LANG = Language(tsgo.language())
18
+
19
+ KIND_MAP = {
20
+ "function_declaration": "function",
21
+ "method_declaration": "function",
22
+ "type_spec": "class",
23
+ }
24
+
25
+
26
+ class GoParser(BaseParser):
27
+ """Parse Go sources for funcs, types, and calls."""
28
+
29
+ language = "go"
30
+
31
+ def parse_file(self, path: Path) -> list[CodeNode]:
32
+ """Parse a Go file from disk."""
33
+ rel = rel_path(path)
34
+ try:
35
+ source = path.read_text(encoding="utf-8")
36
+ except (OSError, UnicodeDecodeError) as exc:
37
+ logger.warning("Read failed, skip %s: %s", path, exc)
38
+ return []
39
+ return self.parse_text(rel, source)
40
+
41
+ def parse_text(
42
+ self,
43
+ rel_path_: str,
44
+ source: str,
45
+ commit_sha: str = "",
46
+ valid_from: datetime | None = None,
47
+ ) -> list[CodeNode]:
48
+ """Parse Go source text."""
49
+ rel = rel_path_.replace("\\", "/")
50
+ self._calls.clear()
51
+ raw = source.encode("utf-8")
52
+ tree = Parser(_LANG).parse(raw)
53
+ out: list[CodeNode] = []
54
+ pending: list[tuple[CodeNode, str]] = []
55
+ self._walk(tree.root_node, raw, rel, out, None, "", commit_sha, valid_from, pending)
56
+ _attach_methods(out, pending)
57
+ return out
58
+
59
+ def _walk(
60
+ self, node, raw, rel, out, parent_uid, current_fn, commit_sha, valid_from, pending
61
+ ):
62
+ kind = KIND_MAP.get(node.type)
63
+ name = decl_name(node, raw)
64
+ next_parent, next_fn = parent_uid, current_fn
65
+ if kind and name:
66
+ code_node = make_node(
67
+ node, rel, kind, name, parent_uid, self.language, commit_sha, valid_from
68
+ )
69
+ out.append(code_node)
70
+ if kind in {"class", "function"}:
71
+ next_parent = code_node.uid
72
+ if kind == "function":
73
+ next_fn = name
74
+ if node.type == "method_declaration":
75
+ receiver = node.child_by_field_name("receiver")
76
+ resolved = _receiver_type(receiver, raw) if receiver is not None else None
77
+ if resolved:
78
+ pending.append((code_node, resolved))
79
+ if node.type == "call_expression":
80
+ fn = node.child_by_field_name("function")
81
+ callee = simple_callee(fn, raw) if fn is not None else None
82
+ if callee:
83
+ self._calls.append(
84
+ (current_fn or name or "", callee, node.start_point[0] + 1, rel)
85
+ )
86
+ for child in node.children:
87
+ self._walk(
88
+ child, raw, rel, out, next_parent, next_fn, commit_sha, valid_from, pending
89
+ )
90
+
91
+
92
+ def _receiver_type(receiver, raw: bytes) -> str | None:
93
+ """Return the type a Go method hangs off: `func (c *Calculator) Add()`."""
94
+ for child in receiver.children:
95
+ if child.type in {"parameter_declaration", "variadic_parameter_declaration"}:
96
+ type_node = child.child_by_field_name("type")
97
+ if type_node is not None:
98
+ resolved = type_name(type_node, raw)
99
+ if resolved:
100
+ return resolved
101
+ return None
102
+
103
+
104
+ def _attach_methods(out: list[CodeNode], pending: list[tuple[CodeNode, str]]) -> None:
105
+ """Point methods at the type in their receiver."""
106
+ by_name: dict[str, CodeNode] = {}
107
+ for node in out:
108
+ if node.kind == "class":
109
+ by_name.setdefault(node.name, node)
110
+ for code_node, type_name_ in pending:
111
+ parent = by_name.get(type_name_)
112
+ if parent is not None:
113
+ code_node.parent_uid = parent.uid
@@ -0,0 +1,79 @@
1
+ """Java tree-sitter parser."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ from datetime import datetime
7
+ from pathlib import Path
8
+
9
+ import tree_sitter_java as tsjava
10
+ from tree_sitter import Language, Parser
11
+
12
+ from codegraph.models import CodeNode
13
+ from codegraph.parser.base import BaseParser
14
+ from codegraph.parser.common import decl_name, make_node, rel_path, simple_callee
15
+
16
+ logger = logging.getLogger(__name__)
17
+ _LANG = Language(tsjava.language())
18
+
19
+ KIND_MAP = {
20
+ "class_declaration": "class",
21
+ "interface_declaration": "class",
22
+ "method_declaration": "function",
23
+ "constructor_declaration": "function",
24
+ }
25
+
26
+
27
+ class JavaParser(BaseParser):
28
+ """Parse Java sources for types, methods, and calls."""
29
+
30
+ language = "java"
31
+
32
+ def parse_file(self, path: Path) -> list[CodeNode]:
33
+ """Parse a Java file from disk."""
34
+ rel = rel_path(path)
35
+ try:
36
+ source = path.read_text(encoding="utf-8")
37
+ except (OSError, UnicodeDecodeError) as exc:
38
+ logger.warning("Read failed, skip %s: %s", path, exc)
39
+ return []
40
+ return self.parse_text(rel, source)
41
+
42
+ def parse_text(
43
+ self,
44
+ rel_path_: str,
45
+ source: str,
46
+ commit_sha: str = "",
47
+ valid_from: datetime | None = None,
48
+ ) -> list[CodeNode]:
49
+ """Parse Java source text."""
50
+ rel = rel_path(Path(rel_path_)) if "/" not in rel_path_ else rel_path_.replace("\\", "/")
51
+ self._calls.clear()
52
+ raw = source.encode("utf-8")
53
+ tree = Parser(_LANG).parse(raw)
54
+ out: list[CodeNode] = []
55
+ self._walk(tree.root_node, raw, rel, out, None, "", commit_sha, valid_from)
56
+ return out
57
+
58
+ def _walk(self, node, raw, rel, out, parent_uid, current_fn, commit_sha, valid_from):
59
+ kind = KIND_MAP.get(node.type)
60
+ name = decl_name(node, raw)
61
+ next_parent, next_fn = parent_uid, current_fn
62
+ if kind and name:
63
+ code_node = make_node(
64
+ node, rel, kind, name, parent_uid, self.language, commit_sha, valid_from
65
+ )
66
+ out.append(code_node)
67
+ if kind in {"class", "function"}:
68
+ next_parent = code_node.uid
69
+ if kind == "function":
70
+ next_fn = name
71
+ if node.type == "method_invocation":
72
+ fn = node.child_by_field_name("name") or node.child_by_field_name("object")
73
+ callee = simple_callee(fn, raw) if fn is not None else None
74
+ if callee:
75
+ self._calls.append(
76
+ (current_fn or name or "", callee, node.start_point[0] + 1, rel)
77
+ )
78
+ for child in node.children:
79
+ self._walk(child, raw, rel, out, next_parent, next_fn, commit_sha, valid_from)
@@ -0,0 +1,15 @@
1
+ """JavaScript parser reusing the TypeScript grammar."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from codegraph.parser.treesitter_ts import TypeScriptParser
6
+
7
+
8
+ class JavaScriptParser(TypeScriptParser):
9
+ """Parse .js / .mjs / .cjs with the TypeScript (non-JSX) grammar.
10
+
11
+ JavaScript is a subset of TypeScript syntax, so tree-sitter-typescript
12
+ can parse it without a separate grammar package.
13
+ """
14
+
15
+ language = "javascript"