fallow_types/results_feature_flags.rs
1//! Feature flags, export usages, and reference locations.
2
3use super::*;
4
5/// The detection method used to identify a feature flag.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
8#[serde(rename_all = "snake_case")]
9pub enum FlagKind {
10 /// Environment variable check (e.g., `process.env.FEATURE_X`).
11 EnvironmentVariable,
12 /// Feature flag SDK call (e.g., `useFlag('name')`, `variation('name', false)`).
13 SdkCall,
14 /// Config object property access (e.g., `config.features.newCheckout`).
15 ConfigObject,
16}
17
18/// Detection confidence for a feature flag finding.
19#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
20#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
21#[serde(rename_all = "snake_case")]
22pub enum FlagConfidence {
23 /// Low confidence: heuristic match (config object patterns).
24 Low,
25 /// Medium confidence: a generic SDK name, such as `isEnabled`, in a file
26 /// that imports no flag SDK or flag module.
27 Medium,
28 /// High confidence: unambiguous pattern (env vars, specific SDK calls,
29 /// generic SDK calls in a file that imports a flag SDK or flag module).
30 High,
31}
32
33/// A detected feature flag use site.
34#[derive(Debug, Clone, Serialize, Deserialize)]
35#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
36pub struct FeatureFlag {
37 /// File containing the feature flag usage.
38 #[serde(serialize_with = "serde_path::serialize")]
39 pub path: PathBuf,
40 /// Name or identifier of the flag (e.g., `ENABLE_NEW_CHECKOUT`, `new-checkout`).
41 pub flag_name: String,
42 /// How the flag was detected.
43 pub kind: FlagKind,
44 /// Detection confidence level.
45 pub confidence: FlagConfidence,
46 /// 1-based line number.
47 pub line: u32,
48 /// 0-based byte column offset.
49 pub col: u32,
50 /// Start byte offset of the guarded code block (if-branch span), if detected.
51 #[serde(skip)]
52 pub guard_span_start: Option<u32>,
53 /// End byte offset of the guarded code block (if-branch span), if detected.
54 #[serde(skip)]
55 pub guard_span_end: Option<u32>,
56 /// SDK or provider name (e.g., "LaunchDarkly", "Statsig"), if detected from SDK call.
57 #[serde(default, skip_serializing_if = "Option::is_none")]
58 pub sdk_name: Option<String>,
59 /// Line range of the guarded code block (derived from guard_span + line_offsets).
60 /// Used for cross-reference with dead code findings.
61 #[serde(skip)]
62 pub guard_line_start: Option<u32>,
63 /// End line of the guarded code block.
64 #[serde(skip)]
65 pub guard_line_end: Option<u32>,
66 /// Unused exports found within the guarded code block.
67 /// Populated by cross-reference with dead code analysis.
68 #[serde(default, skip_serializing_if = "Vec::is_empty")]
69 pub guarded_dead_exports: Vec<String>,
70}
71
72// Size assertion: FeatureFlag is stored in a Vec per analysis run.
73
74/// Usage count for an export symbol. Used by the LSP Code Lens to show
75/// the number of importing files above each export declaration.
76#[derive(Debug, Clone, Serialize, Deserialize)]
77#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
78pub struct ExportUsage {
79 /// File containing the export.
80 #[serde(serialize_with = "serde_path::serialize")]
81 pub path: PathBuf,
82 /// Name of the exported symbol.
83 pub export_name: String,
84 /// 1-based line number.
85 pub line: u32,
86 /// 0-based byte column offset.
87 pub col: u32,
88 /// Number of distinct files that import this export. Two imports in one
89 /// file count as one file.
90 pub reference_count: usize,
91 /// Import sites that reference this export, one entry per physical import.
92 /// A file with two imports gives two entries, so this list can be longer
93 /// than `reference_count`. An import without a source span has no entry.
94 /// Used by the LSP Code Lens to enable click-to-navigate via
95 /// `editor.action.showReferences`.
96 pub reference_locations: Vec<ReferenceLocation>,
97}
98
99/// A location where an export is referenced (import site in another file).
100#[derive(Debug, Clone, Serialize, Deserialize)]
101#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
102pub struct ReferenceLocation {
103 /// File containing the import that references the export.
104 #[serde(serialize_with = "serde_path::serialize")]
105 pub path: PathBuf,
106 /// 1-based line number.
107 pub line: u32,
108 /// 0-based byte column offset.
109 pub col: u32,
110}