"""Repository facts use the same names, ranges and calls in every consumer.""" import pytest from openultrasast import source from openultrasast.semantic import cst from openultrasast.semantic.extra import grammar_for SAMPLES = [ ("python", "app.py", "def run(x):\n return x\n", (1, 2)), ("javascript", "app.js", "function run(x) {\n return x;\n}\n", (1, 3)), ("java", "App.java", "class App {\n public int run(int x) {\n return x;\n }\n}\n", (2, 4)), ("go", "app.go", "package app\nfunc (r *Repo) run(x int) int {\n return x\n}\n", (2, 4)), ("ruby", "app.rb", "class App\n def run(x)\n x\n end\nend\n", (2, 4)), ("php", "app.php", " source.MINIFIED_LINE_CHARS calls: list[str] = [] original = source._declares monkeypatch.setattr(source, "_declares", lambda line, name: calls.append(line) or original(line, name)) result = source.ranges("bundle.js", text, "javascript") assert len(result) == 3001 and result[0].name == source.MODULE_FUNCTION assert set(calls) == {""} # the long line itself never reaches the per-function scan assert {r.name for r in result[1:]} == {source.ANON_FUNCTION} def test_generated_reason_names_build_output_only(tmp_path): (tmp_path / "dist").mkdir() (tmp_path / "dist" / "bundle.js").write_text("function a() {}\n") (tmp_path / "app.min.js").write_text("function b() {}\n") (tmp_path / "one.js").write_text("".join(f"function f{i}(){{}}" for i in range(400)) + "\n") (tmp_path / "big.js").write_text("function c() {}\n" + "// padding\n" * 100_000) (tmp_path / "app.js").write_text("function d() {}\n") assert source.generated_reason(tmp_path, "dist/bundle.js") == "excluded_dir" assert source.generated_reason(tmp_path, "app.min.js") == "minified_name" assert source.generated_reason(tmp_path, "one.js") == "long_line" assert source.generated_reason(tmp_path, "big.js") == "too_large" assert source.generated_reason(tmp_path, "app.js") is None assert source.generated_reason(tmp_path, "missing.js") is None def test_facts_keep_only_the_entry_points_in_the_file_or_a_caller_file(tmp_path): from openultrasast.mapping import EntryPointRecord (tmp_path / "app.py").write_text("def run(x):\n return x\n") (tmp_path / "web.py").write_text("from app import run\n\ndef view():\n return run(1)\n") (tmp_path / "far.py").write_text("def other():\n return 2\n") def entry(path): return EntryPointRecord(path, 1, 2, None, path, "route", "public", "external", [], [], "test", "fixture") entries = [entry("app.py"), entry("web.py"), entry("far.py")] facts = source.facts_for(tmp_path, "app.py", 2, entry_points=entries, files=["app.py", "web.py", "far.py"]) assert facts.callers == (("web.py", 4),) assert [e.path for e in facts.entry_points] == ["app.py", "web.py"]