codeatlas-viz 0.2.2__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.
- codeatlas/__init__.py +15 -0
- codeatlas/analyzers/__init__.py +72 -0
- codeatlas/analyzers/base.py +20 -0
- codeatlas/analyzers/c_family_analyzer.py +208 -0
- codeatlas/analyzers/js_analyzer.py +207 -0
- codeatlas/analyzers/python_analyzer.py +130 -0
- codeatlas/cli.py +134 -0
- codeatlas/models.py +276 -0
- codeatlas/scanner.py +335 -0
- codeatlas/server.py +208 -0
- codeatlas/web/assets/index-WkvacVWK.js +4 -0
- codeatlas/web/assets/index-vVc60hrc.css +1 -0
- codeatlas/web/index.html +13 -0
- codeatlas_viz-0.2.2.dist-info/METADATA +146 -0
- codeatlas_viz-0.2.2.dist-info/RECORD +19 -0
- codeatlas_viz-0.2.2.dist-info/WHEEL +5 -0
- codeatlas_viz-0.2.2.dist-info/entry_points.txt +2 -0
- codeatlas_viz-0.2.2.dist-info/licenses/LICENSE +21 -0
- codeatlas_viz-0.2.2.dist-info/top_level.txt +1 -0
codeatlas/__init__.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""CodeAtlas: Zero-config local codebase architecture & call-graph visualizer."""
|
|
2
|
+
|
|
3
|
+
from .models import CallEdge, CodeGraph, FileSummary, SymbolNode
|
|
4
|
+
from .scanner import scan_directory
|
|
5
|
+
|
|
6
|
+
__version__ = "0.2.2"
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"__version__",
|
|
10
|
+
"CallEdge",
|
|
11
|
+
"CodeGraph",
|
|
12
|
+
"FileSummary",
|
|
13
|
+
"SymbolNode",
|
|
14
|
+
"scan_directory",
|
|
15
|
+
]
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""Language analyzer registry."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from .base import ParsedFile
|
|
8
|
+
from .c_family_analyzer import analyze_c_family
|
|
9
|
+
from .js_analyzer import analyze_js
|
|
10
|
+
from .python_analyzer import analyze_python
|
|
11
|
+
|
|
12
|
+
EXTENSION_LANGUAGE: dict[str, str] = {
|
|
13
|
+
".py": "python",
|
|
14
|
+
".js": "javascript",
|
|
15
|
+
".jsx": "javascript",
|
|
16
|
+
".mjs": "javascript",
|
|
17
|
+
".ts": "typescript",
|
|
18
|
+
".tsx": "typescript",
|
|
19
|
+
".svelte": "svelte",
|
|
20
|
+
".go": "go",
|
|
21
|
+
".rs": "rust",
|
|
22
|
+
".java": "java",
|
|
23
|
+
".c": "c",
|
|
24
|
+
".h": "c",
|
|
25
|
+
".cpp": "cpp",
|
|
26
|
+
".cc": "cpp",
|
|
27
|
+
".cxx": "cpp",
|
|
28
|
+
".hpp": "cpp",
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
_C_FAMILY_LANGS = frozenset({"go", "rust", "java", "c", "cpp"})
|
|
32
|
+
_MINIFIABLE_LANGS = frozenset({"javascript", "typescript"})
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _looks_minified_source(source: str, lang: str) -> bool:
|
|
36
|
+
"""Detect minified JS/TS bundles from already-loaded source without re-opening the file."""
|
|
37
|
+
if lang not in _MINIFIABLE_LANGS:
|
|
38
|
+
return False
|
|
39
|
+
pos = 0
|
|
40
|
+
for _ in range(10):
|
|
41
|
+
nl = source.find("\n", pos)
|
|
42
|
+
line_len = (len(source) - pos) if nl == -1 else (nl - pos)
|
|
43
|
+
if line_len > 700:
|
|
44
|
+
return True
|
|
45
|
+
if nl == -1:
|
|
46
|
+
break
|
|
47
|
+
pos = nl + 1
|
|
48
|
+
return False
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def analyze_file(path: Path, rel_path: str) -> tuple[str, int, ParsedFile] | None:
|
|
52
|
+
"""Analyze a source file if its extension is supported."""
|
|
53
|
+
lang = EXTENSION_LANGUAGE.get(path.suffix.lower())
|
|
54
|
+
if not lang:
|
|
55
|
+
return None
|
|
56
|
+
|
|
57
|
+
try:
|
|
58
|
+
source = path.read_text(encoding="utf-8", errors="replace")
|
|
59
|
+
except OSError:
|
|
60
|
+
return None
|
|
61
|
+
|
|
62
|
+
if _looks_minified_source(source, lang):
|
|
63
|
+
return None
|
|
64
|
+
|
|
65
|
+
line_count = source.count("\n") + (1 if source else 0)
|
|
66
|
+
if lang == "python":
|
|
67
|
+
parsed = analyze_python(source, rel_path)
|
|
68
|
+
elif lang in _C_FAMILY_LANGS:
|
|
69
|
+
parsed = analyze_c_family(source, rel_path, language=lang)
|
|
70
|
+
else:
|
|
71
|
+
parsed = analyze_js(source, rel_path, language=lang)
|
|
72
|
+
return lang, line_count, parsed
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""Shared analyzer types."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
|
|
7
|
+
from ..models import SymbolNode
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(slots=True)
|
|
11
|
+
class ParsedFile:
|
|
12
|
+
"""Intermediate representation produced by a language analyzer."""
|
|
13
|
+
|
|
14
|
+
symbols: list[SymbolNode] = field(default_factory=list)
|
|
15
|
+
# Maps caller symbol ID -> list of raw callee names referenced inside it.
|
|
16
|
+
raw_calls: dict[str, list[str]] = field(default_factory=dict)
|
|
17
|
+
# Imported module/file hints to help disambiguate cross-file calls.
|
|
18
|
+
imports: list[str] = field(default_factory=list)
|
|
19
|
+
# Maps local alias -> original imported symbol name (e.g. "vt" -> "verify_token").
|
|
20
|
+
aliases: dict[str, str] = field(default_factory=dict)
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"""Structural analyzer for Go, Rust, Java, and C/C++ source files."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
from ..models import SymbolNode
|
|
8
|
+
from .base import ParsedFile
|
|
9
|
+
from .js_analyzer import (
|
|
10
|
+
_build_line_index,
|
|
11
|
+
_find_matching_brace,
|
|
12
|
+
_line_at,
|
|
13
|
+
_strip_comments_and_strings,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
_C_FAMILY_KEYWORDS = frozenset({
|
|
17
|
+
"if", "else", "for", "while", "do", "switch", "case", "catch", "finally",
|
|
18
|
+
"try", "return", "throw", "sizeof", "alignof", "typeof", "new", "delete",
|
|
19
|
+
"class", "struct", "enum", "union", "interface", "impl", "trait", "fn",
|
|
20
|
+
"func", "pub", "private", "protected", "public", "static", "const",
|
|
21
|
+
"virtual", "override", "synchronized", "match", "loop", "where", "defer",
|
|
22
|
+
"go", "select", "chan", "package", "import", "use", "mod", "crate",
|
|
23
|
+
"super", "self", "this", "main", "printf", "println", "fmt", "std",
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
# Go: func (r *Recv) Name(args) [ret] { OR func Name(args) [ret] {
|
|
27
|
+
_GO_FUNC_RE = re.compile(
|
|
28
|
+
r"""(?:^|\n)\s*func\s+(?:\(\s*[A-Za-z_]\w*\s+\*?([A-Za-z_]\w*)\s*\)\s+)?([A-Za-z_]\w*)\s*(?:\[[^\]]*\])?\s*\([^)]*\)[^{;]*\{""",
|
|
29
|
+
)
|
|
30
|
+
_GO_IMPORT_RE = re.compile(r'"([^"]+)"')
|
|
31
|
+
|
|
32
|
+
# Rust: [pub] [async] [unsafe] fn name[<T>](args) [-> Ret] [where ...] {
|
|
33
|
+
_RUST_STRUCT_IMPL_RE = re.compile(
|
|
34
|
+
r"""(?:^|\n)\s*(?:pub(?:\([^)]*\))?\s+)?(?:struct|enum|trait|impl(?:\s*<[^>]*>)?(?:\s+[A-Za-z_][\w:<>]*\s+for)?)\s+([A-Za-z_]\w*)[^{;]*\{""",
|
|
35
|
+
)
|
|
36
|
+
_RUST_FN_RE = re.compile(
|
|
37
|
+
r"""(?:^|\n)\s*(?:pub(?:\([^)]*\))?\s+)?(?:const\s+)?(?:async\s+)?(?:unsafe\s+)?fn\s+([A-Za-z_]\w*)\s*(?:<[^>]*>)?\s*\([^)]*\)[^{;]*\{""",
|
|
38
|
+
)
|
|
39
|
+
_RUST_USE_RE = re.compile(r"""(?:^|\n)\s*use\s+([A-Za-z_][\w:]*)\s*;""")
|
|
40
|
+
|
|
41
|
+
# Java / C / C++ classes & functions/methods (no nested quantifiers to avoid ReDoS)
|
|
42
|
+
_CLASS_STRUCT_RE = re.compile(
|
|
43
|
+
r"""(?:^|[\n;{}])\s*(?:(?:public|private|protected|abstract|final|static)\s+)*(?:class|struct|interface)\s+([A-Za-z_]\w*)[^{;]*\{""",
|
|
44
|
+
)
|
|
45
|
+
_CFAMILY_FUNC_RE = re.compile(
|
|
46
|
+
r"""(?:^|[\n;{}])\s*(?:(?:public|private|protected|static|final|virtual|inline|explicit|constexpr|synchronized|native|async)\s+)*(?:[A-Za-z_~][\w:<>,*&]*\s+)+([A-Za-z_~][\w:]*(?:::[A-Za-z_~]\w*)?)\s*\([^;{}]*\)\s*(?:const\s*)?(?:noexcept\s*)?(?:override\s*)?(?:throws\s+[A-Za-z0-9_.,\s]+)?\{""",
|
|
47
|
+
)
|
|
48
|
+
_JAVA_IMPORT_RE = re.compile(r"""(?:^|\n)\s*import\s+(?:static\s+)?([A-Za-z_][\w.]*)\s*;""")
|
|
49
|
+
_CPP_INCLUDE_RE = re.compile(r"""#\s*include\s*[<"]([^>"]+)[>"]""")
|
|
50
|
+
|
|
51
|
+
_CALL_RE = re.compile(r"""(?<![.\w:])([A-Za-z_]\w*)\s*\(""")
|
|
52
|
+
_MEMBER_CALL_RE = re.compile(r"""(?:\.|->|::)([A-Za-z_]\w*)\s*\(""")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _extract_calls(body: str) -> list[str]:
|
|
56
|
+
calls = [
|
|
57
|
+
m.group(1)
|
|
58
|
+
for m in _CALL_RE.finditer(body)
|
|
59
|
+
if m.group(1) not in _C_FAMILY_KEYWORDS
|
|
60
|
+
]
|
|
61
|
+
calls.extend(
|
|
62
|
+
m.group(1)
|
|
63
|
+
for m in _MEMBER_CALL_RE.finditer(body)
|
|
64
|
+
if m.group(1) not in _C_FAMILY_KEYWORDS
|
|
65
|
+
)
|
|
66
|
+
return calls
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _innermost_parent(pos: int, spans: list[tuple[int, int, str]]) -> str | None:
|
|
70
|
+
"""Return the innermost enclosing class/impl name for pos."""
|
|
71
|
+
best_span = None
|
|
72
|
+
best_name = None
|
|
73
|
+
for ob, cb, cname in spans:
|
|
74
|
+
if ob < pos < cb:
|
|
75
|
+
size = cb - ob
|
|
76
|
+
if best_span is None or size < best_span:
|
|
77
|
+
best_span = size
|
|
78
|
+
best_name = cname
|
|
79
|
+
return best_name
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _inside_any_span(pos: int, spans: list[tuple[int, int]]) -> bool:
|
|
83
|
+
return any(ob < pos < cb for ob, cb in spans)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def analyze_c_family(source: str, rel_path: str, language: str) -> ParsedFile:
|
|
87
|
+
"""Analyze Go, Rust, Java, or C/C++ source code into symbols and call sites."""
|
|
88
|
+
imports: list[str] = []
|
|
89
|
+
if language == "go":
|
|
90
|
+
imports.extend(_GO_IMPORT_RE.findall(source))
|
|
91
|
+
elif language == "rust":
|
|
92
|
+
imports.extend(m.replace("::", "/") for m in _RUST_USE_RE.findall(source))
|
|
93
|
+
elif language == "java":
|
|
94
|
+
imports.extend(_JAVA_IMPORT_RE.findall(source))
|
|
95
|
+
elif language in {"c", "cpp"}:
|
|
96
|
+
imports.extend(_CPP_INCLUDE_RE.findall(source))
|
|
97
|
+
|
|
98
|
+
clean = _strip_comments_and_strings(source)
|
|
99
|
+
newlines = _build_line_index(clean)
|
|
100
|
+
|
|
101
|
+
symbols: list[SymbolNode] = []
|
|
102
|
+
raw_calls: dict[str, list[str]] = {}
|
|
103
|
+
seen_ids: set[str] = set()
|
|
104
|
+
|
|
105
|
+
def register(
|
|
106
|
+
name: str,
|
|
107
|
+
kind: str,
|
|
108
|
+
decl_start: int,
|
|
109
|
+
open_brace: int,
|
|
110
|
+
parent: str | None = None,
|
|
111
|
+
) -> int:
|
|
112
|
+
close_idx = _find_matching_brace(clean, open_brace)
|
|
113
|
+
qname = f"{parent}.{name}" if parent else name
|
|
114
|
+
sym_id = f"{rel_path}::{qname}"
|
|
115
|
+
if sym_id in seen_ids:
|
|
116
|
+
return close_idx
|
|
117
|
+
seen_ids.add(sym_id)
|
|
118
|
+
|
|
119
|
+
symbols.append(
|
|
120
|
+
SymbolNode(
|
|
121
|
+
id=sym_id,
|
|
122
|
+
name=name,
|
|
123
|
+
kind=kind,
|
|
124
|
+
file=rel_path,
|
|
125
|
+
language=language,
|
|
126
|
+
start_line=_line_at(newlines, decl_start),
|
|
127
|
+
end_line=_line_at(newlines, close_idx),
|
|
128
|
+
parent=parent,
|
|
129
|
+
)
|
|
130
|
+
)
|
|
131
|
+
if kind != "class":
|
|
132
|
+
raw_calls[sym_id] = _extract_calls(clean[open_brace + 1 : close_idx])
|
|
133
|
+
return close_idx
|
|
134
|
+
|
|
135
|
+
if language == "go":
|
|
136
|
+
for m in _GO_FUNC_RE.finditer(clean):
|
|
137
|
+
recv, name = m.group(1), m.group(2)
|
|
138
|
+
kind = "method" if recv else "function"
|
|
139
|
+
register(name, kind, m.start(2), m.end() - 1, parent=recv)
|
|
140
|
+
|
|
141
|
+
elif language == "rust":
|
|
142
|
+
impl_spans: list[tuple[int, int, str]] = []
|
|
143
|
+
for m in _RUST_STRUCT_IMPL_RE.finditer(clean):
|
|
144
|
+
cname = m.group(1)
|
|
145
|
+
open_b = m.end() - 1
|
|
146
|
+
close_b = _find_matching_brace(clean, open_b)
|
|
147
|
+
register(cname, "class", m.start(1), open_b)
|
|
148
|
+
impl_spans.append((open_b, close_b, cname))
|
|
149
|
+
|
|
150
|
+
fn_spans: list[tuple[int, int]] = []
|
|
151
|
+
for m in _RUST_FN_RE.finditer(clean):
|
|
152
|
+
fn_name = m.group(1)
|
|
153
|
+
pos = m.start(1)
|
|
154
|
+
if _inside_any_span(pos, fn_spans):
|
|
155
|
+
continue
|
|
156
|
+
parent = _innermost_parent(pos, impl_spans)
|
|
157
|
+
open_b = m.end() - 1
|
|
158
|
+
close_b = register(
|
|
159
|
+
fn_name,
|
|
160
|
+
"method" if parent else "function",
|
|
161
|
+
pos,
|
|
162
|
+
open_b,
|
|
163
|
+
parent=parent,
|
|
164
|
+
)
|
|
165
|
+
fn_spans.append((open_b, close_b))
|
|
166
|
+
|
|
167
|
+
else:
|
|
168
|
+
# Java, C, C++
|
|
169
|
+
class_spans: list[tuple[int, int, str]] = []
|
|
170
|
+
for m in _CLASS_STRUCT_RE.finditer(clean):
|
|
171
|
+
cname = m.group(1)
|
|
172
|
+
if cname in _C_FAMILY_KEYWORDS:
|
|
173
|
+
continue
|
|
174
|
+
open_b = m.end() - 1
|
|
175
|
+
close_b = register(cname, "class", m.start(1), open_b)
|
|
176
|
+
class_spans.append((open_b, close_b, cname))
|
|
177
|
+
|
|
178
|
+
fn_spans = []
|
|
179
|
+
for m in _CFAMILY_FUNC_RE.finditer(clean):
|
|
180
|
+
raw_name = m.group(1)
|
|
181
|
+
pos = m.start(1)
|
|
182
|
+
if _inside_any_span(pos, fn_spans):
|
|
183
|
+
continue
|
|
184
|
+
|
|
185
|
+
parent: str | None = None
|
|
186
|
+
fname = raw_name
|
|
187
|
+
if "::" in raw_name:
|
|
188
|
+
parts = raw_name.split("::")
|
|
189
|
+
parent = parts[-2]
|
|
190
|
+
fname = parts[-1]
|
|
191
|
+
else:
|
|
192
|
+
parent = _innermost_parent(pos, class_spans)
|
|
193
|
+
|
|
194
|
+
if fname in _C_FAMILY_KEYWORDS:
|
|
195
|
+
continue
|
|
196
|
+
|
|
197
|
+
open_b = m.end() - 1
|
|
198
|
+
close_b = register(
|
|
199
|
+
fname,
|
|
200
|
+
"method" if parent else "function",
|
|
201
|
+
pos,
|
|
202
|
+
open_b,
|
|
203
|
+
parent=parent,
|
|
204
|
+
)
|
|
205
|
+
fn_spans.append((open_b, close_b))
|
|
206
|
+
|
|
207
|
+
symbols.sort(key=lambda s: s.start_line)
|
|
208
|
+
return ParsedFile(symbols=symbols, raw_calls=raw_calls, imports=imports)
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
"""Structural analyzer for JavaScript, TypeScript, and Svelte files."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import bisect
|
|
6
|
+
import re
|
|
7
|
+
|
|
8
|
+
from ..models import SymbolNode
|
|
9
|
+
from .base import ParsedFile
|
|
10
|
+
|
|
11
|
+
_KEYWORDS = frozenset({
|
|
12
|
+
"if", "else", "for", "while", "do", "switch", "case", "catch", "finally",
|
|
13
|
+
"try", "return", "throw", "typeof", "instanceof", "new", "delete", "void",
|
|
14
|
+
"await", "yield", "import", "export", "from", "class", "function", "super",
|
|
15
|
+
"this", "constructor", "get", "set", "static", "async",
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
_SVELTE_SCRIPT_RE = re.compile(
|
|
19
|
+
r"<script(?:\s[^>]*)?>(.*?)</script>",
|
|
20
|
+
re.DOTALL | re.IGNORECASE,
|
|
21
|
+
)
|
|
22
|
+
_IMPORT_RE = re.compile(r"""import\s+([^;]*?)\s+from\s+['"]([^'"]+)['"]""")
|
|
23
|
+
_NAMED_ALIAS_RE = re.compile(r"""([A-Za-z_$][\w$]*)\s+as\s+([A-Za-z_$][\w$]*)""")
|
|
24
|
+
_FUNC_DECL_RE = re.compile(
|
|
25
|
+
r"""(?:export\s+(?:default\s+)?)?(?:async\s+)?function\s+([A-Za-z_$][\w$]*)\s*(?:<[^>]*>)?\s*\([^)]*\)\s*(?::\s*[^\{]+)?\{""",
|
|
26
|
+
)
|
|
27
|
+
_ARROW_DECL_RE = re.compile(
|
|
28
|
+
r"""(?:export\s+)?(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*(?::\s*[^=]+)?=\s*(?:async\s+)?(?:\([^)]*\)|[A-Za-z_$][\w$]*)\s*(?::\s*[^=]+)?=>\s*\{""",
|
|
29
|
+
)
|
|
30
|
+
_CLASS_DECL_RE = re.compile(
|
|
31
|
+
r"""(?:export\s+(?:default\s+)?)?class\s+([A-Za-z_$][\w$]*)(?:\s+(?:extends|implements)\s+[A-Za-z_$][\w$.<> ,]*)?\s*\{""",
|
|
32
|
+
)
|
|
33
|
+
_METHOD_DECL_RE = re.compile(
|
|
34
|
+
r"""(?:^|[\n;{}])\s*(?:(?:public|private|protected|static|async|override|readonly|get|set)\s+)*([A-Za-z_$][\w$]*)\s*(?:<[^>]*>)?\s*\([^)]*\)\s*(?::\s*[^\{]+)?\{""",
|
|
35
|
+
)
|
|
36
|
+
_CLASS_ARROW_PROP_RE = re.compile(
|
|
37
|
+
r"""(?:^|[\n;{}])\s*(?:(?:public|private|protected|static|override|readonly)\s+)*([A-Za-z_$][\w$]*)\s*(?::\s*[^=]+)?=\s*(?:async\s+)?(?:\([^)]*\)|[A-Za-z_$][\w$]*)\s*(?::\s*[^=]+)?=>\s*\{""",
|
|
38
|
+
)
|
|
39
|
+
_CALL_RE = re.compile(r"""(?<![.\w$])([A-Za-z_$][\w$]*)\s*\(""")
|
|
40
|
+
_METHOD_CALL_RE = re.compile(r"""\.([A-Za-z_$][\w$]*)\s*\(""")
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _extract_svelte_scripts(source: str) -> str:
|
|
44
|
+
"""Keep only <script> contents while preserving original line numbers."""
|
|
45
|
+
chars = list(source)
|
|
46
|
+
keep = [False] * len(chars)
|
|
47
|
+
for match in _SVELTE_SCRIPT_RE.finditer(source):
|
|
48
|
+
for idx in range(match.start(1), match.end(1)):
|
|
49
|
+
keep[idx] = True
|
|
50
|
+
|
|
51
|
+
return "".join(ch if (keep[idx] or ch == "\n") else " " for idx, ch in enumerate(chars))
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
_STRIP_RE = re.compile(
|
|
55
|
+
r'//[^\n]*|/\*[\s\S]*?\*/|"(?:\\.|[^"\\\n])*"|\'(?:\\.|[^\'\\\n])*\'|`(?:\\.|[^`\\])*`'
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _blank_match(m: re.Match[str]) -> str:
|
|
60
|
+
txt = m.group(0)
|
|
61
|
+
if "\n" not in txt:
|
|
62
|
+
return " " * len(txt)
|
|
63
|
+
return "\n".join(" " * len(part) for part in txt.split("\n"))
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _strip_comments_and_strings(code: str) -> str:
|
|
67
|
+
"""Replace comments and string/template contents with spaces, keeping exact offsets and newlines."""
|
|
68
|
+
return _STRIP_RE.sub(_blank_match, code)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _find_matching_brace(code: str, open_idx: int) -> int:
|
|
72
|
+
"""Return index of closing '}' matching the '{' at open_idx."""
|
|
73
|
+
depth = 0
|
|
74
|
+
for idx in range(open_idx, len(code)):
|
|
75
|
+
if code[idx] == "{":
|
|
76
|
+
depth += 1
|
|
77
|
+
elif code[idx] == "}":
|
|
78
|
+
depth -= 1
|
|
79
|
+
if depth == 0:
|
|
80
|
+
return idx
|
|
81
|
+
return len(code) - 1
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _build_line_index(code: str) -> list[int]:
|
|
85
|
+
"""Build sorted char offsets of all newlines using C-level str.find."""
|
|
86
|
+
newlines: list[int] = []
|
|
87
|
+
pos = code.find("\n")
|
|
88
|
+
while pos != -1:
|
|
89
|
+
newlines.append(pos)
|
|
90
|
+
pos = code.find("\n", pos + 1)
|
|
91
|
+
return newlines
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _line_at(newlines: list[int], idx: int) -> int:
|
|
95
|
+
return bisect.bisect_left(newlines, idx) + 1
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _extract_calls(body: str) -> list[str]:
|
|
99
|
+
calls = [
|
|
100
|
+
m.group(1)
|
|
101
|
+
for m in _CALL_RE.finditer(body)
|
|
102
|
+
if m.group(1) not in _KEYWORDS
|
|
103
|
+
]
|
|
104
|
+
calls.extend(
|
|
105
|
+
m.group(1)
|
|
106
|
+
for m in _METHOD_CALL_RE.finditer(body)
|
|
107
|
+
if m.group(1) not in _KEYWORDS
|
|
108
|
+
)
|
|
109
|
+
return calls
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def analyze_js(source: str, rel_path: str, language: str = "javascript") -> ParsedFile:
|
|
113
|
+
"""Analyze JS/TS/Svelte source code for symbols, methods, aliases, and call sites."""
|
|
114
|
+
working = _extract_svelte_scripts(source) if language == "svelte" else source
|
|
115
|
+
|
|
116
|
+
imports: list[str] = []
|
|
117
|
+
aliases: dict[str, str] = {}
|
|
118
|
+
for clause, mod in _IMPORT_RE.findall(working):
|
|
119
|
+
imports.append(mod)
|
|
120
|
+
for orig, alias in _NAMED_ALIAS_RE.findall(clause):
|
|
121
|
+
aliases[alias] = orig
|
|
122
|
+
|
|
123
|
+
clean = _strip_comments_and_strings(working)
|
|
124
|
+
newlines = _build_line_index(clean)
|
|
125
|
+
|
|
126
|
+
# Collect raw spans: (decl_start, open_brace, close_brace, name, kind)
|
|
127
|
+
raw_spans: list[tuple[int, int, int, str, str]] = []
|
|
128
|
+
seen_open_braces: set[int] = set()
|
|
129
|
+
|
|
130
|
+
def add_span(name: str, kind: str, decl_start: int, open_brace: int) -> int:
|
|
131
|
+
close_idx = _find_matching_brace(clean, open_brace)
|
|
132
|
+
if open_brace in seen_open_braces:
|
|
133
|
+
return close_idx
|
|
134
|
+
seen_open_braces.add(open_brace)
|
|
135
|
+
raw_spans.append((decl_start, open_brace, close_idx, name, kind))
|
|
136
|
+
return close_idx
|
|
137
|
+
|
|
138
|
+
# 1. Classes and direct class body methods/arrow properties
|
|
139
|
+
for match in _CLASS_DECL_RE.finditer(clean):
|
|
140
|
+
class_open = match.end() - 1
|
|
141
|
+
class_close = add_span(match.group(1), "class", match.start(), class_open)
|
|
142
|
+
class_body = clean[class_open + 1 : class_close]
|
|
143
|
+
offset = class_open + 1
|
|
144
|
+
|
|
145
|
+
# Precompute depth array once for O(1) depth check at any match position
|
|
146
|
+
depth_at: list[int] = [0] * (len(class_body) + 1)
|
|
147
|
+
cur_depth = 0
|
|
148
|
+
for i, ch in enumerate(class_body):
|
|
149
|
+
depth_at[i] = cur_depth
|
|
150
|
+
if ch == "{":
|
|
151
|
+
cur_depth += 1
|
|
152
|
+
elif ch == "}":
|
|
153
|
+
cur_depth -= 1
|
|
154
|
+
depth_at[len(class_body)] = cur_depth
|
|
155
|
+
|
|
156
|
+
for pat in (_METHOD_DECL_RE, _CLASS_ARROW_PROP_RE):
|
|
157
|
+
for m in pat.finditer(class_body):
|
|
158
|
+
method_name = m.group(1)
|
|
159
|
+
if method_name in _KEYWORDS:
|
|
160
|
+
continue
|
|
161
|
+
# Check brace depth right at the method identifier start
|
|
162
|
+
if depth_at[m.start(1)] != 0:
|
|
163
|
+
continue
|
|
164
|
+
add_span(method_name, "method", offset + m.start(1), offset + m.end() - 1)
|
|
165
|
+
|
|
166
|
+
# 2. Functions and const/let/var arrow functions (including nested scopes)
|
|
167
|
+
for pattern in (_FUNC_DECL_RE, _ARROW_DECL_RE):
|
|
168
|
+
for match in pattern.finditer(clean):
|
|
169
|
+
add_span(match.group(1), "function", match.start(), match.end() - 1)
|
|
170
|
+
|
|
171
|
+
# Sort by source position and compute hierarchical qualified IDs
|
|
172
|
+
raw_spans.sort(key=lambda sp: sp[0])
|
|
173
|
+
scope_stack: list[tuple[int, str, str]] = [] # (close_idx, name, kind)
|
|
174
|
+
|
|
175
|
+
symbols: list[SymbolNode] = []
|
|
176
|
+
raw_calls: dict[str, list[str]] = {}
|
|
177
|
+
seen_ids: set[str] = set()
|
|
178
|
+
|
|
179
|
+
for decl_start, open_brace, close_idx, name, kind in raw_spans:
|
|
180
|
+
while scope_stack and scope_stack[-1][0] < decl_start:
|
|
181
|
+
scope_stack.pop()
|
|
182
|
+
|
|
183
|
+
parent = scope_stack[-1][1] if scope_stack else None
|
|
184
|
+
qname = ".".join([*(s[1] for s in scope_stack), name])
|
|
185
|
+
sym_id = f"{rel_path}::{qname}"
|
|
186
|
+
if sym_id in seen_ids:
|
|
187
|
+
continue
|
|
188
|
+
seen_ids.add(sym_id)
|
|
189
|
+
|
|
190
|
+
symbols.append(
|
|
191
|
+
SymbolNode(
|
|
192
|
+
id=sym_id,
|
|
193
|
+
name=name,
|
|
194
|
+
kind=kind,
|
|
195
|
+
file=rel_path,
|
|
196
|
+
language=language,
|
|
197
|
+
start_line=_line_at(newlines, decl_start),
|
|
198
|
+
end_line=_line_at(newlines, close_idx),
|
|
199
|
+
parent=parent,
|
|
200
|
+
)
|
|
201
|
+
)
|
|
202
|
+
if kind != "class":
|
|
203
|
+
raw_calls[sym_id] = _extract_calls(clean[open_brace + 1 : close_idx])
|
|
204
|
+
|
|
205
|
+
scope_stack.append((close_idx, name, kind))
|
|
206
|
+
|
|
207
|
+
return ParsedFile(symbols=symbols, raw_calls=raw_calls, imports=imports, aliases=aliases)
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""Python AST analyzer for extracting symbols and call relationships."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ast
|
|
6
|
+
|
|
7
|
+
from ..models import SymbolNode
|
|
8
|
+
from .base import ParsedFile
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _call_name(node: ast.expr) -> str | None:
|
|
12
|
+
"""Extract a readable callee name from an ast.Call func expression."""
|
|
13
|
+
if isinstance(node, ast.Name):
|
|
14
|
+
return node.id
|
|
15
|
+
if isinstance(node, ast.Attribute):
|
|
16
|
+
return node.attr
|
|
17
|
+
return None
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class _PythonVisitor(ast.NodeVisitor):
|
|
21
|
+
"""Walks a Python AST tracking the active class and function scope."""
|
|
22
|
+
|
|
23
|
+
def __init__(self, rel_path: str) -> None:
|
|
24
|
+
self.rel_path = rel_path
|
|
25
|
+
self.symbols: list[SymbolNode] = []
|
|
26
|
+
self.raw_calls: dict[str, list[str]] = {}
|
|
27
|
+
self.imports: list[str] = []
|
|
28
|
+
self.aliases: dict[str, str] = {}
|
|
29
|
+
self._class_stack: list[str] = []
|
|
30
|
+
self._scope_names: list[str] = []
|
|
31
|
+
self._func_stack: list[str] = []
|
|
32
|
+
|
|
33
|
+
def _make_id(self, qualified_name: str) -> str:
|
|
34
|
+
return f"{self.rel_path}::{qualified_name}"
|
|
35
|
+
|
|
36
|
+
def visit_Import(self, node: ast.Import) -> None:
|
|
37
|
+
for alias in node.names:
|
|
38
|
+
self.imports.append(alias.name)
|
|
39
|
+
if alias.asname:
|
|
40
|
+
self.aliases[alias.asname] = alias.name.split(".")[-1]
|
|
41
|
+
|
|
42
|
+
def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
|
|
43
|
+
if node.module:
|
|
44
|
+
self.imports.append(node.module)
|
|
45
|
+
for alias in node.names:
|
|
46
|
+
if not node.module and alias.name != "*":
|
|
47
|
+
self.imports.append(alias.name)
|
|
48
|
+
if alias.asname and alias.name != "*":
|
|
49
|
+
self.aliases[alias.asname] = alias.name
|
|
50
|
+
|
|
51
|
+
def visit_ClassDef(self, node: ast.ClassDef) -> None:
|
|
52
|
+
self._scope_names.append(node.name)
|
|
53
|
+
qname = ".".join(self._scope_names)
|
|
54
|
+
sym_id = self._make_id(qname)
|
|
55
|
+
doc = ast.get_docstring(node) or ""
|
|
56
|
+
self.symbols.append(
|
|
57
|
+
SymbolNode(
|
|
58
|
+
id=sym_id,
|
|
59
|
+
name=node.name,
|
|
60
|
+
kind="class",
|
|
61
|
+
file=self.rel_path,
|
|
62
|
+
language="python",
|
|
63
|
+
start_line=node.lineno,
|
|
64
|
+
end_line=node.end_lineno or node.lineno,
|
|
65
|
+
docstring=doc.splitlines()[0] if doc else "",
|
|
66
|
+
parent=self._class_stack[-1] if self._class_stack else None,
|
|
67
|
+
)
|
|
68
|
+
)
|
|
69
|
+
self._class_stack.append(node.name)
|
|
70
|
+
self.generic_visit(node)
|
|
71
|
+
self._class_stack.pop()
|
|
72
|
+
self._scope_names.pop()
|
|
73
|
+
|
|
74
|
+
def _visit_func(self, node: ast.FunctionDef | ast.AsyncFunctionDef) -> None:
|
|
75
|
+
in_class_directly = bool(self._class_stack) and not self._func_stack
|
|
76
|
+
kind = "method" if in_class_directly else "function"
|
|
77
|
+
parent = self._class_stack[-1] if self._class_stack else None
|
|
78
|
+
|
|
79
|
+
self._scope_names.append(node.name)
|
|
80
|
+
qname = ".".join(self._scope_names)
|
|
81
|
+
sym_id = self._make_id(qname)
|
|
82
|
+
doc = ast.get_docstring(node) or ""
|
|
83
|
+
self.symbols.append(
|
|
84
|
+
SymbolNode(
|
|
85
|
+
id=sym_id,
|
|
86
|
+
name=node.name,
|
|
87
|
+
kind=kind,
|
|
88
|
+
file=self.rel_path,
|
|
89
|
+
language="python",
|
|
90
|
+
start_line=node.lineno,
|
|
91
|
+
end_line=node.end_lineno or node.lineno,
|
|
92
|
+
docstring=doc.splitlines()[0] if doc else "",
|
|
93
|
+
parent=parent,
|
|
94
|
+
)
|
|
95
|
+
)
|
|
96
|
+
self.raw_calls.setdefault(sym_id, [])
|
|
97
|
+
self._func_stack.append(sym_id)
|
|
98
|
+
self.generic_visit(node)
|
|
99
|
+
self._func_stack.pop()
|
|
100
|
+
self._scope_names.pop()
|
|
101
|
+
|
|
102
|
+
def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
|
|
103
|
+
self._visit_func(node)
|
|
104
|
+
|
|
105
|
+
def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None:
|
|
106
|
+
self._visit_func(node)
|
|
107
|
+
|
|
108
|
+
def visit_Call(self, node: ast.Call) -> None:
|
|
109
|
+
if self._func_stack:
|
|
110
|
+
callee = _call_name(node.func)
|
|
111
|
+
if callee:
|
|
112
|
+
self.raw_calls[self._func_stack[-1]].append(callee)
|
|
113
|
+
self.generic_visit(node)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def analyze_python(source: str, rel_path: str) -> ParsedFile:
|
|
117
|
+
"""Parse Python source code into symbols and call references."""
|
|
118
|
+
try:
|
|
119
|
+
tree = ast.parse(source, filename=rel_path)
|
|
120
|
+
except SyntaxError:
|
|
121
|
+
return ParsedFile()
|
|
122
|
+
|
|
123
|
+
visitor = _PythonVisitor(rel_path)
|
|
124
|
+
visitor.visit(tree)
|
|
125
|
+
return ParsedFile(
|
|
126
|
+
symbols=visitor.symbols,
|
|
127
|
+
raw_calls=visitor.raw_calls,
|
|
128
|
+
imports=visitor.imports,
|
|
129
|
+
aliases=visitor.aliases,
|
|
130
|
+
)
|