MCPcopy Create free account
hub / github.com/Graphify-Labs/graphify / _extract_js_rationale

Function _extract_js_rationale

graphify/extract.py:5902–5976  ·  view source on GitHub ↗

Post-pass: extract rationale comments and doc references from JS/TS source. Mutates result in-place by appending to result['nodes'] and result['edges'].

(path: Path, result: dict)

Source from the content-addressed store, hash-verified

5900
5901
5902def _extract_js_rationale(path: Path, result: dict) -> None:
5903 """Post-pass: extract rationale comments and doc references from JS/TS source.
5904 Mutates result in-place by appending to result['nodes'] and result['edges'].
5905 """
5906 try:
5907 source_text = path.read_text(encoding="utf-8", errors="replace")
5908 except Exception:
5909 return
5910
5911 stem = _file_stem(path)
5912 str_path = str(path)
5913 nodes = result["nodes"]
5914 edges = result["edges"]
5915 seen_ids = {n["id"] for n in nodes}
5916 file_nid = _make_id(str(path))
5917 seen_doc_refs: set[str] = set()
5918
5919 def _add_rationale(text: str, line: int) -> None:
5920 label = text[:80].replace("\r\n", " ").replace("\r", " ").replace("\n", " ").strip()
5921 rid = _make_id(stem, "rationale", str(line))
5922 if rid not in seen_ids:
5923 seen_ids.add(rid)
5924 nodes.append({
5925 "id": rid,
5926 "label": label,
5927 "file_type": "rationale",
5928 "source_file": str_path,
5929 "source_location": f"L{line}",
5930 })
5931 edges.append({
5932 "source": rid,
5933 "target": file_nid,
5934 "relation": "rationale_for",
5935 "confidence": "EXTRACTED",
5936 "source_file": str_path,
5937 "source_location": f"L{line}",
5938 "weight": 1.0,
5939 })
5940
5941 def _add_doc_ref(token: str, line: int) -> None:
5942 # Normalize "adr 11" / "ADR-0011" spellings to a canonical "ADR-0011"
5943 # style label so references to the same document collapse to one node.
5944 kind, num = re.match(r"([A-Za-z]+)[- ]?(\d+)", token).groups()
5945 kind = kind.upper()
5946 label = f"{kind}-{num.zfill(4)}" if kind == "ADR" else f"{kind}-{num}"
5947 if label in seen_doc_refs:
5948 return
5949 seen_doc_refs.add(label)
5950 rid = _make_id("docref", label)
5951 if rid not in seen_ids:
5952 seen_ids.add(rid)
5953 nodes.append({
5954 "id": rid,
5955 "label": label,
5956 "file_type": "doc_ref",
5957 "source_file": str_path,
5958 "source_location": f"L{line}",
5959 })

Callers 1

extract_jsFunction · 0.85

Calls 4

_file_stemFunction · 0.90
_make_idFunction · 0.90
_add_rationaleFunction · 0.85
_add_doc_refFunction · 0.85

Tested by

no test coverage detected