@allansantos-dev/smart-tool 0.9.61 → 0.9.66

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.
package/code_graph_go.py CHANGED
@@ -1,19 +1,318 @@
1
- """Imports between Go files, read from the package clause and the import declarations of the indexed snapshot.
1
+ """Go files read without a Go toolchain: imports, the functions, methods and types each file declares, their doc
2
+ comments and the static calls between them.
3
+
4
+ A lexer following the lexical elements of the Go specification (comments, interpreted and raw strings, runes) feeds a
5
+ declaration reader built on Go's automatic semicolons: a top-level declaration ends at the first line break, outside
6
+ brackets, after a token that ends a statement. A doc comment is the comment group on the lines right above a
7
+ declaration, as go/parser attaches it.
2
8
 
3
9
  A Go package is a directory, so a file that imports a package depends on every non-test file of that directory, and an
4
10
  internal test file (`package x` in `x_test.go`) depends on every non-test file of its own directory. Import paths are
5
11
  resolved against the nearest `go.mod` module path; with no `go.mod` in the snapshot an import resolves to the longest
6
- directory whose path ends the import path. No symbols or calls are produced: the doc and duplicate hooks stay silent
7
- for Go until their thresholds are measured on it, and `affected_tests` selects by package import."""
12
+ directory whose path ends the import path.
13
+
14
+ A call is resolved only when its target is certain: a function of the same package, an exported function of an imported
15
+ package, or a method called on the receiver, a parameter or a local variable whose type is written in the code
16
+ (`var x T`, `x := &T{}`, `x := NewT()` with a declared result type). Calls through interfaces, embedded fields,
17
+ function values and chains stay unresolved."""
18
+ import bisect
8
19
  import posixpath
9
20
  import re
21
+ from collections import namedtuple
10
22
 
23
+ _TOKEN = re.compile(r"(?P<comment>//[^\n]*|/\*.*?\*/)"
24
+ r"|(?P<string>\"(?:\\.|[^\"\\\n])*\"|`[^`]*`|'(?:\\.|[^'\\\n])*')"
25
+ r"|(?P<ident>[^\W\d]\w*)"
26
+ r"|(?P<number>\.?\d[\w.]*)"
27
+ r"|(?P<op>:=|\.\.\.|\S)", re.S)
11
28
  _COMMENT = re.compile(r"//[^\n]*|/\*.*?\*/", re.S)
12
29
  _PACKAGE = re.compile(r"^package\s+(\w+)", re.M)
13
30
  _IMPORT_BLOCK = re.compile(r"^import\s*\((.*?)^\)", re.M | re.S)
14
- _IMPORT_ONE = re.compile(r'^import\s+(?:[\w.]+\s+)?"([^"]+)"', re.M)
15
- _IMPORT_SPEC = re.compile(r'^\s*(?:[\w.]+\s+)?"([^"]+)"', re.M)
31
+ _IMPORT_ONE = re.compile(r'^import\s+([\w.]+\s+)?"([^"]+)"', re.M)
32
+ _IMPORT_SPEC = re.compile(r'^\s*([\w.]+\s+)?"([^"]+)"', re.M)
16
33
  _MODULE = re.compile(r"^module\s+(\S+)", re.M)
34
+ _DIRECTIVE = re.compile(r"^(line |extern |export |[a-z0-9]+:[a-z0-9])")
35
+ _VERSION_SUFFIX = re.compile(r"^v\d+$")
36
+ _KEYWORDS = {"break", "case", "chan", "const", "continue", "default", "defer", "else", "fallthrough", "for", "func",
37
+ "go", "goto", "if", "import", "interface", "map", "package", "range", "return", "select", "struct",
38
+ "switch", "type", "var"}
39
+ _BUILTINS = {"append", "cap", "clear", "close", "complex", "copy", "delete", "imag", "len", "make", "max", "min", "new",
40
+ "panic", "print", "println", "real", "recover", "any", "bool", "byte", "comparable", "complex64",
41
+ "complex128", "error", "float32", "float64", "int", "int8", "int16", "int32", "int64", "rune", "string",
42
+ "uint", "uint8", "uint16", "uint32", "uint64", "uintptr"}
43
+ _UNRESOLVED = "External, dynamic or unresolved target in this snapshot."
44
+ _Token = namedtuple("_Token", "kind text start end line end_line")
45
+
46
+
47
+ def _lex(text):
48
+ """Code tokens and comments of one Go file, each with its first and last line."""
49
+ newlines = [match.start() for match in re.finditer("\n", text)]
50
+ tokens, comments = [], []
51
+ for match in _TOKEN.finditer(text):
52
+ token = _Token(match.lastgroup, match.group(), match.start(), match.end(),
53
+ bisect.bisect_left(newlines, match.start()) + 1,
54
+ bisect.bisect_left(newlines, match.end() - 1) + 1)
55
+ (comments if token.kind == "comment" else tokens).append(token)
56
+ return tokens, comments
57
+
58
+
59
+ def _pairs(tokens):
60
+ """Index of the matching closing bracket of each opening one."""
61
+ pairs, stack = {}, []
62
+ for index, token in enumerate(tokens):
63
+ if token.kind == "op" and token.text in ("(", "[", "{"):
64
+ stack.append(index)
65
+ elif token.kind == "op" and token.text in (")", "]", "}") and stack:
66
+ pairs[stack.pop()] = index
67
+ return pairs
68
+
69
+
70
+ def _ends_statement(token):
71
+ return token.kind in ("ident", "number", "string") or token.text in (")", "]", "}")
72
+
73
+
74
+ def _end(tokens, pairs, start, stop):
75
+ """Index of the last token of the declaration or spec starting at start: where Go inserts a semicolon (a line
76
+ break after a statement-ending token outside brackets), before an explicit ';', or right before stop."""
77
+ index = start
78
+ while index < stop:
79
+ if tokens[index].text in ("(", "[", "{") and tokens[index].kind == "op":
80
+ index = min(pairs.get(index, stop - 1), stop - 1)
81
+ following = tokens[index + 1] if index + 1 < stop else None
82
+ if following is None or following.text == ";" or (following.line > tokens[index].end_line
83
+ and _ends_statement(tokens[index])):
84
+ return index
85
+ index += 1
86
+ return stop - 1
87
+
88
+
89
+ def _comment_text(group):
90
+ """Text of a comment group as go/ast CommentGroup.Text returns it: markers, the first space of a line comment,
91
+ directives (//go:, //line ...) and surrounding empty lines removed."""
92
+ lines = []
93
+ for comment in group:
94
+ if comment.text.startswith("//"):
95
+ body = comment.text[2:]
96
+ if not _DIRECTIVE.match(body):
97
+ lines.append(body[1:] if body.startswith(" ") else body)
98
+ else:
99
+ lines.extend(comment.text[2:-2].splitlines())
100
+ return "\n".join(line.rstrip() for line in lines).strip("\n")
101
+
102
+
103
+ def _lead_docs(tokens, comments):
104
+ """Text of each comment group that stands on its own lines, keyed by the line right after the group. A comment
105
+ after code on the same line is a line comment and never documents the next declaration."""
106
+ code_end = {}
107
+ for token in tokens:
108
+ code_end[token.end_line] = min(code_end.get(token.end_line, token.end), token.end)
109
+ docs, group = {}, []
110
+ for comment in comments:
111
+ trailing = code_end.get(comment.line, comment.start + 1) <= comment.start
112
+ if group and not trailing and comment.line <= group[-1].end_line + 1:
113
+ group.append(comment)
114
+ continue
115
+ if group:
116
+ docs[group[-1].end_line + 1] = _comment_text(group)
117
+ group = [] if trailing else [comment]
118
+ if group:
119
+ docs[group[-1].end_line + 1] = _comment_text(group)
120
+ return docs
121
+
122
+
123
+ def _type_ref(tokens):
124
+ """(package alias or None, type name) of a type written as T, *T, pkg.T or a generic instance of them; None for
125
+ any other type expression."""
126
+ index = 0
127
+ while index < len(tokens) and tokens[index].text in ("*", "&"):
128
+ index += 1
129
+ rest = tokens[index:]
130
+ cut = next((k for k, token in enumerate(rest) if token.text in ("[", "{")), len(rest))
131
+ rest = rest[:cut]
132
+ if len(rest) == 1 and rest[0].kind == "ident" and rest[0].text not in _KEYWORDS:
133
+ return None, rest[0].text
134
+ if len(rest) == 3 and rest[0].kind == "ident" and rest[1].text == "." and rest[2].kind == "ident":
135
+ return rest[0].text, rest[2].text
136
+ return None
137
+
138
+
139
+ def _split(tokens, pairs, start, stop):
140
+ """Comma-separated groups of tokens[start:stop], brackets kept whole."""
141
+ groups, current, index = [], [], start
142
+ while index < stop:
143
+ token = tokens[index]
144
+ if token.text == "," and token.kind == "op":
145
+ groups.append(current)
146
+ current = []
147
+ index += 1
148
+ continue
149
+ close = pairs.get(index, index) if token.text in ("(", "[", "{") and token.kind == "op" else index
150
+ current.extend(tokens[index:close + 1])
151
+ index = close + 1
152
+ if current:
153
+ groups.append(current)
154
+ return groups
155
+
156
+
157
+ def _parameters(tokens, pairs, open_index):
158
+ """Parameter name -> type reference of a parameter list, following Go's grouping (`a, b T`)."""
159
+ found, pending = {}, []
160
+ for group in _split(tokens, pairs, open_index + 1, pairs.get(open_index, open_index)):
161
+ if len(group) == 1 and group[0].kind == "ident":
162
+ pending.append(group[0].text)
163
+ continue
164
+ if len(group) > 1 and group[0].kind == "ident" and group[1].text != ".":
165
+ reference = _type_ref([t for t in group[1:] if t.text != "..."])
166
+ for name in pending + [group[0].text]:
167
+ found[name] = reference
168
+ pending = []
169
+ return found
170
+
171
+
172
+ def _function(tokens, pairs, opens, start, end, text):
173
+ """One func declaration: name, receiver type and variable, lines, signature, parameter and result types and the
174
+ token range of its body (None for a declaration without body)."""
175
+ index = start + 1
176
+ receiver = receiver_var = None
177
+ if index <= end and tokens[index].text == "(":
178
+ close = pairs.get(index, end)
179
+ inner = tokens[index + 1:close]
180
+ cut = next((k for k, token in enumerate(inner) if token.text == "["), len(inner))
181
+ names = [token.text for token in inner[:cut] if token.kind == "ident"]
182
+ if not names:
183
+ return None
184
+ receiver, receiver_var = names[-1], names[0] if len(names) > 1 else None
185
+ index = close + 1
186
+ if index > end or tokens[index].kind != "ident":
187
+ return None
188
+ name = tokens[index].text
189
+ index += 1
190
+ if index <= end and tokens[index].text == "[":
191
+ index = pairs.get(index, end) + 1
192
+ if index > end or tokens[index].text != "(":
193
+ return None
194
+ parameters = _parameters(tokens, pairs, index)
195
+ index = pairs.get(index, end) + 1
196
+ body = None
197
+ opener = opens.get(end)
198
+ if tokens[end].text == "}" and opener is not None and opener >= index and \
199
+ tokens[opener - 1].text not in ("struct", "interface"):
200
+ body = (opener, end)
201
+ results = tokens[index:body[0] if body else end + 1]
202
+ if results and results[0].text == "(":
203
+ groups = _split(tokens, pairs, index + 1, pairs.get(index, index))
204
+ first = groups[0] if groups else []
205
+ if len(first) > 1 and first[0].kind == "ident" and first[1].text != ".":
206
+ first = first[1:]
207
+ result = _type_ref(first)
208
+ else:
209
+ result = _type_ref(results)
210
+ last = tokens[body[0] - 1] if body else tokens[end]
211
+ return {"name": name, "kind": "method" if receiver else "function", "receiver": receiver,
212
+ "receiver_var": receiver_var, "start": tokens[start].line, "end": tokens[end].end_line,
213
+ "signature": " ".join(text[tokens[start].start:last.end].split()), "parameters": parameters,
214
+ "result": result, "body": body}
215
+
216
+
217
+ def _ranges(tokens, pairs, open_index, close):
218
+ """(first, last) token index of each spec inside the brackets at open_index: a grouped declaration or the fields
219
+ of a struct."""
220
+ ranges, spec = [], open_index + 1
221
+ while spec < close:
222
+ spec_end = _end(tokens, pairs, spec, close)
223
+ if tokens[spec].text != ";":
224
+ ranges.append((spec, spec_end))
225
+ spec = spec_end + 1
226
+ return ranges
227
+
228
+
229
+ def _fields(tokens, pairs, open_index):
230
+ """(field names, or None for an embedded field, type reference) of each field of the struct at open_index."""
231
+ found = []
232
+ for first, last in _ranges(tokens, pairs, open_index, pairs.get(open_index, open_index)):
233
+ spec = tokens[first:last + 1]
234
+ if spec and spec[-1].kind == "string":
235
+ spec = spec[:-1]
236
+ if not spec:
237
+ continue
238
+ if spec[0].text == "*" or len(spec) == 1 or spec[1].text == ".":
239
+ found.append((None, _type_ref(spec)))
240
+ continue
241
+ names, index = [spec[0].text], 1
242
+ while index + 1 < len(spec) and spec[index].text == "," and spec[index + 1].kind == "ident":
243
+ names.append(spec[index + 1].text)
244
+ index += 2
245
+ found.append((names, _type_ref(spec[index:])))
246
+ return found
247
+
248
+
249
+ def _specs(tokens, pairs, start, end, docs, text):
250
+ """The type, const or var specs of one declaration, each with its lines and doc comment; a spec of a parenthesized
251
+ declaration also carries the declaration's own doc comment."""
252
+ keyword = tokens[start].text
253
+ group_doc = docs.get(tokens[start].line)
254
+ index = start + 1
255
+ grouped = index <= end and tokens[index].text == "("
256
+ if grouped:
257
+ ranges = _ranges(tokens, pairs, index, pairs.get(index, end))
258
+ else:
259
+ ranges = [(index, end)] if index <= end else []
260
+ found = []
261
+ for spec, spec_end in ranges:
262
+ if tokens[spec].kind != "ident":
263
+ continue
264
+ own = docs.get(tokens[spec].line) if grouped else group_doc
265
+ item = {"start": tokens[spec].line, "end": tokens[spec_end].end_line, "doc": own, "grouped": grouped,
266
+ "group_doc": group_doc if grouped else None, "block": tokens[start].line if grouped else None}
267
+ if keyword == "type":
268
+ after = spec + 1
269
+ if after <= spec_end and tokens[after].text == "[" and after + 1 <= spec_end \
270
+ and tokens[after + 1].kind == "ident" and tokens[pairs.get(after, after) - 1].kind == "ident":
271
+ after = pairs.get(after, after) + 1
272
+ if after <= spec_end and tokens[after].text == "=":
273
+ after += 1
274
+ head = text[tokens[spec].start:tokens[spec_end].end].split("{")[0]
275
+ struct = after + 1 <= spec_end and tokens[after].text == "struct" and tokens[after + 1].text == "{"
276
+ found.append({**item, "name": tokens[spec].text, "signature": "type " + " ".join(head.split()),
277
+ "fields": _fields(tokens, pairs, after + 1) if struct else [],
278
+ "kind": "interface" if after <= spec_end and tokens[after].text == "interface" else "type"})
279
+ else:
280
+ names, index = [tokens[spec].text], spec + 1
281
+ while index + 1 <= spec_end and tokens[index].text == "," and tokens[index + 1].kind == "ident":
282
+ names.append(tokens[index + 1].text)
283
+ index += 2
284
+ found.append({**item, "name": names[0], "names": names, "kind": keyword, "signature": ""})
285
+ return found
286
+
287
+
288
+ def _parse(text):
289
+ """Package name, top-level declarations and the token stream of one Go file."""
290
+ tokens, comments = _lex(text)
291
+ pairs = _pairs(tokens)
292
+ opens = {close: open_ for open_, close in pairs.items()}
293
+ docs = _lead_docs(tokens, comments)
294
+ package, found, index = None, [], 0
295
+ while index < len(tokens):
296
+ token = tokens[index]
297
+ end = _end(tokens, pairs, index, len(tokens))
298
+ if token.kind == "ident" and token.text == "package" and index + 1 < len(tokens):
299
+ package = tokens[index + 1].text
300
+ elif token.kind == "ident" and token.text == "func":
301
+ item = _function(tokens, pairs, opens, index, end, text)
302
+ if item:
303
+ found.append({**item, "doc": docs.get(token.line)})
304
+ elif token.kind == "ident" and token.text in ("type", "const", "var"):
305
+ found.extend(_specs(tokens, pairs, index, end, docs, text))
306
+ index = end + 1
307
+ return package, found, tokens, pairs
308
+
309
+
310
+ def declarations(text):
311
+ """Package name and top-level declarations of one Go file: functions, methods, types, consts and vars, each with
312
+ name, kind, start and end lines, doc comment text (None when there is none) and signature; methods with their
313
+ receiver type, const/var specs with all their names and the line of the parenthesized declaration they sit in
314
+ ("block", None outside one)."""
315
+ return _parse(text)[:2]
17
316
 
18
317
 
19
318
  def _module_roots(sources):
@@ -38,14 +337,24 @@ def _enclosing_module(folder, roots):
38
337
 
39
338
 
40
339
  def _imports(text):
41
- """Import paths of one Go file (comments already blanked)."""
42
- found = [match.group(1) for match in _IMPORT_ONE.finditer(text)]
340
+ """(alias or None, import path) of each import of one Go file (comments already blanked)."""
341
+ found = [((match.group(1) or "").strip() or None, match.group(2)) for match in _IMPORT_ONE.finditer(text)]
43
342
  for block in _IMPORT_BLOCK.finditer(text):
44
- found.extend(spec.group(1) for spec in _IMPORT_SPEC.finditer(block.group(1)))
343
+ found.extend(((spec.group(1) or "").strip() or None, spec.group(2))
344
+ for spec in _IMPORT_SPEC.finditer(block.group(1)))
45
345
  return found
46
346
 
47
347
 
48
- def analyze(sources):
348
+ def _default_alias(spec):
349
+ parts = spec.split("/")
350
+ return parts[-2] if len(parts) > 1 and _VERSION_SUFFIX.match(parts[-1]) else parts[-1]
351
+
352
+
353
+ def analyze(sources, limits=None):
354
+ """Imports, symbols (functions, methods, types) and resolved calls of the Go files in sources (path -> text).
355
+ limits caps symbols and edges ({'symbols': n, 'edges': n}); anything left out sets 'truncated'."""
356
+ limits = limits or {}
357
+ max_symbols, max_edges = limits.get("symbols", float("inf")), limits.get("edges", float("inf"))
49
358
  go_files = {path: text for path, text in sources.items() if path.endswith(".go")}
50
359
  parsed = [path for path in sources if path.endswith(".go") or posixpath.basename(path) == "go.mod"]
51
360
  if not go_files:
@@ -65,13 +374,15 @@ def analyze(sources):
65
374
  candidates = [d for d in packages if d and (spec == d or spec.endswith("/" + d))]
66
375
  return max(candidates, key=len, default=None)
67
376
 
68
- dependencies = []
377
+ dependencies, files = [], {}
69
378
  for path, text in sorted(go_files.items()):
70
379
  bare = _COMMENT.sub(lambda m: re.sub(r"[^\n]", " ", m.group(0)), text)
71
- for spec in _imports(bare):
380
+ imports = []
381
+ for alias, spec in _imports(bare):
72
382
  folder = package_dir(path, spec)
73
383
  line = bare[:bare.find(f'"{spec}"')].count("\n") + 1
74
384
  targets = [t for t in packages.get(folder, ()) if t != path] if folder is not None else []
385
+ imports.append((alias, spec, folder if targets else None))
75
386
  if not targets:
76
387
  dependencies.append({"source": path, "target": None, "specifier": spec, "line": line, "kind": "import",
77
388
  "resolution": "external"})
@@ -82,4 +393,207 @@ def analyze(sources):
82
393
  dependencies.extend({"source": path, "target": target, "specifier": posixpath.dirname(path) or ".",
83
394
  "line": 1, "kind": "package", "resolution": "resolved"}
84
395
  for target in packages.get(posixpath.dirname(path), ()))
85
- return {"dependencies": dependencies, "parsed": parsed}
396
+ package, found, tokens, pairs = _parse(text)
397
+ files[path] = {"package": package, "decls": found, "tokens": tokens, "pairs": pairs, "imports": imports}
398
+ graph = _symbols_and_calls(files, max_symbols, max_edges)
399
+ return {"dependencies": dependencies, "parsed": parsed, **graph}
400
+
401
+
402
+
403
+
404
+ def _symbols_and_calls(files, max_symbols, max_edges):
405
+ """Graph symbols of every function, method and type, then the calls each function body makes."""
406
+ dir_package = {}
407
+ for path, info in files.items():
408
+ if not path.endswith("_test.go") and info["package"]:
409
+ dir_package.setdefault(posixpath.dirname(path), info["package"])
410
+
411
+ def key_of(path):
412
+ return posixpath.dirname(path), files[path]["package"]
413
+
414
+ imports_of = {}
415
+ for path, info in files.items():
416
+ imports_of[path] = {}
417
+ for alias, spec, folder in info["imports"]:
418
+ if alias != "_":
419
+ target = (folder, dir_package.get(folder)) if folder is not None else None
420
+ imports_of[path][alias or (target[1] if target and target[1] else _default_alias(spec))] = target
421
+
422
+ def resolve_type(path, reference):
423
+ if not reference or reference[1] in _BUILTINS:
424
+ return None
425
+ alias, name = reference
426
+ if alias is None:
427
+ return key_of(path), name
428
+ target = imports_of[path].get(alias)
429
+ return (target, name) if target else None
430
+
431
+ index, results, symbols, calls, unresolved, truncated = {}, {}, [], [], [], False
432
+ for path, info in files.items():
433
+ tables = index.setdefault(key_of(path), {"funcs": {}, "methods": {}, "types": {}, "fields": {},
434
+ "embedded": {}})
435
+ seen = set()
436
+ for decl in info["decls"]:
437
+ if decl["kind"] not in ("function", "method", "type", "interface"):
438
+ continue
439
+ for names, reference in decl.get("fields", ()):
440
+ found = resolve_type(path, reference)
441
+ if names is None and found:
442
+ tables["embedded"].setdefault(decl["name"], []).append(found)
443
+ tables["fields"][(decl["name"], found[1])] = found
444
+ for name in names or ():
445
+ tables["fields"][(decl["name"], name)] = found
446
+ name = f"{decl['receiver']}.{decl['name']}" if decl["kind"] == "method" else decl["name"]
447
+ symbol_id = f"{path}::{name}"
448
+ if decl["name"] in ("init", "_") or symbol_id in seen:
449
+ symbol_id = f"{symbol_id}@{decl['start']}"
450
+ seen.add(symbol_id)
451
+ if len(symbols) >= max_symbols:
452
+ truncated = True
453
+ continue
454
+ symbol = {"id": symbol_id, "path": path, "name": name, "kind": decl["kind"], "language": "go",
455
+ "start_line": decl["start"], "end_line": decl["end"]}
456
+ symbols.append(symbol)
457
+ decl["symbol"] = symbol
458
+ if decl.get("result"):
459
+ results[symbol_id] = resolve_type(path, decl["result"])
460
+ if decl["kind"] == "method":
461
+ tables["methods"].setdefault((decl["receiver"], decl["name"]), symbol)
462
+ elif decl["kind"] == "function" and decl["name"] not in ("init", "_"):
463
+ tables["funcs"].setdefault(decl["name"], symbol)
464
+ else:
465
+ tables["types"].setdefault(decl["name"], symbol)
466
+
467
+ def member(owner, name, table, depth=0):
468
+ """Method or field `name` of the type `owner`, looked up through its embedded fields as Go promotes them."""
469
+ tables = index.get(owner[0]) if owner and depth < 4 else None
470
+ if not tables:
471
+ return None
472
+ if (owner[1], name) in tables[table]:
473
+ return tables[table][(owner[1], name)]
474
+ return next((hit for inner in tables["embedded"].get(owner[1], ())
475
+ if (hit := member(inner, name, table, depth + 1)) is not None), None)
476
+
477
+ for path, info in files.items():
478
+ for decl in info["decls"]:
479
+ if decl["kind"] in ("function", "method") and decl.get("body") and decl.get("symbol"):
480
+ for call in _body_calls(path, info["tokens"], decl, index, imports_of[path], key_of(path), results,
481
+ resolve_type, member):
482
+ if call["kind"] == "reference":
483
+ if len(calls) < max_edges:
484
+ calls.append({"source": decl["symbol"]["id"], "target": call["target"]["id"],
485
+ "line": call["line"], "kind": "reference", "resolution": "static"})
486
+ else:
487
+ truncated = True
488
+ continue
489
+ if call["target"] is None:
490
+ if len(unresolved) < 1000:
491
+ unresolved.append({"path": path, "line": call["line"], "expression": call["expression"],
492
+ "reason": _UNRESOLVED})
493
+ elif len(calls) < max_edges:
494
+ calls.append({"source": decl["symbol"]["id"], "target": call["target"]["id"],
495
+ "line": call["line"], "kind": "call", "resolution": "static"})
496
+ else:
497
+ truncated = True
498
+ return {"symbols": symbols, "calls": calls, "unresolved": unresolved, "truncated": truncated}
499
+
500
+
501
+ def _body_calls(path, tokens, decl, index, imported, own, results, resolve_type, member):
502
+ """Calls in one function body, {'target': symbol or None, 'expression', 'line', 'kind': 'call'}, and the project
503
+ functions it hands over as values, {'target', 'expression', 'line', 'kind': 'reference'}. Variables get a type from the
504
+ receiver, the parameters, `var x T`, composite literals, known results and field selections, in source order; a
505
+ later declaration of the same name drops it (scopes are not tracked, so a shadowed name stays unresolved)."""
506
+ tables = index[own]
507
+ dots = [index.get(target, {}) for alias, target in imported.items() if alias == "." and target]
508
+ typed = {name: resolve_type(path, reference) for name, reference in decl["parameters"].items()}
509
+ if decl["receiver_var"]:
510
+ typed[decl["receiver_var"]] = (own, decl["receiver"])
511
+ local = set(typed)
512
+ open_, close = decl["body"]
513
+
514
+ def value(chain):
515
+ found = typed.get(chain[0])
516
+ for name in chain[1:]:
517
+ found = member(found, name, "fields")
518
+ return found
519
+
520
+ def callee(chain, name):
521
+ if not chain:
522
+ return tables["funcs"].get(name) or next((d["funcs"][name] for d in dots if name in d.get("funcs", {})),
523
+ None)
524
+ if len(chain) == 1 and chain[0] not in local and imported.get(chain[0]):
525
+ return index.get(imported[chain[0]], {}).get("funcs", {}).get(name)
526
+ return member(value(chain), name, "methods") if chain[0] in typed else None
527
+
528
+ def selection(start):
529
+ chain, end = [tokens[start].text], start + 1
530
+ while end + 1 < close and tokens[end].text == "." and tokens[end + 1].kind == "ident":
531
+ chain.append(tokens[end + 1].text)
532
+ end += 2
533
+ return chain[:-1], chain[-1], end
534
+
535
+ def expression_type(start):
536
+ """Type of the expression starting at token start when it is a composite literal, a call with a known
537
+ result or a field selection; None otherwise."""
538
+ start += tokens[start].text == "&"
539
+ if start >= close or tokens[start].kind != "ident":
540
+ return None
541
+ chain, name, end = selection(start)
542
+ if tokens[end].text == "{" and len(chain) <= 1:
543
+ return resolve_type(path, (chain[0] if chain else None, name))
544
+ if tokens[end].text == "(":
545
+ found = callee(chain, name)
546
+ return results.get(found["id"]) if found else None
547
+ return value(chain + [name]) if chain and chain[0] in typed else None
548
+
549
+ found = []
550
+ for k in range(open_ + 1, close):
551
+ token = tokens[k]
552
+ if token.kind == "op" and token.text == ":=":
553
+ names, back = [], k - 1
554
+ while back > open_ and tokens[back].kind == "ident":
555
+ names.insert(0, tokens[back].text)
556
+ if tokens[back - 1].text != ",":
557
+ break
558
+ back -= 2
559
+ local.update(names)
560
+ reference = expression_type(k + 1) if names else None
561
+ for name in names:
562
+ typed.pop(name, None)
563
+ if reference:
564
+ typed[names[0]] = reference
565
+ elif token.kind == "ident" and token.text == "var" and tokens[k + 1].kind == "ident":
566
+ local.add(tokens[k + 1].text)
567
+ stop = k + 2
568
+ while stop < close and tokens[stop].line == token.line and tokens[stop].text != "=":
569
+ stop += 1
570
+ reference = resolve_type(path, _type_ref(tokens[k + 2:stop])) if stop > k + 2 else \
571
+ expression_type(stop + 1) if stop < close and tokens[stop].text == "=" else None
572
+ typed.pop(tokens[k + 1].text, None)
573
+ if reference:
574
+ typed[tokens[k + 1].text] = reference
575
+ elif token.kind == "ident" and tokens[k + 1].text == "(" and token.text not in _KEYWORDS:
576
+ first = k
577
+ while tokens[first - 1].text == "." and tokens[first - 2].kind == "ident":
578
+ first -= 2
579
+ if tokens[first - 1].text == ".":
580
+ found.append({"target": None, "expression": f"….{token.text}", "line": token.line, "kind": "call"})
581
+ continue
582
+ chain = [tokens[i].text for i in range(first, k, 2)]
583
+ if not chain and (token.text in _BUILTINS or token.text in tables["types"] or token.text in local):
584
+ continue
585
+ if len(chain) == 1 and chain[0] not in local and imported.get(chain[0]) \
586
+ and token.text in index.get(imported[chain[0]], {}).get("types", {}):
587
+ continue
588
+ found.append({"target": callee(chain, token.text), "expression": ".".join(chain + [token.text]),
589
+ "line": token.line, "kind": "call"})
590
+ elif token.kind == "ident" and token.text not in _KEYWORDS and tokens[k - 1].text != "." \
591
+ and tokens[k + 1].text not in (":", "("):
592
+ chain, name, end = selection(k)
593
+ if tokens[end].text == "(" or (not chain and name in local):
594
+ continue
595
+ target = callee(chain, name)
596
+ if target and target.get("kind") in ("function", "method"):
597
+ found.append({"target": target, "expression": ".".join(chain + [name]), "line": token.line,
598
+ "kind": "reference"})
599
+ return found
@@ -1,15 +1,24 @@
1
1
  // Análise estática conservadora de Java a partir de snapshots; nenhum código é executado.
2
2
  import fs from 'node:fs';
3
- import {parse} from './web_adapters/node/node_modules/java-parser/src/index.js';
3
+ import * as nodeModule from 'node:module';
4
+ const modules=new URL('./web_adapters/node/node_modules/',import.meta.url);
5
+ // java-parser reaches chevrotain's sources and lodash one file per function (744 module files, ~0.9 s of startup on
6
+ // every run); the package's own bundle and lodash's single file load the same code from 3 files.
7
+ if(nodeModule.registerHooks){const bundle=new URL('chevrotain/lib/chevrotain.mjs',modules).href,lodash=new URL('lodash/lodash.js',modules).href;
8
+ nodeModule.registerHooks({resolve(specifier,context,next){if(specifier==='chevrotain')return{url:bundle,shortCircuit:true};const fn=/^lodash(?:-es)?\/(\w+)\.js$/.exec(specifier);return fn?{url:'lodash-function:'+fn[1],shortCircuit:true}:next(specifier,context)},
9
+ load(url,context,next){return url.startsWith('lodash-function:')?{format:'module',source:`import _ from ${JSON.stringify(lodash)};export default _.${url.slice(16)};`,shortCircuit:true}:next(url,context)}})}
10
+ const {parse}=await import(new URL('java-parser/src/index.js',modules).href);
4
11
  const input=JSON.parse(fs.readFileSync(0,'utf8'));
5
- const symbols=[],dependencies=[],calls=[],unresolved=[],diagnostics=[],units=[],classes=new Map(),methodNodes=new Map();
6
- const MAX_SYMBOLS=8000,MAX_EDGES=20000;
7
- function children(n){return n?.children?Object.values(n.children).flat().filter(x=>x.children):[]}
12
+ const symbols=[],dependencies=[],calls=[],unresolved=[],diagnostics=[],units=[],classes=new Map(),methodNodes=new Map(),kids=new WeakMap();
13
+ const {symbols:MAX_SYMBOLS,edges:MAX_EDGES}=input.limits;let dropped=false;
14
+ function room(list,max){if(list.length<max)return true;dropped=true;return false}
15
+ const NO_CHILDREN=Object.freeze([]);
16
+ function children(n){const groups=n?.children;if(!groups)return NO_CHILDREN;let list=kids.get(n);if(list)return list;list=[];for(const key in groups)for(const child of groups[key])if(child.children)list.push(child);kids.set(n,list);return list}
8
17
  function all(n,name,stop=false){const result=[];function visit(node){if(node?.name===name){result.push(node);if(stop)return}for(const child of children(node))visit(child)}visit(n);return result}
9
18
  function first(n,name){return all(n,name,true)[0]}
10
19
  function identifiers(n){const out=[];function visit(node){if(node?.children)for(const [key,items] of Object.entries(node.children))for(const child of items){if(child.children)visit(child);else if(key==='Identifier')out.push(child)}}visit(n);return out.sort((a,b)=>a.startOffset-b.startOffset)}
11
20
  function text(u,n){return n?.location?u.text.slice(n.location.startOffset,n.location.endOffset+1):''}
12
- function addSymbol(u,n,name,kind){if(symbols.length>=MAX_SYMBOLS)return null;const s={id:u.path+'::'+name+'@'+n.location.startLine+':'+n.location.startOffset,path:u.path,name,kind,language:'java',start_line:n.location.startLine,end_line:n.location.endLine};symbols.push(s);return s}
21
+ function addSymbol(u,n,name,kind){if(!room(symbols,MAX_SYMBOLS))return null;const s={id:u.path+'::'+name+'@'+n.location.startLine+':'+n.location.startOffset,path:u.path,name,kind,language:'java',start_line:n.location.startLine,end_line:n.location.endLine};symbols.push(s);return s}
13
22
  const classKinds=new Set(['normalClassDeclaration','normalInterfaceDeclaration','enumDeclaration','recordDeclaration']);
14
23
  for(const file of input.files){try{const tree=parse(file.text),pkg=first(tree,'packageDeclaration'),unit={...file,tree,package:pkg?identifiers(pkg).map(t=>t.image).join('.'): '',imports:[],classes:[]};
15
24
  for(const n of all(tree,'importDeclaration')){const raw=text(unit,n).replace(/^import\s+/,'').replace(/;\s*$/,'').trim(),stat=raw.startsWith('static '),spec=(stat?raw.slice(7):raw).replace(/\s+/g,'');unit.imports.push({spec,static:stat,line:n.location.startLine})}
@@ -29,18 +38,18 @@ for(const unit of units){
29
38
  function walk(n,cls=null,method=null,vars=new Map(),owner=null){if(classKinds.has(n.name))cls=unit.classes.find(c=>c.node===n)||cls;if(cls&&n.name==='classBody'&&n!==cls.body)return;
30
39
  if(methodNodes.has(n)){method=methodNodes.get(n);vars=new Map(cls.fields);collectVariables(unit,n,vars);owner=method.symbol}
31
40
  if(n.name==='lambdaExpression'){vars=new Map(vars);for(const param of identifiers(n.children.lambdaParameters?.[0]))vars.set(param.image,null);owner=addSymbol(unit,n,(cls?.local||'')+'.lambda@'+n.location.startLine,'function')||owner}
32
- if(cls&&['unannType','classType','interfaceType'].includes(n.name)){const type=resolveType(unit,text(unit,n),cls);if(type&&type.unit.path!==unit.path&&dependencies.length<MAX_EDGES)dependencies.push({source:unit.path,target:type.unit.path,specifier:type.fqn,line:n.location.startLine,kind:'java_type',resolution:'resolved'})}
41
+ if(cls&&['unannType','classType','interfaceType'].includes(n.name)){const type=resolveType(unit,text(unit,n),cls);if(type&&type.unit.path!==unit.path&&room(dependencies,MAX_EDGES))dependencies.push({source:unit.path,target:type.unit.path,specifier:type.fqn,line:n.location.startLine,kind:'java_type',resolution:'resolved'})}
33
42
  if(cls&&n.name==='primary'){
34
43
  const prefix=n.children.primaryPrefix?.[0],prefixText=text(unit,prefix).trim(),suffixes=n.children.primarySuffix||[];let chain=prefixText,fluent=false;
35
44
  const newMatch=/^new\s+([\w.$]+)/.exec(prefixText);let constructed=newMatch?resolveType(unit,newMatch[1],cls):null;
36
- const link=(type,line)=>{if(type&&type.unit.path!==unit.path&&dependencies.length<MAX_EDGES)dependencies.push({source:unit.path,target:type.unit.path,specifier:type.fqn,line,kind:'java_type',resolution:'resolved'})};
37
- if(constructed&&calls.length<MAX_EDGES)calls.push({source:(owner||cls.symbol).id,target:constructed.symbol.id,line:n.location.startLine,kind:'construct',resolution:'static'});
45
+ const link=(type,line)=>{if(type&&type.unit.path!==unit.path&&room(dependencies,MAX_EDGES))dependencies.push({source:unit.path,target:type.unit.path,specifier:type.fqn,line,kind:'java_type',resolution:'resolved'})};
46
+ if(constructed&&room(calls,MAX_EDGES))calls.push({source:(owner||cls.symbol).id,target:constructed.symbol.id,line:n.location.startLine,kind:'construct',resolution:'static'});
38
47
  link(constructed,n.location.startLine);
39
48
  const staticHead=/^([A-Z][\w$]*)\./.exec(prefixText);if(staticHead&&!vars.has(staticHead[1]))link(resolveType(unit,staticHead[1],cls),n.location.startLine);
40
49
  for(const suffix of suffixes){const invoke=suffix.children.methodInvocationSuffix?.[0];if(!invoke){chain+=text(unit,suffix);continue}const args=invoke.children.argumentList?.[0],arity=args?(args.children.expression||[]).length:0;const match=/^(?:([\w.$]+)\.)?([\w$]+)$/.exec(chain);let target=null;
41
50
  if(match&&!fluent){const receiver=match[1],name=match[2];let type=null;if(!receiver||receiver==='this')type=cls;else if(receiver==='super')type=resolveType(unit,cls.extends,cls);else if(receiver.startsWith('this.'))type=resolveType(unit,cls.fields.get(receiver.slice(5))||'',cls);else if(vars.has(receiver))type=resolveType(unit,vars.get(receiver)||'',cls);else{type=resolveType(unit,receiver,cls);link(type,invoke.location.startLine)}target=lookupMethod(type,name,arity);if(!target&&!receiver){const candidates=unit.imports.filter(i=>i.static&&(i.spec.endsWith('.'+name)||i.spec.endsWith('.*'))).map(i=>lookupMethod(classes.get(i.spec.split('.').slice(0,-1).join('.')),name,arity)).filter(Boolean);if(candidates.length===1)target=candidates[0]}}
42
51
  if(constructed&&!fluent){const methodName=/\.([\w$]+)$/.exec(chain)?.[1];if(methodName)target=lookupMethod(constructed,methodName,arity)}
43
- if(target&&calls.length<MAX_EDGES)calls.push({source:(owner||cls.symbol).id,target:target.id,line:invoke.location.startLine,kind:'call',resolution:'static'});
52
+ if(target&&room(calls,MAX_EDGES))calls.push({source:(owner||cls.symbol).id,target:target.id,line:invoke.location.startLine,kind:'call',resolution:'static'});
44
53
  else if(unresolved.length<1000)unresolved.push({path:unit.path,line:invoke.location.startLine,expression:chain.slice(0,140),reason:'External/dynamic type, interface without implementation, or ambiguous overload in the Java snapshot.'});
45
54
  fluent=true;chain+='<result>';
46
55
  }
@@ -49,4 +58,4 @@ for(const unit of units){
49
58
  }walk(unit.tree)
50
59
  }
51
60
  const ids=new Set(symbols.map(s=>s.id));
52
- process.stdout.write(JSON.stringify({symbols,dependencies:dependencies.slice(0,MAX_EDGES),calls:calls.filter(c=>ids.has(c.source)&&ids.has(c.target)),unresolved,diagnostics,parsed:units.map(u=>u.path),truncated:symbols.length>=MAX_SYMBOLS||dependencies.length>MAX_EDGES||calls.length>=MAX_EDGES}));
61
+ process.stdout.write(JSON.stringify({symbols,dependencies:dependencies.slice(0,MAX_EDGES),calls:calls.filter(c=>ids.has(c.source)&&ids.has(c.target)),unresolved,diagnostics,parsed:units.map(u=>u.path),truncated:dropped||dependencies.length>MAX_EDGES}));