fallow-extract 3.48.0

AST extraction engine for fallow codebase intelligence (parser, complexity, SFC / Astro / MDX / CSS)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
use fallow_types::extract::{SinkArgKind, SinkLiteralValue, SinkShape, SinkSite};
use oxc_allocator::Allocator;
use oxc_ast::ast::{
    BinaryOperator, CallExpression, Expression, ObjectExpression, ObjectPropertyKind, Statement,
};
use oxc_ast_visit::Visit;
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType;
use rustc_hash::{FxHashMap, FxHashSet};

use crate::visitor::ModuleInfoExtractor;
use crate::{MemberAccess, ModuleInfo};

/// The bindings that the script pass of a component declares and that its
/// markup can call. The template-visible imports and bound targets reach the
/// snippet scan as their own parameters; this context adds the bindings that
/// the visitor needs to read a call in markup the same way as in script.
#[derive(Debug, Default, Clone)]
pub struct ScriptBindingContext {
    /// Svelte `createEventDispatcher()` bindings.
    pub(crate) event_dispatch_bindings: FxHashSet<String>,
    /// Vue emit bindings: the `defineEmits()` return binding and `$emit`.
    pub(crate) emit_bindings: FxHashSet<String>,
    /// Script constants with a literal value (`const cmd = "ls"`). A security
    /// sink in markup reads such an argument as a literal, as in script.
    pub(crate) static_sink_literals: FxHashMap<String, SinkLiteralValue>,
}

impl ScriptBindingContext {
    /// Whether `snippet` names a dispatch or emit binding of the context.
    fn names_event_binding(&self, snippet: &str) -> (bool, bool) {
        (
            self.event_dispatch_bindings
                .iter()
                .any(|name| snippet.contains(name.as_str())),
            self.emit_bindings
                .iter()
                .any(|name| snippet.contains(name.as_str())),
        )
    }
}

/// The module of a component while its markup snippets merge into it. Each
/// snippet extractor merges through [`ModuleInfoExtractor::merge_into`], the
/// same merge that the script blocks use. A markup snippet has no source
/// position of its own, so each of its spans moves to `anchor`, the start of
/// the markup tag that holds the snippet.
#[derive(Debug, Clone)]
pub struct MarkupFactSink {
    context: std::sync::Arc<ScriptBindingContext>,
    module: Box<ModuleInfo>,
    anchor: u32,
    /// The snippets that merged, by anchor and text. The Svelte scanner reads
    /// the `{...}` of an attribute both as a brace section of the tag and as
    /// the attribute value. The usage fields are sets, so a second read
    /// changes nothing there, but a fact list such as the dispatched events
    /// must get each snippet once.
    absorbed: FxHashSet<(u32, String)>,
}

impl MarkupFactSink {
    /// A snippet that does not parse can hide a dispatch or emit call. When it
    /// names a dispatch or emit binding, the component abstains, as for a call
    /// with no literal event name.
    fn abstain_on_unparsed(&mut self, snippet: &str) {
        let (dispatch, emit) = self.context.names_event_binding(snippet);
        self.module.has_dynamic_dispatch |= dispatch;
        self.module.has_dynamic_emit |= emit;
    }

    /// Whether the snippet `text` at the current anchor merges for the first time.
    fn first_absorb(&mut self, text: &str) -> bool {
        self.absorbed.insert((self.anchor, text.to_string()))
    }

    fn absorb(&mut self, mut extractor: ModuleInfoExtractor) {
        extractor.clear_script_only_facts();
        let anchor = self.anchor;
        extractor.remap_spans_with(|_| oxc_span::Span::new(anchor, anchor));
        let dispatch_base = self.module.svelte_dispatched_events.len();
        extractor.merge_into(&mut self.module);
        for event in &mut self.module.svelte_dispatched_events[dispatch_base..] {
            event.span_start = anchor;
        }
    }
}

#[derive(Debug, Default, Clone)]
pub struct TemplateUsage {
    pub(crate) used_bindings: FxHashSet<String>,
    pub(crate) member_accesses: Vec<MemberAccess>,
    pub(crate) whole_object_uses: Vec<String>,
    /// PascalCase tag names that matched no import or local binding.
    pub(crate) unresolved_tag_names: FxHashSet<String>,
    pub(crate) security_sinks: Vec<SinkSite>,
    /// The component module that the markup snippet facts merge into. `None`
    /// for a scan that keeps only the usage fields above.
    markup: Option<Box<MarkupFactSink>>,
}

impl TemplateUsage {
    /// A usage scan whose markup snippets merge all their facts into `module`.
    pub(crate) fn with_markup_sink(context: ScriptBindingContext, module: ModuleInfo) -> Self {
        Self {
            markup: Some(Box::new(MarkupFactSink {
                context: std::sync::Arc::new(context),
                module: Box::new(module),
                anchor: 0,
                absorbed: FxHashSet::default(),
            })),
            ..Self::default()
        }
    }

    /// Move the markup sink of `other` into this usage. A scanner that starts
    /// a nested usage takes the sink first and gives it back with this call.
    pub(crate) fn take_markup_sink_from(&mut self, other: &mut Self) {
        if self.markup.is_none() {
            self.markup = other.markup.take();
        }
    }

    /// Set the source offset of the markup tag that holds the next snippets.
    pub(crate) fn set_markup_anchor(&mut self, offset: usize) {
        if let Some(sink) = self.markup.as_mut() {
            sink.anchor = u32::try_from(offset).unwrap_or(u32::MAX);
        }
    }

    /// Give back the component module with the markup snippet facts merged.
    pub(crate) fn take_markup_module(&mut self) -> Option<ModuleInfo> {
        self.markup.take().map(|sink| *sink.module)
    }

    pub(crate) fn merge(&mut self, mut other: Self) {
        self.take_markup_sink_from(&mut other);
        self.used_bindings.extend(other.used_bindings);
        self.unresolved_tag_names.extend(other.unresolved_tag_names);
        self.security_sinks.extend(other.security_sinks);
        for access in other.member_accesses {
            let key = (&access.object, &access.member);
            let already_present = self
                .member_accesses
                .iter()
                .any(|existing| (&existing.object, &existing.member) == key);
            if !already_present {
                self.member_accesses.push(access);
            }
        }
        for whole in other.whole_object_uses {
            if !self
                .whole_object_uses
                .iter()
                .any(|existing| existing == &whole)
            {
                self.whole_object_uses.push(whole);
            }
        }
    }

    #[cfg(test)]
    pub(crate) fn is_empty(&self) -> bool {
        self.used_bindings.is_empty()
            && self.member_accesses.is_empty()
            && self.whole_object_uses.is_empty()
            && self.unresolved_tag_names.is_empty()
            && self.security_sinks.is_empty()
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TemplateSnippetKind {
    Expression,
    Statement,
}

#[cfg(test)]
fn analyze_template_snippet(
    snippet: &str,
    kind: TemplateSnippetKind,
    imported_bindings: &FxHashSet<String>,
    locals: &[String],
    allow_dollar_prefixed_refs: bool,
) -> TemplateUsage {
    analyze_template_snippet_with_bound_targets(
        snippet,
        kind,
        imported_bindings,
        &FxHashMap::default(),
        locals,
        allow_dollar_prefixed_refs,
    )
}

#[cfg(test)]
fn analyze_template_snippet_with_bound_targets(
    snippet: &str,
    kind: TemplateSnippetKind,
    imported_bindings: &FxHashSet<String>,
    bound_targets: &FxHashMap<String, String>,
    locals: &[String],
    allow_dollar_prefixed_refs: bool,
) -> TemplateUsage {
    let mut usage = TemplateUsage::default();
    analyze_template_snippet_into(
        &mut usage,
        &TemplateSnippet {
            source: snippet,
            kind,
            imported_bindings,
            bound_targets,
            locals,
            allow_dollar_prefixed_refs,
        },
    );
    usage
}

/// One markup snippet and the template-visible names it can read.
pub struct TemplateSnippet<'a> {
    pub(crate) source: &'a str,
    pub(crate) kind: TemplateSnippetKind,
    pub(crate) imported_bindings: &'a FxHashSet<String>,
    pub(crate) bound_targets: &'a FxHashMap<String, String>,
    pub(crate) locals: &'a [String],
    pub(crate) allow_dollar_prefixed_refs: bool,
}

/// Analyze one markup snippet into `usage`. With a markup sink, the snippet
/// extractor starts from the script binding context of the component, and
/// every fact that is not script-only merges into the component module.
/// Without a sink, only the usage fields of [`TemplateUsage`] are kept.
pub fn analyze_template_snippet_into(usage: &mut TemplateUsage, input: &TemplateSnippet<'_>) {
    let snippet = input.source.trim();
    let has_sink = usage.markup.is_some();
    if snippet.is_empty()
        || (!has_sink && input.imported_bindings.is_empty() && input.bound_targets.is_empty())
    {
        return;
    }

    let wrapped = wrap_snippet(snippet, input.kind, input.locals);
    let allocator = Allocator::default();
    let parser_return = Parser::new(&allocator, &wrapped, SourceType::ts()).parse();

    let semantic_ret = SemanticBuilder::new().build(&parser_return.program);
    let (used_bindings, unresolved_targets) = classify_unresolved_references(
        &semantic_ret,
        input.imported_bindings,
        input.bound_targets,
        input.allow_dollar_prefixed_refs,
    );

    if unresolved_targets.is_empty() && !has_sink {
        return;
    }

    let mut extractor = ModuleInfoExtractor::new();
    if let Some(sink) = usage.markup.as_mut() {
        if parser_return.fatal_error || !parser_return.diagnostics.is_empty() {
            sink.abstain_on_unparsed(snippet);
        }
        extractor.use_script_binding_context(std::sync::Arc::clone(&sink.context), input.locals);
    }
    extractor.register_template_object_locals(used_bindings.iter().cloned());
    extractor.visit_program(&parser_return.program);

    let member_accesses = std::mem::take(&mut extractor.member_accesses);
    let whole_object_uses = std::mem::take(&mut extractor.whole_object_uses);
    if let Some(sink) = usage.markup.as_mut()
        && sink.first_absorb(snippet)
    {
        sink.absorb(extractor);
    }
    usage.merge(build_template_usage(
        used_bindings,
        &unresolved_targets,
        input.bound_targets,
        input.allow_dollar_prefixed_refs,
        member_accesses,
        whole_object_uses,
    ));
}

/// Split a parsed snippet's root unresolved references into the imported
/// bindings actually used and the full set of remappable target names
/// (imported bindings + bound targets, with optional `$`-prefix stripping).
fn classify_unresolved_references(
    semantic_ret: &oxc_semantic::SemanticBuilderReturn<'_>,
    imported_bindings: &FxHashSet<String>,
    bound_targets: &FxHashMap<String, String>,
    allow_dollar_prefixed_refs: bool,
) -> (FxHashSet<String>, FxHashSet<String>) {
    let mut used_bindings = FxHashSet::default();
    let mut unresolved_targets = FxHashSet::default();
    for name in semantic_ret
        .semantic
        .scoping()
        .root_unresolved_references()
        .keys()
    {
        let name = name.as_str();
        if imported_bindings.contains(name) {
            used_bindings.insert(name.to_string());
            unresolved_targets.insert(name.to_string());
            continue;
        }
        if bound_targets.contains_key(name) {
            unresolved_targets.insert(name.to_string());
            continue;
        }
        if allow_dollar_prefixed_refs && let Some(stripped) = name.strip_prefix('$') {
            if imported_bindings.contains(stripped) {
                used_bindings.insert(stripped.to_string());
                unresolved_targets.insert(stripped.to_string());
            } else if bound_targets.contains_key(stripped) {
                unresolved_targets.insert(stripped.to_string());
            }
        }
    }
    (used_bindings, unresolved_targets)
}

/// Build the [`TemplateUsage`] from the extracted member accesses / whole-object
/// uses, remapping each object name through the resolved target set.
fn build_template_usage(
    used_bindings: FxHashSet<String>,
    unresolved_targets: &FxHashSet<String>,
    bound_targets: &FxHashMap<String, String>,
    allow_dollar_prefixed_refs: bool,
    member_accesses: Vec<MemberAccess>,
    whole_object_uses: Vec<String>,
) -> TemplateUsage {
    TemplateUsage {
        used_bindings,
        member_accesses: dedup_member_accesses(
            member_accesses
                .into_iter()
                .filter_map(|access| {
                    remap_object_name(
                        &access.object,
                        unresolved_targets,
                        bound_targets,
                        allow_dollar_prefixed_refs,
                    )
                    .map(|object| MemberAccess {
                        object,
                        member: access.member,
                    })
                })
                .collect(),
        ),
        whole_object_uses: dedup_names(
            whole_object_uses
                .into_iter()
                .filter_map(|name| {
                    remap_object_name(
                        &name,
                        unresolved_targets,
                        bound_targets,
                        allow_dollar_prefixed_refs,
                    )
                })
                .collect(),
        ),
        unresolved_tag_names: FxHashSet::default(),
        security_sinks: Vec::new(),
        markup: None,
    }
}

/// Collect both unresolved identifier references AND static member-access chains
/// (`obj.member`) where `obj` is unresolved.
///
/// Unlike `analyze_template_snippet`, this does NOT filter against imported bindings.
/// Returns `(identifiers, member_accesses)`. Used by the Angular template scanner
/// for external templates where unresolved identifiers are potential component
/// class member refs and member-access chains (`dataService.getTotal()`) must
/// be resolved against the component's constructor-injected type bindings to
/// credit the target class's member as used.
pub fn collect_unresolved_refs_and_accesses(
    snippet: &str,
    kind: TemplateSnippetKind,
    locals: &[String],
) -> (FxHashSet<String>, Vec<MemberAccess>) {
    let snippet = snippet.trim();
    if snippet.is_empty() {
        return (FxHashSet::default(), Vec::new());
    }

    let wrapped = wrap_snippet(snippet, kind, locals);
    let allocator = Allocator::default();
    let parser_return = Parser::new(&allocator, &wrapped, SourceType::ts()).parse();
    let semantic_ret = SemanticBuilder::new().build(&parser_return.program);

    let unresolved_names: FxHashSet<String> = semantic_ret
        .semantic
        .scoping()
        .root_unresolved_references()
        .keys()
        .map(|name| name.to_string())
        .collect();

    if unresolved_names.is_empty() {
        return (unresolved_names, Vec::new());
    }

    let mut extractor = ModuleInfoExtractor::new();
    extractor.visit_program(&parser_return.program);
    let member_accesses = dedup_member_accesses(
        extractor
            .member_accesses
            .into_iter()
            .filter(|access| unresolved_names.contains(&access.object))
            .collect(),
    );

    (unresolved_names, member_accesses)
}

/// The callee path prefix of a sink in template markup (`template.innerHTML`
/// for Vue `v-html`, Svelte `{@html}` and Angular `[innerHTML]`). The span of
/// such a sink covers its tag or attribute, not the exact sink expression.
pub const TEMPLATE_SINK_CALLEE_PREFIX: &str = "template.";

pub fn template_html_sink(snippet: &str, span_start: usize, span_end: usize) -> Option<SinkSite> {
    template_dom_sink("innerHTML", snippet, span_start, span_end)
}

/// A template binding of an iframe `srcdoc` (`:srcdoc`, `[srcdoc]`,
/// `srcdoc={x}`), captured as the `template.srcdoc` member-assign shape.
pub fn template_srcdoc_sink(snippet: &str, span_start: usize, span_end: usize) -> Option<SinkSite> {
    template_dom_sink("srcdoc", snippet, span_start, span_end)
}

fn template_dom_sink(
    property: &str,
    snippet: &str,
    span_start: usize,
    span_end: usize,
) -> Option<SinkSite> {
    let snippet = snippet.trim();
    if snippet.is_empty() {
        return None;
    }

    let parsed = with_parsed_template_expression(snippet, |expr| {
        if !is_non_literal_template_arg(expr) {
            return None;
        }
        Some((
            classify_template_arg_kind(expr),
            collect_template_arg_idents(expr),
        ))
    });
    let (arg_kind, arg_idents) = match parsed {
        Some(Some(parsed)) => parsed,
        Some(None) => return None,
        None => (SinkArgKind::Other, Vec::new()),
    };

    Some(SinkSite {
        sink_shape: SinkShape::MemberAssign,
        callee_path: format!("{TEMPLATE_SINK_CALLEE_PREFIX}{property}"),
        arg_index: 0,
        arg_is_non_literal: true,
        arg_kind,
        arg_literal: None,
        regex_pattern: None,
        object_properties: Vec::new(),
        object_property_keys: Vec::new(),
        object_property_keys_complete: false,
        arg_idents,
        arg_source_paths: Vec::new(),
        span_start: u32::try_from(span_start).ok()?,
        span_end: u32::try_from(span_end).ok()?,
        url_arg_literal: None,
        url_shape: None,
        call_options: None,
    })
}

fn with_parsed_template_expression<R>(
    snippet: &str,
    body: impl FnOnce(&Expression<'_>) -> R,
) -> Option<R> {
    let snippet = snippet.trim();
    if snippet.is_empty() {
        return None;
    }
    let wrapped = format!("const __f_tpl_sink = ({snippet});");
    let allocator = Allocator::default();
    let parser_return = Parser::new(&allocator, &wrapped, SourceType::ts()).parse();
    let Statement::VariableDeclaration(decl) = parser_return.program.body.first()? else {
        return None;
    };
    let expr = decl.declarations.first()?.init.as_ref()?;
    Some(body(expr))
}

fn is_non_literal_template_arg(expr: &Expression<'_>) -> bool {
    match unwrap_template_parens(expr) {
        Expression::StringLiteral(_)
        | Expression::NumericLiteral(_)
        | Expression::BooleanLiteral(_)
        | Expression::NullLiteral(_)
        | Expression::BigIntLiteral(_)
        | Expression::RegExpLiteral(_) => false,
        Expression::TemplateLiteral(tpl) => !tpl.expressions.is_empty(),
        _ => true,
    }
}

fn classify_template_arg_kind(expr: &Expression<'_>) -> SinkArgKind {
    match unwrap_template_parens(expr) {
        Expression::TemplateLiteral(_) => SinkArgKind::TemplateWithSubst,
        Expression::BinaryExpression(bin) if bin.operator == BinaryOperator::Addition => {
            SinkArgKind::Concat
        }
        Expression::ObjectExpression(_) => SinkArgKind::Object,
        Expression::CallExpression(_) => SinkArgKind::Call,
        _ => SinkArgKind::Other,
    }
}

fn collect_template_arg_idents(expr: &Expression<'_>) -> Vec<String> {
    let mut out = Vec::new();
    collect_template_idents_into(expr, &mut out);
    out
}

fn push_template_ident(name: &str, out: &mut Vec<String>) {
    if !out.iter().any(|existing| existing == name) {
        out.push(name.to_string());
    }
}

fn collect_template_idents_into(expr: &Expression<'_>, out: &mut Vec<String>) {
    match expr {
        Expression::Identifier(ident) => push_template_ident(&ident.name, out),
        Expression::ParenthesizedExpression(paren) => {
            collect_template_idents_into(&paren.expression, out);
        }
        Expression::StaticMemberExpression(member) => {
            collect_template_idents_into(&member.object, out);
        }
        Expression::ComputedMemberExpression(member) => {
            collect_template_pair_idents(&member.object, &member.expression, out);
        }
        Expression::BinaryExpression(bin) => {
            collect_template_pair_idents(&bin.left, &bin.right, out);
        }
        Expression::LogicalExpression(logical) => {
            collect_template_pair_idents(&logical.left, &logical.right, out);
        }
        Expression::ConditionalExpression(cond) => {
            collect_template_conditional_idents(&cond.test, &cond.consequent, &cond.alternate, out);
        }
        Expression::SequenceExpression(seq) => {
            for expr in &seq.expressions {
                collect_template_idents_into(expr, out);
            }
        }
        Expression::TemplateLiteral(tpl) => {
            for expr in &tpl.expressions {
                collect_template_idents_into(expr, out);
            }
        }
        Expression::AwaitExpression(await_expr) => {
            collect_template_idents_into(&await_expr.argument, out);
        }
        Expression::UnaryExpression(unary) => {
            collect_template_idents_into(&unary.argument, out);
        }
        Expression::CallExpression(call) => {
            collect_template_call_idents(call, out);
        }
        Expression::ObjectExpression(obj) => {
            collect_template_object_idents(obj, out);
        }
        _ => {}
    }
}

fn collect_template_pair_idents(
    left: &Expression<'_>,
    right: &Expression<'_>,
    out: &mut Vec<String>,
) {
    collect_template_idents_into(left, out);
    collect_template_idents_into(right, out);
}

fn collect_template_conditional_idents(
    test: &Expression<'_>,
    consequent: &Expression<'_>,
    alternate: &Expression<'_>,
    out: &mut Vec<String>,
) {
    collect_template_idents_into(test, out);
    collect_template_idents_into(consequent, out);
    collect_template_idents_into(alternate, out);
}

fn collect_template_call_idents(call: &CallExpression<'_>, out: &mut Vec<String>) {
    collect_template_idents_into(&call.callee, out);
    for arg in &call.arguments {
        if let Some(arg_expr) = arg.as_expression() {
            collect_template_idents_into(arg_expr, out);
        }
    }
}

fn collect_template_object_idents(obj: &ObjectExpression<'_>, out: &mut Vec<String>) {
    for prop in &obj.properties {
        if let ObjectPropertyKind::ObjectProperty(prop) = prop {
            collect_template_idents_into(&prop.value, out);
        }
    }
}

fn unwrap_template_parens<'a>(mut expr: &'a Expression<'a>) -> &'a Expression<'a> {
    while let Expression::ParenthesizedExpression(paren) = expr {
        expr = &paren.expression;
    }
    expr
}

fn remap_object_name(
    name: &str,
    unresolved_names: &FxHashSet<String>,
    bound_targets: &FxHashMap<String, String>,
    allow_dollar_prefixed_refs: bool,
) -> Option<String> {
    if unresolved_names.contains(name) {
        if let Some(target) = bound_targets.get(name) {
            return Some(target.clone());
        }
        return Some(name.to_string());
    }
    if allow_dollar_prefixed_refs
        && let Some(stripped) = name.strip_prefix('$')
        && unresolved_names.contains(stripped)
    {
        if let Some(target) = bound_targets.get(stripped) {
            return Some(target.clone());
        }
        return Some(stripped.to_string());
    }
    // unused-load-data-key Primitive C: the SvelteKit global page store read in a
    // template, `$page.data.KEY` (Svelte 4 `$app/stores`) or `page.data.KEY`
    // (Svelte 5 `$app/state`), arrives here as the dotted member object
    // `$page.data` / `page.data` (the object of the outer `.KEY` access). The
    // root-only logic above remaps `$page`/`page` but drops the `.data` suffix,
    // so the consumed key is lost. Recover the canonical `page.data` object (the
    // `$`-stripped form, matching how the root branch normalizes `$page` -> `page`)
    // so the cross-file detector can see the project-wide `page.data.<key>`
    // consumer channel. Scoped to the `page` store's `data` member only (gated on
    // the Svelte dollar-ref path) to stay byte-identical for every other dotted
    // template member object; `$page.url.pathname` etc. still map root-only.
    if allow_dollar_prefixed_refs
        && (name == "page.data" || name == "$page.data")
        && unresolved_names.contains("page")
    {
        return Some("page.data".to_string());
    }
    None
}

fn wrap_snippet(snippet: &str, kind: TemplateSnippetKind, locals: &[String]) -> String {
    let mut wrapped = String::new();
    if !locals.is_empty() {
        wrapped.push_str("const __f_tpl_local = undefined;\n");
        for local in locals {
            wrapped.push_str("const ");
            wrapped.push_str(local);
            wrapped.push_str(" = __f_tpl_local;\n");
        }
    }

    match kind {
        TemplateSnippetKind::Expression => {
            wrapped.push_str("void (");
            wrapped.push_str(snippet);
            wrapped.push_str(");\n");
        }
        TemplateSnippetKind::Statement => {
            wrapped.push_str("(() => {\n");
            wrapped.push_str(snippet);
            wrapped.push_str("\n})();\n");
        }
    }

    wrapped
}

fn dedup_member_accesses(accesses: Vec<MemberAccess>) -> Vec<MemberAccess> {
    let mut seen: FxHashSet<(String, String)> = FxHashSet::default();
    let mut deduped = Vec::with_capacity(accesses.len());
    for access in accesses {
        let key = (access.object.clone(), access.member.clone());
        if seen.insert(key) {
            deduped.push(access);
        }
    }
    deduped
}

fn dedup_names(names: Vec<String>) -> Vec<String> {
    let mut seen: FxHashSet<String> = FxHashSet::default();
    let mut deduped = Vec::with_capacity(names.len());
    for name in names {
        if seen.insert(name.clone()) {
            deduped.push(name);
        }
    }
    deduped
}

#[cfg(test)]
mod tests {
    use super::*;

    fn bindings(names: &[&str]) -> FxHashSet<String> {
        names.iter().map(|name| (*name).to_string()).collect()
    }

    #[test]
    fn expression_usage_tracks_named_bindings() {
        let usage = analyze_template_snippet(
            "formatDate(user.createdAt)",
            TemplateSnippetKind::Expression,
            &bindings(&["formatDate"]),
            &[],
            false,
        );

        assert!(usage.used_bindings.contains("formatDate"));
        assert!(usage.member_accesses.is_empty());
        assert!(usage.whole_object_uses.is_empty());
    }

    #[test]
    fn expression_usage_tracks_namespace_members() {
        let usage = analyze_template_snippet(
            "utils.formatDate(user.createdAt)",
            TemplateSnippetKind::Expression,
            &bindings(&["utils"]),
            &[],
            false,
        );

        assert!(usage.used_bindings.contains("utils"));
        assert_eq!(usage.member_accesses.len(), 1);
        assert_eq!(usage.member_accesses[0].object, "utils");
        assert_eq!(usage.member_accesses[0].member, "formatDate");
    }

    #[test]
    fn locals_shadow_imported_names() {
        let usage = analyze_template_snippet(
            "item.name",
            TemplateSnippetKind::Expression,
            &bindings(&["item"]),
            &["item".to_string()],
            false,
        );

        assert!(usage.is_empty());
    }

    #[test]
    fn statement_usage_tracks_handler_references() {
        let usage = analyze_template_snippet(
            "count += increment(step);",
            TemplateSnippetKind::Statement,
            &bindings(&["increment"]),
            &[],
            false,
        );

        assert!(usage.used_bindings.contains("increment"));
    }

    #[test]
    fn dollar_prefixed_refs_can_map_to_imported_store_bindings() {
        let usage = analyze_template_snippet(
            "$page.url.pathname",
            TemplateSnippetKind::Expression,
            &bindings(&["page"]),
            &[],
            true,
        );

        assert!(usage.used_bindings.contains("page"));
        assert_eq!(usage.member_accesses.len(), 1);
        assert_eq!(usage.member_accesses[0].object, "page");
        assert_eq!(usage.member_accesses[0].member, "url");
    }

    // unused-load-data-key Primitive C: the SvelteKit global page store read in a
    // template recovers the nested `page.data.<key>` member access.

    #[test]
    fn svelte4_page_store_data_key_recovers_nested_member() {
        let usage = analyze_template_snippet(
            "$page.data.user",
            TemplateSnippetKind::Expression,
            &bindings(&["page"]),
            &[],
            true,
        );

        assert!(
            usage
                .member_accesses
                .iter()
                .any(|a| a.object == "page.data" && a.member == "user"),
            "`$page.data.user` should recover `page.data.user`, got: {:?}",
            usage.member_accesses
        );
    }

    #[test]
    fn svelte5_page_state_data_key_recovers_nested_member() {
        let usage = analyze_template_snippet(
            "page.data.session",
            TemplateSnippetKind::Expression,
            &bindings(&["page"]),
            &[],
            true,
        );

        assert!(
            usage
                .member_accesses
                .iter()
                .any(|a| a.object == "page.data" && a.member == "session"),
            "`page.data.session` should recover `page.data.session`, got: {:?}",
            usage.member_accesses
        );
    }

    #[test]
    fn page_store_non_data_member_stays_root_only() {
        // `$page.url.pathname` must keep mapping to root `page.url` -> `{page, url}`;
        // only the `data` channel recovers the nested key. Regression guard for the
        // existing store-ref behavior.
        let usage = analyze_template_snippet(
            "$page.url.pathname",
            TemplateSnippetKind::Expression,
            &bindings(&["page"]),
            &[],
            true,
        );

        assert!(
            !usage.member_accesses.iter().any(|a| a.object == "page.url"),
            "`$page.url` must not recover a nested member, got: {:?}",
            usage.member_accesses
        );
    }

    #[test]
    fn page_data_nested_recovery_is_svelte_only() {
        // Vue templates pass allow_dollar_prefixed_refs=false; the `page` store is
        // SvelteKit-specific, so `page.data.x` must NOT recover the nested member
        // in a Vue context (root `{page, data}` is still credited).
        let usage = analyze_template_snippet(
            "page.data.x",
            TemplateSnippetKind::Expression,
            &bindings(&["page"]),
            &[],
            false,
        );

        assert!(
            !usage
                .member_accesses
                .iter()
                .any(|a| a.object == "page.data"),
            "Vue context must not recover `page.data`, got: {:?}",
            usage.member_accesses
        );
    }
}