Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
feat(salesforce): link flow references (#4701)
ZhangDTdfcb494b0b38
diff --git a/packages/core/src/repowise/core/ingestion/framework_edges/salesforce_metadata.py b/packages/core/src/repowise/core/ingestion/framework_edges/salesforce_metadata.pyindex fed3fcbd0..cab47ab3a 100644--- a/packages/core/src/repowise/core/ingestion/framework_edges/salesforce_metadata.py+++ b/packages/core/src/repowise/core/ingestion/framework_edges/salesforce_metadata.py@@ -2,8 +2,9 @@A flow links to the Apex classes its actions name (and their one``@InvocableMethod``), its subflows, its screen components and its Apex-defined-types. Apex ``Flow.Interview`` references and LWC ``<lightning-flow>`` tags-with a literal name link back to the flow.+types. Apex ``Flow.Interview`` references, LWC ``<lightning-flow>`` tags, Aura+``startFlow`` calls and Visualforce interviews with literal names link back to+the flow.Flexipages, quick actions, tabs, email services and Experience sites are readfrom disk (as ``android_manifest`` is) for liveness only: each file they name is@@ -45,6 +46,15 @@ if TYPE_CHECKING:_FLOW = "salesforce_flow"_XML_COMMENT_RE = re.compile(r"<!--.*?(?:-->|\Z)", re.DOTALL)+_AURA_FLOW_TAG_RE = re.compile(r"<lightning:flow\b", re.IGNORECASE)+_AURA_START_FLOW_RE = re.compile(r"\.\s*startFlow\s*\(\s*([\"'])([A-Za-z]\w*)\1")+_JS_COMMENT_OR_STRING_RE = re.compile(+ r"//[^\n]*|/\*.*?(?:\*/|\Z)|'(?:[^'\\\n]|\\.)*'|\"(?:[^\"\\\n]|\\.)*\"|`(?:[^`\\]|\\.)*`",+ re.DOTALL,+)+_VISUALFORCE_FLOW_RE = re.compile(+ r"<flow:interview\b[^>]*?\bname\s*=\s*[\"']([A-Za-z]\w*)[\"']", re.IGNORECASE+)# An Apex comment, or a string literal kept as-is so a `//` inside one is not a comment._APEX_COMMENT_OR_STRING_RE = re.compile(r"//[^\n]*|/\*.*?(?:\*/|\Z)|'(?:[^'\\\n]|\\.)*'", re.DOTALL@@ -71,6 +81,15 @@ _COMPONENT_TAGS = r"componentName|lightningComponent|lightningWebComponent|lwcCo_TAG_VALUE_RE = re.compile(rf"<({_COMPONENT_TAGS}|flowDefinition|apexClass)>\s*([^<\s]+)\s*</\1>")+_FLEXIPAGE_FLOW_RE = re.compile(+ r"<componentInstanceProperties\b[^>]*>"+ r"(?:(?!</componentInstanceProperties>).)*?"+ r"<name>\s*flowName\s*</name>"+ r"(?:(?!</componentInstanceProperties>).)*?"+ r"<value>\s*([^<\s]+)\s*</value>"+ r"(?:(?!</componentInstanceProperties>).)*?</componentInstanceProperties>",+ re.DOTALL,+)_PLACEMENT_SUFFIXES = (".flexipage-meta.xml", ".quickAction-meta.xml", ".tab-meta.xml")# Metadata API names are bare suffixes (``.tab`` is also tab-separated data),# so they count only in their metadata directory.@@ -328,6 +347,57 @@ def _lwc_flow_refs(graph: nx.DiGraph, idx: _Index, ctx: ResolverContext) -> int:return count+def _aura_flow_refs(graph: nx.DiGraph, idx: _Index, ctx: ResolverContext) -> int:+ """Literal ``startFlow`` calls in Aura controllers whose bundle hosts ``lightning:flow``."""+ source_map: dict[str, bytes] = getattr(ctx, "source_map", None) or {}+ bundles: dict[str, list[str]] = {}+ for path, parsed in idx.parsed_files.items():+ p = PurePosixPath(path)+ if parsed.file_info.language == "aura" and len(p.parts) >= 3 and p.parts[-3] == "aura":+ bundles.setdefault(str(p.parent), []).append(path)++ count = 0+ for bundle_dir, paths in bundles.items():+ bundle = PurePosixPath(bundle_dir).name+ markup = [p for p in paths if idx.parsed_files[p].file_info.language == "aura"]+ if not any(+ _AURA_FLOW_TAG_RE.search(_mask(source_text(p, idx.parsed_files[p], source_map), _XML_COMMENT_RE))+ for p in markup+ ):+ continue+ controller = f"{bundle_dir}/{bundle}Controller.js"+ parsed = idx.parsed_files.get(controller)+ if parsed is None:+ continue+ raw = source_text(controller, parsed, source_map)+ literals = _mask(raw, _JS_COMMENT_OR_STRING_RE, keep="'\"` ")+ code = _mask(raw, _JS_COMMENT_OR_STRING_RE)+ for m in _AURA_START_FLOW_RE.finditer(literals):+ if code[m.start()] == " ":+ continue+ target = idx.flow(m.group(2), controller)+ if target is not None:+ count += _link(+ graph, controller, target, [], f"{controller}::__module__", idx.symbol_of(target)+ )+ return count+++def _visualforce_flow_refs(graph: nx.DiGraph, idx: _Index, ctx: ResolverContext) -> int:+ """Literal ``<flow:interview name="Name">`` references in Visualforce markup."""+ source_map: dict[str, bytes] = getattr(ctx, "source_map", None) or {}+ count = 0+ for path, parsed in idx.parsed_files.items():+ if parsed.file_info.language != "visualforce":+ continue+ text = _mask(source_text(path, parsed, source_map, "utf-8-sig"), _XML_COMMENT_RE)+ for m in _VISUALFORCE_FLOW_RE.finditer(text):+ target = idx.flow(m.group(1), path)+ if target is not None:+ count += _link(graph, path, target, [], f"{path}::__module__", idx.symbol_of(target))+ return count++# -- liveness metadata -------------------------------------------------------@@ -350,6 +420,7 @@ def _placed_names(rel: str, text: str) -> list[tuple[str, str]]:return [("component", m.group(1)) for m in _EXPERIENCE_DEFINITION_RE.finditer(text)]text = _mask(text, _XML_COMMENT_RE)out: list[tuple[str, str]] = []+ out.extend(("flow", m.group(1)) for m in _FLEXIPAGE_FLOW_RE.finditer(text))for m in _TAG_VALUE_RE.finditer(text):tag, value = m.group(1), m.group(2)if tag == "apexClass":@@ -424,6 +495,8 @@ class _SalesforceMetadataHandler:_flow_edges(graph, idx, ctx)+ _apex_flow_refs(graph, idx, ctx)+ _lwc_flow_refs(graph, idx, ctx)+ + _aura_flow_refs(graph, idx, ctx)+ + _visualforce_flow_refs(graph, idx, ctx)+ _liveness_edges(graph, idx, ctx, path_set))diff --git a/tests/unit/ingestion/test_salesforce_flow_edges.py b/tests/unit/ingestion/test_salesforce_flow_edges.pyindex 65c6aadba..eb7040596 100644--- a/tests/unit/ingestion/test_salesforce_flow_edges.py+++ b/tests/unit/ingestion/test_salesforce_flow_edges.py@@ -45,6 +45,9 @@ _APP = {"sfdx-project.json": '{"packageDirectories": [{"path": "force-app"}], "namespace": "acme"}\n',f"{_D}/flows/Main_Flow.flow-meta.xml": _MAIN_FLOW,f"{_D}/flows/Child_Flow.flow-meta.xml": _flow(" <processType>AutoLaunchedFlow</processType>\n"),+ f"{_D}/flows/Onboard.flow-meta.xml": _flow(" <processType>AutoLaunchedFlow</processType>\n"),+ f"{_D}/flows/With_Inputs.flow-meta.xml": _flow(" <processType>AutoLaunchedFlow</processType>\n"),+ f"{_D}/flows/Vf_Flow.flow-meta.xml": _flow(" <processType>AutoLaunchedFlow</processType>\n"),f"{_D}/classes/Mailer.cls": ("public with sharing class Mailer {\n"" @InvocableMethod(label='Send')\n"@@ -112,10 +115,31 @@ _APP = {'<aura:component implements="lightning:availableForFlowScreens"/>\n'),f"{_D}/aura/markupAura/markupAuraController.js": "({ init: function (c, e, h) {} })\n",+ f"{_D}/aura/flowHost/flowHost.cmp": (+ '<aura:component><lightning:flow aura:id="flow"/></aura:component>\n'+ ),+ f"{_D}/aura/flowHost/flowHostController.js": (+ "({ start: function(cmp) {\n"+ ' cmp.find("flow").startFlow("Onboard");\n'+ ' cmp.find("flow").startFlow("With_Inputs", inputVariables);\n'+ ' // cmp.find("flow").startFlow("Child_Flow");\n'+ " var example = '.startFlow(\"Child_Flow\")';\n"+ ' cmp.find("flow").startFlow(dynamicName);\n'+ ' cmp.find("flow").startFlow("Missing_Flow");\n'+ "} })\n"+ ),+ f"{_D}/pages/Intake.page": (+ '<apex:page><flow:interview name="Vf_Flow"/>'+ '<!-- <flow:interview name="Child_Flow"/> -->\n</apex:page>\n'+ ),f"{_D}/flexipages/Home.flexipage-meta.xml": (f"<FlexiPage {_NS}><flexiPageRegions><itemInstances><componentInstance>""<componentName>acme:onPage</componentName></componentInstance>""<componentInstance><componentName>force:detailPanel</componentName></componentInstance>"+ "<componentInstanceProperties><name>flowName</name><value>Child_Flow</value>"+ "</componentInstanceProperties>"+ "<!-- <componentInstanceProperties><name>flowName</name><value>Onboard</value>"+ "</componentInstanceProperties> -->""</itemInstances></flexiPageRegions></FlexiPage>\n"),f"{_D}/tabs/On_Tab.tab-meta.xml": f"<CustomTab {_NS}><lwcComponent>onTab</lwcComponent></CustomTab>\n",@@ -220,6 +244,24 @@ def test_flows_are_symbols_linked_to_apex_flows_and_components(tmp_path: Path) -targets = {t for _, t in graph.out_edges(f"{lwc}/host/host.html")}assert main not in targets+ # Aura and Visualforce launch flows from literal references; comments and+ # dynamic Aura names do not add edges.+ aura_controller = f"{_D}/aura/flowHost/flowHostController.js"+ onboard = f"{_D}/flows/Onboard.flow-meta.xml"+ with_inputs = f"{_D}/flows/With_Inputs.flow-meta.xml"+ vf_page = f"{_D}/pages/Intake.page"+ vf_flow = f"{_D}/flows/Vf_Flow.flow-meta.xml"+ assert edge(aura_controller, onboard) == "framework"+ assert edge(aura_controller, with_inputs) == "framework"+ assert edge(f"{aura_controller}::__module__", f"{onboard}::Onboard") == "framework_binds"+ assert edge(vf_page, vf_flow) == "framework"+ assert edge(f"{vf_page}::__module__", f"{vf_flow}::Vf_Flow") == "framework_binds"+ assert not graph.has_edge(aura_controller, child)+ assert not graph.has_edge(vf_page, child)+ assert {t for _, t in graph.out_edges(aura_controller) if "/flows/" in t} == {+ onboard, with_inputs+ }+# Placement metadata anchors what it names, and nothing it merely grants.anchored = {t for _, t in graph.out_edges("framework:salesforce")}assert anchored == {@@ -228,6 +270,7 @@ def test_flows_are_symbols_linked_to_apex_flows_and_components(tmp_path: Path) -f"{lwc}/onAction/onAction.js",f"{lwc}/onSite/onSite.js",f"{cls}/InboundHandler.cls",+ child,}assert graph.nodes[f"{lwc}/onPage/onPage.js"]["is_entry_point"] is True