Skip to main content

fallow_types/
trace_chain.rs

1//! Symbol-level call-chain output contracts.
2
3use std::path::PathBuf;
4
5use serde::Serialize;
6
7use crate::semantic::SemanticNamespace;
8use crate::serde_path;
9
10/// Default chain depth when `--depth` is unset.
11pub const DEFAULT_TRACE_DEPTH: u32 = 2;
12
13/// Which directions to walk.
14#[derive(Debug, Clone, Copy)]
15pub struct TraceDirections {
16    /// Walk up to callers.
17    pub callers: bool,
18    /// Walk down to callees.
19    pub callees: bool,
20}
21
22/// The result of a symbol-level call-chain trace. Its own surface (`kind:
23/// "trace"`), NOT folded into the ranked brief.
24#[derive(Debug, Clone, Serialize)]
25#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
26pub struct SymbolChainTrace {
27    /// The file containing the traced symbol (project-root-relative).
28    #[serde(serialize_with = "serde_path::serialize")]
29    pub file: PathBuf,
30    /// The traced symbol name.
31    pub symbol: String,
32    /// Whether the symbol's defining export was found in the graph. When
33    /// `false`, the chains are empty and `reason` explains why.
34    pub symbol_found: bool,
35    /// The chain depth applied to both directions.
36    pub depth: u32,
37    /// Whether this trace is best-effort (always `true`: symbol-level chains are
38    /// labeled best-effort, syntactic per ADR-001).
39    pub best_effort: bool,
40    /// Caller chain hops (UP). Present only when `--callers` was requested.
41    #[serde(default, skip_serializing_if = "Option::is_none")]
42    pub callers: Option<Vec<ChainHop>>,
43    /// Callee chain hops (down) resolved to an import-symbol edge. Present only
44    /// when `--callees` was requested.
45    #[serde(default, skip_serializing_if = "Option::is_none")]
46    pub callees: Option<Vec<ChainHop>>,
47    /// Callees referenced at a call site in the symbol's module that the
48    /// syntactic walk could NOT resolve to an import-symbol edge (locals,
49    /// globals, dynamic dispatch, re-bound callees). Reported, never dropped.
50    /// Present only when `--callees` was requested.
51    #[serde(default, skip_serializing_if = "Option::is_none")]
52    pub unresolved_callees: Option<Vec<UnresolvedCallee>>,
53    /// Set when the name is unresolvable because two different `export *`
54    /// sources of this file supply it. `symbol_found` is `false` in that case
55    /// for the same reason it is false for an unknown name (the file exports
56    /// nothing under it per ECMA-262 ResolveExport), so this field is the only
57    /// thing that separates a barrel mistake from a typo.
58    #[serde(default, skip_serializing_if = "Option::is_none")]
59    pub star_export_ambiguity: Option<StarExportAmbiguity>,
60    /// A human-readable summary of the trace outcome.
61    pub reason: String,
62}
63
64/// The `export *` collision that keeps a name from being exported.
65#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
66#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
67pub struct StarExportAmbiguity {
68    /// Files that each declare a colliding declaration under the traced name
69    /// (project-root-relative), sorted. These are the origins to fix: keep one,
70    /// rename or explicitly re-export the rest.
71    #[serde(serialize_with = "serde_path::serialize_vec")]
72    pub sources: Vec<PathBuf>,
73    /// The namespaces the collision occurs in, type before value. A name can
74    /// collide in type space, value space, or both.
75    pub namespaces: Vec<SemanticNamespace>,
76}
77
78/// One hop in a caller / callee chain.
79#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
80#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
81pub struct ChainHop {
82    /// The file at this hop (project-root-relative). For a caller hop this is
83    /// the importing module; for a callee hop the imported module.
84    #[serde(serialize_with = "serde_path::serialize")]
85    pub file: PathBuf,
86    /// The symbol name as imported across the edge (`default`, `*` for namespace,
87    /// the imported name otherwise).
88    pub imported_as: String,
89    /// The local import name, or the forwarded export name for a re-export
90    /// (`*` for an unaliased star re-export).
91    pub local_name: String,
92    /// Whether the import edge is type-only (`import type { ... }`).
93    pub type_only: bool,
94    /// The hop's depth (1 = direct caller/callee of the symbol).
95    pub depth: u32,
96}
97
98/// A callee referenced at a call site that did not resolve to an import-symbol
99/// edge. Surfaced so a missing callee is never silently dropped.
100#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
101#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
102pub struct UnresolvedCallee {
103    /// The callee path as written at the call site (e.g. `helper`,
104    /// `obj.method`).
105    pub callee: String,
106    /// Why it is unresolved (best-effort classification).
107    pub reason: UnresolvedReason,
108}
109
110/// Best-effort classification of why a callee did not resolve to an edge.
111#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
112#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
113#[serde(rename_all = "kebab-case")]
114pub enum UnresolvedReason {
115    /// A call through a bare identifier with no matching import-symbol edge.
116    /// This includes local bindings imported from external packages.
117    LocalOrGlobal,
118    /// A computed / member-expression callee (`obj.method`, dynamic dispatch).
119    MemberOrDynamic,
120}
121
122/// Target and traversal parameters for a symbol-chain trace.
123#[derive(Debug, Clone, Copy)]
124pub struct SymbolChainQuery<'a> {
125    /// File path of the target symbol, root-relative or absolute.
126    pub file: &'a str,
127    /// Exported symbol name to trace.
128    pub symbol: &'a str,
129    /// Maximum traversal depth in each direction.
130    pub depth: u32,
131    /// Which directions to walk.
132    pub directions: TraceDirections,
133}