fallow-extract 3.34.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
//! Dynamic import capture helpers for the visitor implementation.

use super::*;
use crate::visitor::{
    LoaderImport, LocalImportLoader, collect_loader_imports, collect_returned_imports,
};

/// The default-export contract of an imported component loader.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(in crate::visitor) enum ComponentLoaderImport {
    Function,
    ReactNamespace,
    VueNamespace,
    NextDynamic,
}

impl<'a> ModuleInfoExtractor {
    /// Record each `new URL(path, import.meta.url)` passed directly to a
    /// filesystem call, so `visit_new_expression` marks it speculative.
    pub(super) fn record_filesystem_path_new_url_arguments(&mut self, expr: &CallExpression<'_>) {
        if !is_filesystem_path_callee(&expr.callee) {
            return;
        }
        for arg in &expr.arguments {
            if let Argument::NewExpression(new_expr) = arg
                && new_url_import_source(new_expr).is_some()
            {
                self.filesystem_path_new_url_spans.insert(new_expr.span);
            }
        }
    }

    fn push_relative_dynamic_import_pattern(
        &mut self,
        prefix: String,
        excludes: &[String],
        span: Span,
    ) {
        if prefix.starts_with("./") || prefix.starts_with("../") {
            self.dynamic_import_patterns.push(DynamicImportPattern {
                prefix,
                suffix: None,
                span,
                mechanism: ModuleLoadMechanism::EsModule,
                excludes: excludes.to_vec(),
            });
        }
    }

    pub(super) fn record_import_meta_glob_patterns(&mut self, expr: &CallExpression<'_>) {
        if let Expression::StaticMemberExpression(member) = &expr.callee
            && member.property.name == "glob"
            && matches!(
                member.object,
                Expression::ImportMeta(_) | Expression::NewTarget(_)
            )
            && let Some(first_arg) = expr.arguments.first()
        {
            if expr.arguments.get(1).is_some_and(is_eager_glob_options) {
                self.mark_import_load_kind(expr.span, ImportLoadKind::Static);
            }
            match first_arg {
                Argument::StringLiteral(lit) => {
                    self.push_relative_dynamic_import_pattern(
                        lit.value.to_string(),
                        &[],
                        expr.span,
                    );
                }
                Argument::ArrayExpression(arr) => {
                    let entries: Vec<&str> = arr
                        .elements
                        .iter()
                        .filter_map(|elem| match elem {
                            ArrayExpressionElement::StringLiteral(lit) => Some(lit.value.as_str()),
                            _ => None,
                        })
                        .collect();
                    let excludes = import_meta_glob_excludes(&entries);
                    for entry in entries {
                        if !entry.starts_with('!') {
                            self.push_relative_dynamic_import_pattern(
                                entry.to_string(),
                                &excludes,
                                expr.span,
                            );
                        }
                    }
                }
                _ => {}
            }
        }
    }

    pub(super) fn record_require_context_pattern(&mut self, expr: &CallExpression<'_>) {
        if let Expression::StaticMemberExpression(member) = &expr.callee
            && member.property.name == "context"
            && let Expression::Identifier(obj) = &member.object
            && obj.name == "require"
            && let Some(Argument::StringLiteral(dir_lit)) = expr.arguments.first()
        {
            let dir = escape_glob_literal(&dir_lit.value);
            if dir.starts_with("./") || dir.starts_with("../") {
                let recursive = expr
                    .arguments
                    .get(1)
                    .is_some_and(|arg| matches!(arg, Argument::BooleanLiteral(b) if b.value));
                let prefix = if recursive {
                    format!("{dir}/**/")
                } else {
                    format!("{dir}/")
                };
                let suffix = expr.arguments.get(2).and_then(|arg| match arg {
                    Argument::RegExpLiteral(re) => regex_pattern_to_suffix(&re.regex.pattern.text),
                    _ => None,
                });
                self.dynamic_import_patterns.push(DynamicImportPattern {
                    prefix,
                    suffix,
                    span: expr.span,
                    mechanism: ModuleLoadMechanism::CommonJsRequire,
                    excludes: Vec::new(),
                });
            }
        }
    }

    /// Push one `DynamicImportInfo` edge per statically-resolvable branch of a
    /// dynamic `import()`, every branch sharing the same span and bindings.
    /// Single choke point for the branch fan-out used by the declaration,
    /// bare-expression, `.then`, arrow-wrapped, and route-property paths.
    pub(in crate::visitor) fn push_dynamic_import_branches(
        &mut self,
        sources: &[String],
        span: Span,
        destructured_names: &[String],
        local_name: Option<&str>,
    ) {
        for source in sources {
            self.dynamic_imports.push(DynamicImportInfo {
                source: source.clone(),
                span,
                destructured_names: destructured_names.to_vec(),
                local_name: local_name.map(str::to_string),
                is_speculative: false,
                value_escapes: false,
            });
        }
    }

    /// Push the edges of an `import()` without a binding. Only a discarded
    /// value is a plain load. Any other position hands the namespace to code
    /// that the walk does not follow, so the edge credits the whole module.
    pub(super) fn push_unbound_dynamic_import_branches(&mut self, sources: &[String], span: Span) {
        let value_escapes = !self.discarded_import_spans.contains(&span);
        for source in sources {
            self.dynamic_imports.push(DynamicImportInfo {
                source: source.clone(),
                span,
                destructured_names: Vec::new(),
                local_name: None,
                is_speculative: false,
                value_escapes,
            });
        }
    }

    /// Mark each `import()` whose value an expression statement discards:
    /// `import('./x')`, `await import('./x')`, `void import('./x')`, a
    /// `.catch()`, `.finally()` or parameterless `.then()` handler on it,
    /// `await Promise.all([import('./x')])`, and the operands of `&&`, `||`,
    /// `??`, `?:` and `,`. These forms only load the module and read no export.
    pub(super) fn record_discarded_dynamic_import(&mut self, expression: &Expression<'_>) {
        match expression.without_parentheses() {
            Expression::ImportExpression(import_expr) => {
                self.discarded_import_spans.insert(import_expr.span);
            }
            Expression::AwaitExpression(await_expr) => {
                self.record_discarded_dynamic_import(&await_expr.argument);
            }
            Expression::UnaryExpression(unary) if unary.operator == UnaryOperator::Void => {
                self.record_discarded_dynamic_import(&unary.argument);
            }
            Expression::SequenceExpression(sequence) => {
                for expr in &sequence.expressions {
                    self.record_discarded_dynamic_import(expr);
                }
            }
            Expression::LogicalExpression(logical) => {
                self.record_discarded_dynamic_import(&logical.left);
                self.record_discarded_dynamic_import(&logical.right);
            }
            Expression::ConditionalExpression(conditional) => {
                self.record_discarded_dynamic_import(&conditional.consequent);
                self.record_discarded_dynamic_import(&conditional.alternate);
            }
            Expression::CallExpression(call) => {
                if let Some(elements) = extract_promise_all_elements(expression) {
                    for element in elements {
                        if let Some(expr) = element.as_expression() {
                            self.record_discarded_dynamic_import(expr);
                        }
                    }
                } else if let Expression::StaticMemberExpression(member) = &call.callee
                    && matches!(member.property.name.as_str(), "catch" | "finally")
                {
                    self.record_discarded_dynamic_import(&member.object);
                } else if let Some(object) = parameterless_then_object(call) {
                    self.record_discarded_dynamic_import(object);
                }
            }
            _ => {}
        }
    }

    /// Mark the `import()` of `import('./x').then(() => ...)` as discarded in
    /// any position. The callback has no parameter, so it reads no export, and
    /// the chain resolves to the callback result, not to the namespace.
    pub(super) fn record_parameterless_then_import(&mut self, call: &CallExpression<'_>) {
        if let Some(Expression::ImportExpression(import_expr)) =
            parameterless_then_object(call).map(Expression::without_parentheses)
        {
            self.discarded_import_spans.insert(import_expr.span);
        }
    }

    /// Record a member read directly on an awaited dynamic import, such as
    /// `(await import('./x')).run()` or `new (await import('./x')).Cls()`.
    /// The read member is the only export the expression uses, so each
    /// statically resolvable branch credits that one name. The parent member
    /// expression is visited before the `import()` itself, so marking the span
    /// as handled stops `visit_import_expression` from adding a second edge.
    pub(super) fn record_awaited_dynamic_import_member(
        &mut self,
        object: &Expression<'_>,
        member: &str,
    ) {
        let Expression::AwaitExpression(await_expr) = object.without_parentheses() else {
            return;
        };
        let Expression::ImportExpression(import_expr) = await_expr.argument.without_parentheses()
        else {
            return;
        };
        if self.handled_import_spans.contains(&import_expr.span) {
            return;
        }
        let mut sources = Vec::new();
        collect_static_import_specifiers(&import_expr.source, &mut sources);
        if sources.is_empty() {
            return;
        }
        self.push_dynamic_import_branches(&sources, import_expr.span, &[member.to_string()], None);
        self.handled_import_spans.insert(import_expr.span);
    }

    pub(super) fn record_import_callback_dynamic_imports(&mut self, expr: &CallExpression<'_>) {
        if let Some(then_cb) = try_extract_import_then_callback(expr) {
            if let Some(local) = &then_cb.local_name {
                self.then_callback_namespace_bindings
                    .insert(then_cb.callback_start, local.clone());
            }
            self.handled_import_spans.insert(then_cb.import_span);
            self.push_dynamic_import_branches(
                &then_cb.sources,
                then_cb.import_span,
                &then_cb.destructured_names,
                then_cb.local_name.as_deref(),
            );
        }
    }

    /// Bind the namespace param of an `import('./x').then(m => ...)` callback
    /// in the callback scope, which the caller pushed before this call.
    pub(super) fn bind_then_callback_namespace(&mut self, start: u32) {
        if let Some(local) = self.then_callback_namespace_bindings.remove(&start) {
            self.record_namespace_binding_name(local);
        }
    }

    /// Record a namespace edge for a `<receiver>.ssrLoadModule('<literal>')`
    /// call whose result has no binding. A declaration such as
    /// `const m = await server.ssrLoadModule(...)` records the edge with its
    /// bindings first and marks the call span as handled.
    pub(super) fn record_ssr_load_module(&mut self, expr: &CallExpression<'_>) {
        if self.handled_import_spans.contains(&expr.span) {
            return;
        }
        if let Some(source) = ssr_load_module_source(expr) {
            self.push_dynamic_import_branches(&[source], expr.span, &[], None);
        }
    }

    /// Register runtime component-loader imports before visiting calls, including
    /// imports declared later in the file. Local lookalikes provide no contract.
    pub(super) fn record_program_component_loader_imports(&mut self, program: &Program<'_>) {
        self.component_loader_imports.clear();
        for statement in &program.body {
            let Statement::ImportDeclaration(decl) = statement else {
                continue;
            };
            if decl.import_kind.is_type() {
                continue;
            }
            for specifier in decl.specifiers.iter().flatten() {
                let imported = match specifier {
                    ImportDeclarationSpecifier::ImportSpecifier(named)
                        if !named.import_kind.is_type() =>
                    {
                        Some(named.imported.name())
                    }
                    ImportDeclarationSpecifier::ImportDefaultSpecifier(_) => Some("default".into()),
                    _ => None,
                };
                let contract = match (decl.source.value.as_str(), imported.as_deref(), specifier) {
                    ("react", Some("lazy"), _) | ("vue", Some("defineAsyncComponent"), _) => {
                        Some(ComponentLoaderImport::Function)
                    }
                    ("react", Some("default"), _)
                    | ("react", _, ImportDeclarationSpecifier::ImportNamespaceSpecifier(_)) => {
                        Some(ComponentLoaderImport::ReactNamespace)
                    }
                    ("vue", _, ImportDeclarationSpecifier::ImportNamespaceSpecifier(_)) => {
                        Some(ComponentLoaderImport::VueNamespace)
                    }
                    ("next/dynamic", Some("default"), _) => {
                        Some(ComponentLoaderImport::NextDynamic)
                    }
                    _ => None,
                };
                if let Some(contract) = contract {
                    self.component_loader_imports
                        .insert(specifier.local().name.to_string(), contract);
                }
            }
        }
    }

    fn component_loader_binding_is_shadowed(&self, name: &str) -> bool {
        // Exported namespaces do not maintain the ordinary declaration stack.
        // Other lexical roots also include catch bindings and local namespaces.
        self.namespace_depth > 0
            || self.nested_scope_shadows(name)
            || self
                .scoped_direct_object_binding_roots
                .iter()
                .any(|roots| roots.contains(name))
    }

    fn is_component_loader_call(&self, expr: &CallExpression<'_>) -> bool {
        match expr.callee.without_parentheses() {
            Expression::Identifier(callee) => {
                matches!(
                    self.component_loader_imports.get(callee.name.as_str()),
                    Some(ComponentLoaderImport::Function | ComponentLoaderImport::NextDynamic)
                ) && !self.component_loader_binding_is_shadowed(&callee.name)
            }
            Expression::StaticMemberExpression(member) => {
                let Expression::Identifier(root) = member.object.without_parentheses() else {
                    return false;
                };
                matches!(
                    (
                        self.component_loader_imports.get(root.name.as_str()),
                        member.property.name.as_str()
                    ),
                    (Some(ComponentLoaderImport::ReactNamespace), "lazy")
                        | (
                            Some(ComponentLoaderImport::VueNamespace),
                            "defineAsyncComponent"
                        )
                ) && !self.component_loader_binding_is_shadowed(&root.name)
            }
            _ => false,
        }
    }

    pub(super) fn record_arrow_wrapped_dynamic_import(&mut self, expr: &CallExpression<'_>) {
        // Unknown callbacks can return a namespace to arbitrary code. Let the
        // ordinary import walk retain its escaping-value credit instead.
        if !self.is_component_loader_call(expr) {
            return;
        }
        self.record_loader_argument_dynamic_imports(expr);
        if let Some((import_expr, sources)) = try_extract_arrow_wrapped_import(&expr.arguments) {
            self.push_dynamic_import_branches(
                &sources,
                import_expr.span,
                &["default".to_string()],
                None,
            );
            self.handled_import_spans.insert(import_expr.span);

            // Record the `import()` span when this is a
            // `next/dynamic(() => import('./X'), { ssr: false })` call. ssr:false
            // is Next.js's sanctioned client-only escape hatch, so the security
            // `client-server-leak` BFS must not treat a server-only module reached
            // only through it as a leak.
            if self.is_next_dynamic_ssr_false_call(expr) {
                self.client_only_dynamic_import_spans
                    .push(import_expr.span.start);
            }
        }
    }

    /// Credit `default` for each argument that names a local loader function,
    /// the same credit an inline `() => import('./x')` argument gets
    /// (`const load = () => import('./x'); lazy(load)`).
    fn record_loader_argument_dynamic_imports(&mut self, expr: &CallExpression<'_>) {
        if self.local_import_loaders.is_empty() {
            return;
        }
        let Some(Argument::Identifier(ident)) = expr.arguments.first() else {
            return;
        };
        if !self.local_import_loaders.contains_key(ident.name.as_str())
            || self.component_loader_binding_is_shadowed(&ident.name)
        {
            return;
        }
        let Some(loader) = self.local_import_loaders.get_mut(ident.name.as_str()) else {
            return;
        };
        loader.credited_references += 1;
        let imports = loader.imports.clone();
        let is_client_only = self.is_next_dynamic_ssr_false_call(expr);
        for import in imports {
            self.push_dynamic_import_branches(
                &import.sources,
                import.span,
                &["default".to_string()],
                None,
            );
            if is_client_only {
                self.client_only_dynamic_import_spans
                    .push(import.span.start);
            }
        }
    }

    /// Bind `const m = await load()` and `const { a } = await load()` to the
    /// `import()` that the local loader `load` returns, the same way as
    /// `const m = await import('./x')`. Returns whether the declarator had
    /// this shape.
    pub(super) fn record_loader_call_declaration(
        &mut self,
        declarator: &VariableDeclarator<'_>,
        init: &Expression<'_>,
    ) -> bool {
        if self.local_import_loaders.is_empty()
            || !matches!(
                declarator.id,
                BindingPattern::ObjectPattern(_) | BindingPattern::BindingIdentifier(_)
            )
        {
            return false;
        }
        let Expression::AwaitExpression(await_expr) = init.without_parentheses() else {
            return false;
        };
        let Expression::CallExpression(call) = await_expr.argument.without_parentheses() else {
            return false;
        };
        let Expression::Identifier(callee) = &call.callee else {
            return false;
        };
        let Some(loader) = self.local_import_loaders.get_mut(callee.name.as_str()) else {
            return false;
        };
        loader.credited_references += 1;
        for import in loader.imports.clone() {
            self.handle_dynamic_import_declaration(&declarator.id, import.span, &import.sources);
        }
        true
    }

    /// Count one reference to a local loader function. The end of the program
    /// walk compares this count with the references that credit precise names.
    pub(super) fn count_local_import_loader_reference(&mut self, name: &str) {
        if let Some(loader) = self.local_import_loaders.get_mut(name) {
            loader.references += 1;
        }
    }

    /// Register the top-level local loader functions of `program` before the
    /// body walk. A loader is a `const` arrow or function expression, or a
    /// function declaration, that returns a static `import()`. The `import()`
    /// span is marked as handled: the loader references decide its credit.
    pub(super) fn record_program_local_import_loaders(&mut self, program: &Program<'_>) {
        self.local_import_loaders.clear();
        let mut exported_names: Vec<String> = Vec::new();
        for stmt in &program.body {
            match stmt {
                Statement::VariableDeclaration(decl) => self.register_variable_loaders(decl, false),
                Statement::FunctionDeclaration(func) => self.register_function_loader(func, false),
                Statement::ExportDeclaration(export) => match &export.declaration {
                    Declaration::VariableDeclaration(decl) => {
                        self.register_variable_loaders(decl, true);
                    }
                    Declaration::FunctionDeclaration(func) => {
                        self.register_function_loader(func, true);
                    }
                    _ => {}
                },
                Statement::ExportNamedDeclaration(export) => {
                    exported_names.extend(
                        export
                            .specifiers
                            .iter()
                            .map(|spec| spec.local.name().to_string()),
                    );
                }
                _ => {}
            }
        }
        for name in exported_names {
            if let Some(loader) = self.local_import_loaders.get_mut(&name) {
                loader.is_exported = true;
            }
        }
    }

    fn register_variable_loaders(&mut self, decl: &VariableDeclaration<'_>, is_exported: bool) {
        if decl.kind != VariableDeclarationKind::Const {
            return;
        }
        for declarator in &decl.declarations {
            if let BindingPattern::BindingIdentifier(id) = &declarator.id
                && let Some(init) = &declarator.init
            {
                let imports = collect_loader_imports(init.without_parentheses());
                self.register_local_import_loader(id.name.as_str(), &imports, is_exported);
            }
        }
    }

    fn register_function_loader(&mut self, func: &Function<'_>, is_exported: bool) {
        if let Some(id) = &func.id
            && let Some(body) = &func.body
        {
            let mut imports = Vec::new();
            collect_returned_imports(&body.statements, &mut imports);
            self.register_local_import_loader(id.name.as_str(), &imports, is_exported);
        }
    }

    /// Register `name` as a loader when it returns at least one `import()` and
    /// every returned `import()` has a static specifier. A computed specifier
    /// keeps the old path, so the loader abstains as a whole.
    fn register_local_import_loader(
        &mut self,
        name: &str,
        import_exprs: &[&ImportExpression<'_>],
        is_exported: bool,
    ) {
        if import_exprs.is_empty() || self.local_import_loaders.contains_key(name) {
            return;
        }
        let mut imports = Vec::with_capacity(import_exprs.len());
        for import_expr in import_exprs {
            let mut sources = Vec::new();
            collect_static_import_specifiers(&import_expr.source, &mut sources);
            if sources.is_empty() {
                return;
            }
            imports.push(LoaderImport {
                span: import_expr.span,
                sources,
            });
        }
        for import in &imports {
            self.handled_import_spans.insert(import.span);
        }
        self.local_import_loaders.insert(
            name.to_string(),
            LocalImportLoader {
                imports,
                references: 0,
                credited_references: 0,
                is_exported,
            },
        );
    }

    /// Give each local loader that has a reference in an unknown shape, no
    /// credited reference, or an export the whole-module credit. The loader
    /// result can reach code that the walk cannot follow, so this credit can
    /// only remove false positives. A loader without a reference and without
    /// an export gets a bare edge: the target stays reachable, but the dead
    /// loader credits no export.
    pub(super) fn finish_local_import_loaders(&mut self) {
        let mut loaders: Vec<(String, LocalImportLoader)> =
            std::mem::take(&mut self.local_import_loaders)
                .into_iter()
                .collect();
        loaders.sort_unstable_by_key(|(_, loader)| loader.imports[0].span.start);
        for (name, loader) in loaders {
            if loader.references == 0 && !loader.is_exported {
                for import in &loader.imports {
                    self.push_dynamic_import_branches(&import.sources, import.span, &[], None);
                }
                continue;
            }
            if loader.is_exported
                || loader.credited_references == 0
                || loader.references > loader.credited_references
            {
                for import in &loader.imports {
                    self.push_dynamic_import_branches(
                        &import.sources,
                        import.span,
                        &[],
                        Some(&name),
                    );
                }
                self.push_whole_object_use(name);
            }
        }
    }

    /// Give each `import()` promise binding with a member read the
    /// whole-module credit. A promise binding without any use credits
    /// nothing, and a binding handed on or awaited is already a whole use.
    pub(super) fn finish_promise_import_bindings(&mut self) {
        let mut names: Vec<String> = std::mem::take(&mut self.promise_import_bindings)
            .into_iter()
            .collect();
        names.sort_unstable();
        for name in names {
            if self
                .member_accesses
                .iter()
                .any(|access| access.object == name)
            {
                self.push_whole_object_use(name);
            }
        }
    }

    /// Whether `expr` is a `next/dynamic(callback, { ssr: false })` call: the
    /// callee is the local binding of the `next/dynamic` default import, and the
    /// second argument is an object literal with a literal `ssr: false` property.
    fn is_next_dynamic_ssr_false_call(&self, expr: &CallExpression<'_>) -> bool {
        let Expression::Identifier(callee) = &expr.callee else {
            return false;
        };
        if self.component_loader_imports.get(callee.name.as_str())
            != Some(&ComponentLoaderImport::NextDynamic)
            || self.component_loader_binding_is_shadowed(&callee.name)
        {
            return false;
        }
        let Some(Argument::ObjectExpression(options)) = expr.arguments.get(1) else {
            return false;
        };
        options.properties.iter().any(|prop| {
            let ObjectPropertyKind::ObjectProperty(prop) = prop else {
                return false;
            };
            prop.key.static_name().as_deref() == Some("ssr")
                && matches!(&prop.value, Expression::BooleanLiteral(lit) if !lit.value)
        })
    }

    /// Record a dynamic-import glob pattern from an interpolated template
    /// literal (`import(\`./views/${name}.js\`)`): a relative-prefixed quasi
    /// becomes a `DynamicImportPattern`; multiple interpolations widen the
    /// prefix with a recursive `**/` segment.
    pub(super) fn record_dynamic_import_template_pattern(
        &mut self,
        tpl: &TemplateLiteral<'a>,
        span: Span,
    ) {
        let first_quasi = escape_glob_literal(&tpl.quasis[0].value.raw);
        if !(first_quasi.starts_with("./") || first_quasi.starts_with("../")) {
            return;
        }
        let prefix = if tpl.expressions.len() > 1 {
            format!("{first_quasi}**/")
        } else {
            first_quasi
        };
        let suffix = if tpl.quasis.len() > 1 {
            let last = &tpl.quasis[tpl.quasis.len() - 1];
            let s = escape_glob_literal(&last.value.raw);
            if s.is_empty() { None } else { Some(s) }
        } else {
            None
        };
        self.dynamic_import_patterns.push(DynamicImportPattern {
            prefix,
            suffix,
            span,
            mechanism: ModuleLoadMechanism::EsModule,
            excludes: Vec::new(),
        });
    }
}

/// The exclude globs of an `import.meta.glob` array: each negative entry
/// without its `!`. Vite reads a negative entry that starts with `./`, `../`
/// or `**`. Other negative entries need the alias resolver of Vite, so they
/// give no exclude, and the pattern keeps the files.
///
/// Vite matches a `**` entry relative to the common base directory of the
/// positive entries, not relative to the importing file. Each `**` exclude
/// therefore starts at that base. When the base is not known, the `**`
/// entries give no exclude.
fn import_meta_glob_excludes(entries: &[&str]) -> Vec<String> {
    let double_star_base = common_glob_base(entries);
    entries
        .iter()
        .filter_map(|entry| entry.strip_prefix('!'))
        .filter_map(|glob| {
            if glob.starts_with("./") || glob.starts_with("../") {
                return Some(glob.to_string());
            }
            if !glob.starts_with("**") {
                return None;
            }
            match double_star_base.as_deref() {
                Some("") => Some(glob.to_string()),
                Some(base) => Some(format!("{base}/{glob}")),
                None => None,
            }
        })
        .collect()
}

/// The common base directory of the positive `import.meta.glob` entries,
/// relative to the importing file: `""` for the importing directory, or a
/// path such as `./pages` or `../lib`. This follows `getCommonBase` of Vite.
///
/// Returns `None` when the base is not known from the entries alone: an entry
/// that is not relative, an entry with a backslash escape, or entries that go
/// up a different number of directories.
fn common_glob_base(entries: &[&str]) -> Option<String> {
    let mut bases = entries
        .iter()
        .filter(|entry| !entry.starts_with('!'))
        .map(|entry| relative_glob_base(entry));
    let (ups, mut common) = bases.next()??;
    for base in bases {
        let (base_ups, segments) = base?;
        if base_ups != ups {
            return None;
        }
        let shared = common
            .iter()
            .zip(&segments)
            .take_while(|(left, right)| left == right)
            .count();
        common.truncate(shared);
    }
    if ups == 0 && common.is_empty() {
        return Some(String::new());
    }
    let mut parts: Vec<&str> = if ups == 0 { vec!["."] } else { vec![".."; ups] };
    parts.extend(common.iter().map(String::as_str));
    Some(parts.join("/"))
}

/// The literal base directory of one relative glob, as the number of `..`
/// steps and the directory names after them. The base ends before the first
/// segment with a glob character. Like Vite, the base drops a last segment
/// that has a `.`, because such a segment usually names a file.
fn relative_glob_base(glob: &str) -> Option<(usize, Vec<String>)> {
    if !(glob.starts_with("./") || glob.starts_with("../")) || glob.contains('\\') {
        return None;
    }
    let mut ups = 0;
    let mut segments: Vec<String> = Vec::new();
    let literal = glob
        .split('/')
        .take_while(|segment| !segment.contains(['*', '?', '[', '{', '(']));
    for segment in literal {
        match segment {
            "" | "." => {}
            ".." => {
                if segments.pop().is_none() {
                    ups += 1;
                }
            }
            name => segments.push(name.to_string()),
        }
    }
    if segments.last().is_some_and(|name| name.contains('.')) {
        segments.pop();
    }
    Some((ups, segments))
}

/// Whether an `import.meta.glob` options argument sets `eager: true`, which
/// makes Vite import every match before the importing module runs.
fn is_eager_glob_options(argument: &Argument<'_>) -> bool {
    let Argument::ObjectExpression(options) = argument else {
        return false;
    };
    options.properties.iter().any(|property| {
        matches!(
            property,
            ObjectPropertyKind::ObjectProperty(property)
                if property.key.static_name().as_deref() == Some("eager")
                    && matches!(&property.value, Expression::BooleanLiteral(value) if value.value)
        )
    })
}

/// The receiver of `receiver.then(() => ...)` when the first callback is an
/// arrow function without parameters. An arrow has no own `arguments`, so it
/// cannot read the resolved value.
fn parameterless_then_object<'a, 'b>(call: &'b CallExpression<'a>) -> Option<&'b Expression<'a>> {
    let Expression::StaticMemberExpression(member) = &call.callee else {
        return None;
    };
    if member.property.name != "then" {
        return None;
    }
    let Some(Argument::ArrowFunctionExpression(arrow)) = call.arguments.first() else {
        return None;
    };
    (arrow.params.items.is_empty() && arrow.params.rest.is_none()).then_some(&member.object)
}