fallow-extract 3.32.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
#[allow(
    clippy::wildcard_imports,
    reason = "package-resolution helpers use many AST node shapes"
)]
use oxc_ast::ast::*;
use oxc_ast_visit::{Visit, walk};
use rustc_hash::{FxHashMap, FxHashSet};

use crate::DynamicImportInfo;
use fallow_types::extract::ImportLoadKind;

use super::super::ModuleInfoExtractor;
use super::{
    StaticPackageLoopBindings, for_of_binding_name, object_values_or_entries_argument_name,
};

/// The name of the CommonJS `require` function.
const REQUIRE: &str = "require";

/// Whether `expr` is `require.resolve`, by syntax only. The caller checks
/// that no local binding shadows `require`.
fn is_require_resolve_callee(expr: &Expression<'_>) -> bool {
    let Expression::StaticMemberExpression(member) = expr else {
        return false;
    };
    let Expression::Identifier(object) = &member.object else {
        return false;
    };
    object.name == REQUIRE && member.property.name == "resolve"
}

/// Whether `expr` is `import.meta.resolve`. Code cannot rebind `import.meta`,
/// so no shadow check applies.
fn is_import_meta_resolve_callee(expr: &Expression<'_>) -> bool {
    let Expression::StaticMemberExpression(member) = expr else {
        return false;
    };
    matches!(member.object, Expression::ImportMeta(_)) && member.property.name == "resolve"
}

/// The value of a string literal or of a template literal without expressions.
fn static_string_argument<'a>(argument: &'a Argument<'_>) -> Option<&'a str> {
    match argument {
        Argument::StringLiteral(lit) => Some(lit.value.as_str()),
        Argument::TemplateLiteral(tpl) if tpl.expressions.is_empty() => tpl
            .quasis
            .first()
            .and_then(|quasi| quasi.value.cooked.as_ref())
            .map(|cooked| cooked.as_str()),
        _ => None,
    }
}

/// Whether the program has top-level import or export syntax, the rule that
/// TypeScript uses to treat a file as a module and not as a script.
/// `export as namespace` alone does not make a module file.
pub(super) fn program_has_module_syntax(program: &Program<'_>) -> bool {
    program.body.iter().any(|statement| match statement {
        Statement::TSNamespaceExportDeclaration(_) => false,
        Statement::TSImportEqualsDeclaration(decl) => matches!(
            decl.module_reference,
            TSModuleReference::ExternalModuleReference(_)
        ),
        _ => statement.is_module_declaration(),
    })
}

/// Whether TypeScript reads the program as adding to the global scope: a
/// script file (no top-level import or export, see
/// [`program_has_module_syntax`]), or a module file with a top-level
/// `declare global` block or string-named `declare module` block (an ambient
/// module declaration or a module augmentation).
pub(super) fn program_has_global_declarations(program: &Program<'_>, is_module_file: bool) -> bool {
    !is_module_file
        || program.body.iter().any(|statement| {
            matches!(
                statement,
                Statement::TSGlobalDeclaration(_) | Statement::TSExternalModuleDeclaration(_)
            )
        })
}

/// The `path` values of every `/// <reference path="..." />` directive in the
/// program, in source order. `types`, `lib` and `no-default-lib` directives
/// name packages or compiler settings, not files, and are skipped.
pub(super) fn triple_slash_reference_paths(program: &Program<'_>) -> Vec<String> {
    program
        .comments
        .iter()
        .filter(|comment| comment.is_line())
        .filter_map(|comment| {
            reference_directive_path(comment.content_span().source_text(program.source_text))
        })
        .collect()
}

/// Parse the content of a line comment (the text after `//`) as a
/// `/ <reference path="..." />` directive and return its `path` value.
fn reference_directive_path(content: &str) -> Option<String> {
    let rest = content.strip_prefix('/')?.trim_start();
    let attributes = rest.strip_prefix("<reference")?;
    if !attributes.starts_with(char::is_whitespace) {
        return None;
    }
    let mut search = attributes;
    while let Some(index) = search.find("path") {
        let preceded_by_space = search[..index]
            .chars()
            .next_back()
            .is_some_and(char::is_whitespace);
        let after = &search[index + "path".len()..];
        if preceded_by_space && let Some(value) = after.trim_start().strip_prefix('=') {
            let value = value.trim_start();
            let quote = value.chars().next().filter(|c| *c == '"' || *c == '\'')?;
            let value = &value[quote.len_utf8()..];
            let end = value.find(quote)?;
            let path = value[..end].trim();
            return (!path.is_empty()).then(|| path.to_string());
        }
        search = after;
    }
    None
}

/// The package that a resolution specifier names: everything before the
/// first `/`, or before the second `/` for a scoped package. A subpath, such
/// as `pkg/lib/tsc`, resolves inside the package, so it names `pkg` too.
fn package_from_resolution_specifier(specifier: &str) -> Option<String> {
    if !is_package_resolution_specifier(specifier) {
        return None;
    }
    package_name_from_specifier(specifier)
}

/// The package of a specifier that names the package root: a bare package
/// name or `<pkg>/package.json`.
///
/// A path alias, such as `@/lib/x` or `src/lib/x` with a `baseUrl`, can look
/// like a package subpath. A resolve call does not go through the resolver,
/// so only a root specifier is sure enough to be an unlisted-dependency site.
fn package_root_from_resolution_specifier(specifier: &str) -> Option<String> {
    let package_name = package_from_resolution_specifier(specifier)?;
    let suffix = specifier
        .strip_prefix(package_name.as_str())
        .unwrap_or_default();
    (suffix.is_empty() || suffix == "/package.json").then_some(package_name)
}

fn is_package_resolution_specifier(specifier: &str) -> bool {
    if specifier.is_empty()
        || specifier.starts_with('.')
        || specifier.starts_with('/')
        || specifier.starts_with('#')
        || specifier.starts_with('$')
        || specifier.contains('\\')
        || specifier.contains(' ')
        || specifier.contains('?')
        || specifier.contains('!')
        || specifier.contains(':')
    {
        return false;
    }
    specifier
        .bytes()
        .any(|b| b.is_ascii_alphabetic() || b == b'@')
}

fn package_name_from_specifier(specifier: &str) -> Option<String> {
    if specifier.starts_with('@') {
        let mut parts = specifier.split('/');
        let scope = parts.next()?;
        let package = parts.next()?;
        if package.is_empty() {
            return None;
        }
        return Some(format!("{scope}/{package}"));
    }

    specifier
        .split('/')
        .next()
        .filter(|name| !name.is_empty())
        .map(str::to_string)
}

fn package_values_from_raw_values(values: &[String]) -> Vec<String> {
    values
        .iter()
        .filter_map(|value| package_from_resolution_specifier(value))
        .collect()
}

fn static_object_string_property_values(
    obj: &ObjectExpression<'_>,
) -> FxHashMap<String, Vec<String>> {
    let mut values = FxHashMap::default();
    collect_static_object_string_property_values(obj, &mut values);
    values
}

fn collect_static_object_string_property_values(
    obj: &ObjectExpression<'_>,
    values: &mut FxHashMap<String, Vec<String>>,
) {
    for prop in &obj.properties {
        let ObjectPropertyKind::ObjectProperty(prop) = prop else {
            continue;
        };
        let Some(key_name) = prop.key.static_name() else {
            continue;
        };
        match &prop.value {
            Expression::StringLiteral(lit) => {
                values
                    .entry(key_name.to_string())
                    .or_default()
                    .push(lit.value.to_string());
            }
            Expression::ObjectExpression(child) => {
                collect_static_object_string_property_values(child, values);
            }
            _ => {}
        }
    }
}

/// The path segment that leads to the installed binaries of a package manager.
const NODE_MODULES_BIN: &str = "node_modules/.bin/";

/// The binary names in each `node_modules/.bin/<name>` path segment of `text`.
///
/// The segment must start the text or follow a character that cannot be part
/// of a directory name, such as `/`, a space or a quote. The name ends at the
/// first character that a binary name does not use, such as `/`, a space or a
/// quote. A segment without a name gives nothing.
fn bin_names_in_text(text: &str) -> impl Iterator<Item = &str> {
    text.match_indices(NODE_MODULES_BIN)
        .filter(|(start, _)| {
            text[..*start]
                .chars()
                .next_back()
                .is_none_or(|prev| !is_bin_name_char(prev))
        })
        .filter_map(|(start, _)| {
            let rest = &text[start + NODE_MODULES_BIN.len()..];
            let end = rest
                .find(|ch: char| !is_bin_name_char(ch))
                .unwrap_or(rest.len());
            let name = &rest[..end];
            (!name.is_empty()).then_some(name)
        })
}

fn is_bin_name_char(ch: char) -> bool {
    ch.is_ascii_alphanumeric() || matches!(ch, '-' | '_' | '.' | '+' | '@')
}

impl ModuleInfoExtractor {
    /// Whether `callee` is `require.resolve` on the `require` of the module.
    ///
    /// A parameter or a declaration named `require` in a nested scope, such
    /// as `function f(require) { require.resolve('./x') }`, is some other
    /// function, so its calls reference nothing. A module-level
    /// `const require = createRequire(import.meta.url)` stays the module
    /// `require`: it resolves relative to the same file.
    fn is_module_require_resolve(&self, callee: &Expression<'_>) -> bool {
        is_require_resolve_callee(callee) && !self.nested_scope_shadows(REQUIRE)
    }

    pub(super) fn record_static_package_values(&mut self, name: &str, init: &Expression<'_>) {
        match init {
            Expression::StringLiteral(lit) => {
                self.static_string_bindings
                    .insert(name.to_string(), lit.value.to_string());
            }
            Expression::ArrayExpression(array) => {
                let values: Vec<String> = array
                    .elements
                    .iter()
                    .filter_map(|element| match element {
                        ArrayExpressionElement::StringLiteral(lit) => Some(lit.value.to_string()),
                        _ => None,
                    })
                    .collect();
                if !values.is_empty() {
                    self.static_string_arrays.insert(name.to_string(), values);
                }
            }
            Expression::ObjectExpression(obj) => {
                let values = static_object_string_property_values(obj);
                if !values.is_empty() {
                    self.static_object_property_values
                        .insert(name.to_string(), values);
                }
            }
            _ => {}
        }
    }

    pub(super) fn try_record_package_path_reference(&mut self, call: &CallExpression<'_>) {
        if (self.is_module_require_resolve(&call.callee)
            || is_import_meta_resolve_callee(&call.callee))
            && let Some(arg) = call.arguments.first()
        {
            let references = self.package_references_from_argument(arg);
            self.push_package_path_references(references);
            self.try_record_package_resolve_site(call);
        }

        if let Expression::Identifier(callee) = &call.callee
            && let Some(arg_index) = self
                .package_resolution_function_args
                .get(callee.name.as_str())
                .copied()
            && let Some(arg) = call.arguments.get(arg_index)
        {
            let references = self.package_references_from_argument(arg);
            self.push_package_path_references(references);
        }
    }

    /// Record the site of a direct `require.resolve('pkg')` call.
    ///
    /// The argument is a string literal or a template literal without
    /// expressions, so the call names the package at a known location. The
    /// specifier must name the package root, see
    /// [`package_root_from_resolution_specifier`]. A call
    /// with a second argument (the `paths` option) resolves from other
    /// directories and gets no site. Names from resolver functions, loop
    /// bindings and static tables also get no site: they only credit the
    /// dependency.
    fn try_record_package_resolve_site(&mut self, call: &CallExpression<'_>) {
        if call.arguments.len() != 1 {
            return;
        }
        let Some(package_name) = call
            .arguments
            .first()
            .and_then(static_string_argument)
            .and_then(package_root_from_resolution_specifier)
        else {
            return;
        };
        self.package_resolve_sites.push((package_name, call.span));
    }

    /// Record `require.resolve('./file')` as a reference to a project file.
    ///
    /// The call returns a path, and code hands that path to a consumer that
    /// static analysis cannot follow, such as a webpack module replacement or
    /// a worker. That consumer uses the whole module, so the edge credits every
    /// export. The call does not load the module, so the edge is a path
    /// reference and never closes a cycle.
    ///
    /// The argument is a string literal or a template literal without
    /// expressions. A call with a second argument (the `paths` option)
    /// resolves from other directories, so it is not recorded. The reference
    /// is speculative: a target that is not on disk, such as build output or a
    /// native addon, is dropped and does not become an unresolved import.
    pub(super) fn try_record_relative_require_resolve(&mut self, call: &CallExpression<'_>) {
        if !self.is_module_require_resolve(&call.callee) || call.arguments.len() != 1 {
            return;
        }
        let Some(source) = call.arguments.first().and_then(static_string_argument) else {
            return;
        };
        if !(source.starts_with("./") || source.starts_with("../")) {
            return;
        }
        self.dynamic_imports.push(DynamicImportInfo {
            source: source.to_string(),
            span: call.span,
            destructured_names: Vec::new(),
            local_name: Some(String::new()),
            is_speculative: true,
        });
        self.mark_import_load_kind(call.span, ImportLoadKind::PathReference);
    }

    /// Record the package that a module augmentation names.
    ///
    /// TypeScript treats `declare module 'pkg' { ... }` as an augmentation
    /// only in a module file, and there `pkg` must resolve. So the declaration
    /// is a type-only use of the package. In a script file the same syntax
    /// declares an ambient module, which uses nothing. A wildcard pattern such
    /// as `'*.svg'` and a relative path name no package.
    pub(super) fn record_module_augmentation(&mut self, decl: &TSExternalModuleDeclaration<'_>) {
        if !self.is_module_file || self.ambient_module_depth > 0 || decl.body.is_none() {
            return;
        }
        let specifier = decl.id.value.as_str();
        if specifier.contains('*') || !is_package_resolution_specifier(specifier) {
            return;
        }
        if let Some(package_name) = package_name_from_specifier(specifier)
            && !self.type_package_references.contains(&package_name)
        {
            self.type_package_references.push(package_name);
        }
    }

    /// Record the binary name of each `node_modules/.bin/<name>` path in the
    /// text of a string literal or a template quasi.
    ///
    /// Code hands such a path to a consumer that static analysis cannot
    /// follow, such as a child process. The analysis maps the name to the
    /// package that declares the binary and credits that package.
    pub(super) fn record_bin_path_references(&mut self, text: &str) {
        for name in bin_names_in_text(text) {
            if !self.bin_path_references.iter().any(|known| known == name) {
                self.bin_path_references.push(name.to_string());
            }
        }
    }

    fn push_package_path_references(&mut self, references: Vec<String>) {
        for package_name in references {
            if !self.package_path_references.contains(&package_name) {
                self.package_path_references.push(package_name);
            }
        }
    }

    fn package_references_from_argument(&self, arg: &Argument<'_>) -> Vec<String> {
        match arg {
            Argument::StringLiteral(lit) => package_from_resolution_specifier(lit.value.as_str())
                .into_iter()
                .collect(),
            Argument::TemplateLiteral(tpl) => self.package_references_from_template(tpl),
            Argument::Identifier(ident) => self.package_values_for_identifier(&ident.name),
            Argument::StaticMemberExpression(member) => {
                self.package_values_for_static_member(member)
            }
            _ => arg.as_expression().map_or_else(Vec::new, |expr| {
                self.package_references_from_expression(expr)
            }),
        }
    }

    fn package_references_from_expression(&self, expr: &Expression<'_>) -> Vec<String> {
        match expr {
            Expression::StringLiteral(lit) => package_from_resolution_specifier(lit.value.as_str())
                .into_iter()
                .collect(),
            Expression::TemplateLiteral(tpl) => self.package_references_from_template(tpl),
            Expression::Identifier(ident) => self.package_values_for_identifier(&ident.name),
            Expression::StaticMemberExpression(member) => {
                self.package_values_for_static_member(member)
            }
            _ => Vec::new(),
        }
    }

    fn package_references_from_template(&self, tpl: &TemplateLiteral<'_>) -> Vec<String> {
        if tpl.expressions.is_empty() {
            return tpl
                .quasis
                .first()
                .and_then(|quasi| package_from_resolution_specifier(quasi.value.raw.as_str()))
                .into_iter()
                .collect();
        }

        if tpl.expressions.len() != 1 || tpl.quasis.len() != 2 {
            return Vec::new();
        }

        let Some(first) = tpl.quasis.first() else {
            return Vec::new();
        };
        let Some(last) = tpl.quasis.last() else {
            return Vec::new();
        };
        if !first.value.raw.is_empty() || last.value.raw.as_str() != "/package.json" {
            return Vec::new();
        }

        self.package_references_from_expression(&tpl.expressions[0])
    }

    fn package_values_for_identifier(&self, name: &str) -> Vec<String> {
        for scope in self.loop_string_bindings.iter().rev() {
            if let Some(values) = scope.get(name) {
                return package_values_from_raw_values(values);
            }
        }

        self.static_string_bindings
            .get(name)
            .map_or_else(Vec::new, |value| {
                package_from_resolution_specifier(value)
                    .into_iter()
                    .collect()
            })
    }

    fn package_values_for_static_member(&self, member: &StaticMemberExpression<'_>) -> Vec<String> {
        let Expression::Identifier(object) = &member.object else {
            return Vec::new();
        };
        let property = member.property.name.as_str();

        for scope in self.loop_object_property_values.iter().rev() {
            if let Some(properties) = scope.get(object.name.as_str())
                && let Some(values) = properties.get(property)
            {
                return package_values_from_raw_values(values);
            }
        }

        self.static_object_property_values
            .get(object.name.as_str())
            .and_then(|properties| properties.get(property))
            .map_or_else(Vec::new, |values| package_values_from_raw_values(values))
    }

    pub(super) fn static_package_loop_bindings(
        &self,
        stmt: &ForOfStatement<'_>,
    ) -> Option<StaticPackageLoopBindings> {
        let loop_name = for_of_binding_name(&stmt.left)?;
        let mut strings = FxHashMap::default();
        let mut objects = FxHashMap::default();

        if let Expression::Identifier(iterable) = &stmt.right
            && let Some(values) = self.static_string_arrays.get(iterable.name.as_str())
        {
            strings.insert(loop_name.clone(), values.clone());
        }

        if let Some(object_name) = object_values_or_entries_argument_name(&stmt.right)
            && let Some(properties) = self.static_object_property_values.get(&object_name)
        {
            objects.insert(loop_name, properties.clone());
        }

        (!strings.is_empty() || !objects.is_empty()).then_some((strings, objects))
    }
}

pub(super) fn package_resolution_arg_index(
    params: &FormalParameters<'_>,
    body: &FunctionBody<'_>,
    known_helpers: &FxHashMap<String, usize>,
) -> Option<usize> {
    let param_names: Vec<String> = params
        .items
        .iter()
        .filter_map(|param| match &param.pattern {
            BindingPattern::BindingIdentifier(id) => Some(id.name.to_string()),
            _ => None,
        })
        .collect();
    let param_set: FxHashSet<String> = param_names.iter().cloned().collect();
    if params_bind_require(params) {
        return None;
    }
    let mut collector = PackageResolutionParamCollector {
        params: &param_set,
        known_helpers,
        matched: FxHashSet::default(),
        require_shadow_depth: 0,
    };
    collector.visit_function_body(body);

    param_names
        .iter()
        .position(|name| collector.matched.contains(name))
}

/// Whether a parameter list binds the name `require`, also through a
/// destructuring pattern.
fn params_bind_require(params: &FormalParameters<'_>) -> bool {
    params.items.iter().any(|param| {
        param
            .pattern
            .get_binding_identifiers()
            .iter()
            .any(|id| id.name == REQUIRE)
    })
}

struct PackageResolutionParamCollector<'p> {
    params: &'p FxHashSet<String>,
    known_helpers: &'p FxHashMap<String, usize>,
    matched: FxHashSet<String>,
    /// How many enclosing nested functions bind a `require` parameter. Inside
    /// such a function, `require.resolve` is not the module `require`.
    require_shadow_depth: usize,
}

impl<'a> Visit<'a> for PackageResolutionParamCollector<'_> {
    fn visit_function(&mut self, func: &Function<'a>, flags: oxc_semantic::ScopeFlags) {
        let shadows = params_bind_require(&func.params);
        self.require_shadow_depth += usize::from(shadows);
        walk::walk_function(self, func, flags);
        self.require_shadow_depth -= usize::from(shadows);
    }

    fn visit_arrow_function_expression(&mut self, expr: &ArrowFunctionExpression<'a>) {
        let shadows = params_bind_require(&expr.params);
        self.require_shadow_depth += usize::from(shadows);
        walk::walk_arrow_function_expression(self, expr);
        self.require_shadow_depth -= usize::from(shadows);
    }

    fn visit_call_expression(&mut self, call: &CallExpression<'a>) {
        if self.require_shadow_depth == 0
            && is_require_resolve_callee(&call.callee)
            && let Some(arg) = call.arguments.first()
            && let Some(param) = package_resolution_param_from_argument(arg, self.params)
        {
            self.matched.insert(param);
        }

        if call_joins_node_modules_with_param(call, self.params)
            && let Some(param) = call
                .arguments
                .iter()
                .find_map(|arg| package_param_argument_identifier_name(arg, self.params))
        {
            self.matched.insert(param);
        }

        if let Expression::Identifier(callee) = &call.callee
            && let Some(arg_index) = self.known_helpers.get(callee.name.as_str()).copied()
            && let Some(arg) = call.arguments.get(arg_index)
            && let Some(param) = package_param_argument_identifier_name(arg, self.params)
        {
            self.matched.insert(param);
        }

        walk::walk_call_expression(self, call);
    }
}

fn package_resolution_param_from_argument(
    arg: &Argument<'_>,
    params: &FxHashSet<String>,
) -> Option<String> {
    match arg {
        Argument::Identifier(ident) if params.contains(ident.name.as_str()) => {
            Some(ident.name.to_string())
        }
        Argument::TemplateLiteral(tpl)
            if tpl.expressions.len() == 1
                && tpl.quasis.len() == 2
                && tpl.quasis.first()?.value.raw.is_empty()
                && tpl.quasis.last()?.value.raw.as_str() == "/package.json" =>
        {
            package_param_expression_identifier_name(&tpl.expressions[0], params)
        }
        _ => arg
            .as_expression()
            .and_then(|expr| package_param_expression_identifier_name(expr, params)),
    }
}

fn call_joins_node_modules_with_param(
    call: &CallExpression<'_>,
    params: &FxHashSet<String>,
) -> bool {
    let has_node_modules = call.arguments.iter().any(
        |arg| matches!(arg, Argument::StringLiteral(lit) if lit.value.as_str() == "node_modules"),
    );
    has_node_modules
        && call
            .arguments
            .iter()
            .any(|arg| package_param_argument_identifier_name(arg, params).is_some())
}

fn package_param_argument_identifier_name(
    arg: &Argument<'_>,
    params: &FxHashSet<String>,
) -> Option<String> {
    match arg {
        Argument::Identifier(ident) if params.contains(ident.name.as_str()) => {
            Some(ident.name.to_string())
        }
        _ => arg
            .as_expression()
            .and_then(|expr| package_param_expression_identifier_name(expr, params)),
    }
}

fn package_param_expression_identifier_name(
    expr: &Expression<'_>,
    params: &FxHashSet<String>,
) -> Option<String> {
    match expr {
        Expression::Identifier(ident) if params.contains(ident.name.as_str()) => {
            Some(ident.name.to_string())
        }
        _ => None,
    }
}