fallow-core 3.42.0

Internal detector backend for fallow-engine and fallow-api
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
888
889
//! Find the names whose value the program changes through another name.
//!
//! A config can change a value through an alias: `const r = common.resolve;
//! r.alias.a = x` changes `common`, and `const config = { entry };
//! config.entry.admin = x` changes `entry`. This module follows each change
//! along a key path, so `config.devtool = x` or `config.plugins.push(x)` does
//! not change `entry`.
//!
//! Only a change that the file shows counts: a write, a known mutating call
//! such as `push` or `Object.assign`, and a call to a function of the same
//! file whose body changes its parameter. A call to an imported or unknown
//! function does not count. A missed change can only hide an unused file, but
//! a change that is not real reports a used file as unused.

use oxc_ast::ast::*;
use oxc_ast_visit::{Visit, walk};
use oxc_syntax::scope::ScopeFlags;
use rustc_hash::{FxHashMap, FxHashSet};

use super::mutated_target;

/// The keys of a member chain after its root name, in source order. `None`
/// stands for a computed key, which can be any key.
type Chain = Vec<Option<String>>;

/// The maximum number of steps that one analysis takes: each change that it
/// records and each alias that it follows counts as one step.
const MAX_STEPS: usize = 200_000;

/// The maximum number of aliases that one analysis collects.
const MAX_EDGES: usize = 20_000;

/// The most steps that one analysis can count: each collected alias, plus
/// each step of the propagation up to the one that passes [`MAX_STEPS`].
#[cfg(test)]
pub(super) const MAX_TOTAL_STEPS: usize = MAX_EDGES + MAX_STEPS + 1;

#[cfg(test)]
thread_local! {
    /// The number of steps that the alias analysis took on this thread.
    static STEPS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}

/// Count `steps` steps of the alias analysis. Tests read the count to check
/// that the analysis stops at its limits.
fn count_steps(steps: usize) {
    #[cfg(test)]
    STEPS.set(STEPS.get() + steps);
    #[cfg(not(test))]
    let _ = steps;
}

/// The number of steps that the alias analysis took on this thread since the
/// last call. The count starts again at zero.
#[cfg(test)]
pub(super) fn take_steps() -> usize {
    STEPS.replace(0)
}

/// A change of the value that a name holds.
type NamedChange = (String, Change);

/// One change of a value, with the key chain under the changed name.
#[derive(Clone, PartialEq, Eq, Hash)]
enum Change {
    /// A write to a member, such as `x.a.b = 1` (chain `[a, b]`).
    Write(Chain),
    /// A mutating call on the value at the chain, such as `x.a.push(1)`.
    Mutation(Chain),
}

impl Change {
    /// The same change with the first `keep` keys of its chain.
    fn cut(self, keep: usize) -> Self {
        match self {
            Change::Write(mut chain) => {
                chain.truncate(keep);
                Change::Write(chain)
            }
            Change::Mutation(mut chain) => {
                chain.truncate(keep);
                Change::Mutation(chain)
            }
        }
    }
}

/// One step of the way that an object or array literal holds a value.
#[derive(Clone)]
enum Hold {
    /// A property with a static key, as `entry` in `{ entry }`.
    Key(String),
    /// An array element or a computed key, which can be any key.
    Any,
    /// A spread, as in `{ ...common }`. The copy has the same keys, but a
    /// write to a key of the copy does not change the spread value. The list
    /// holds the static keys that the same literal sets after the spread, as
    /// `plugins` in `{ ...common, plugins: [] }`: the copy does not share the
    /// value of such a key with the spread value.
    Spread(Vec<String>),
}

/// A name that holds the value of `root`, or a part of it. A change of `name`
/// with chain `c` is a change of the value at `prefix ++ c`. That value holds
/// the root value under `holds`, and `part` is the member chain from the root
/// value, as `[plugins]` in `const p = config.plugins`.
struct Edge {
    name: String,
    root: String,
    prefix: Chain,
    holds: Vec<Hold>,
    part: Chain,
}

impl Edge {
    /// The change of `root` that a change of `name` makes, if any. The
    /// result keeps the first `keep` keys of its chain.
    fn translate(&self, change: &Change, keep: usize) -> Option<Change> {
        let (chain, is_write) = match change {
            Change::Write(chain) => (chain, true),
            Change::Mutation(chain) => (chain, false),
        };
        let full: Chain = self.prefix.iter().chain(chain).cloned().collect();
        let mut index = 0;
        let mut shallow = 0;
        for hold in &self.holds {
            match hold {
                Hold::Key(key) => {
                    let found = full.get(index)?;
                    if found.as_ref().is_some_and(|found| found != key) {
                        return None;
                    }
                    index += 1;
                }
                Hold::Any => {
                    full.get(index)?;
                    index += 1;
                }
                Hold::Spread(overridden) => {
                    let first = full.get(index)?;
                    if first
                        .as_ref()
                        .is_some_and(|key| overridden.iter().any(|own| own == key))
                    {
                        return None;
                    }
                    shallow = 1;
                }
            }
        }
        let rest = &full[index..];
        let needed = if is_write { 1 + shallow } else { shallow };
        if rest.len() < needed {
            return None;
        }
        let chain: Chain = self.part.iter().chain(rest).take(keep).cloned().collect();
        Some(if is_write {
            Change::Write(chain)
        } else {
            Change::Mutation(chain)
        })
    }
}

/// The names that the program changes, directly, through an alias or through
/// a call to a local function. A name of the program that no change reaches
/// is not in the set.
///
/// When the program has more than [`MAX_EDGES`] aliases, or the analysis
/// needs more than [`MAX_STEPS`] steps, the result is empty: no change
/// through an alias counts, and the caller reads the literal values. A missed
/// change can only hide an unused file.
pub(super) fn changed_names(program: &Program<'_>) -> FxHashSet<String> {
    let Some((edges, changes)) = collect(program) else {
        return FxHashSet::default();
    };
    let keep = chain_keep(&edges);
    propagate(&edges, changes, keep).unwrap_or_default()
}

/// The aliases and the direct changes of the program. Returns `None` when
/// the program has more than [`MAX_EDGES`] aliases.
fn collect(program: &Program<'_>) -> Option<(Vec<Edge>, Vec<NamedChange>)> {
    let mut functions = FunctionFinder::default();
    functions.visit_program(program);
    let local: FxHashMap<String, Option<usize>> = functions
        .functions
        .into_iter()
        .filter(|(name, _)| functions.bindings.get(name) == Some(&1))
        .collect();
    let mut collector = Collector {
        local: &local,
        scopes: Vec::new(),
        next_function: None,
        edges: Vec::new(),
        changes: Vec::new(),
        overflow: false,
    };
    collector.visit_program(program);
    (!collector.overflow).then_some((collector.edges, collector.changes))
}

/// [`changed_names`] without the chain cut, for the test that shows that the
/// cut does not change the result.
#[cfg(test)]
fn changed_names_uncut(program: &Program<'_>) -> Option<FxHashSet<String>> {
    let (edges, changes) = collect(program)?;
    propagate(&edges, changes, usize::MAX)
}

/// The number of chain keys that the analysis keeps: the longest
/// `prefix.len() + holds.len()` of any edge, plus 2. One edge reads only
/// those keys of its prefixed chain and needs at most two more, so the cut
/// does not change the result of one edge. The cut bounds the number of
/// chains that a recursive function such as
/// `function mark(o) { o.seen = true; mark(o.next); }` can make.
///
/// A chain through more than one holder can lose a change. In
/// `holder.a.b.c.d.e.f.admin = x`, where `holder` holds `x` under `a.b.c` and
/// `x` holds `entry` under `d.e.f`, the cut keeps `[a, b, c, d, e]`, so the
/// second holder does not find `f` and `entry` does not change. Such a lost
/// change only makes fallow read the old value, which can hide an unused file
/// but never reports a used file as unused.
fn chain_keep(edges: &[Edge]) -> usize {
    edges
        .iter()
        .map(|edge| edge.prefix.len() + edge.holds.len())
        .max()
        .unwrap_or(0)
        + 2
}

/// Follow each change along the aliases to the roots, with each chain cut to
/// `keep` keys. Returns `None` when the analysis stops at [`MAX_STEPS`].
fn propagate(
    edges: &[Edge],
    changes: Vec<(String, Change)>,
    keep: usize,
) -> Option<FxHashSet<String>> {
    let mut edges_by_name: FxHashMap<&str, Vec<&Edge>> = FxHashMap::default();
    for edge in edges {
        edges_by_name
            .entry(edge.name.as_str())
            .or_default()
            .push(edge);
    }
    let mut steps = changes.len();
    if steps > MAX_STEPS {
        count_steps(steps);
        return None;
    }
    let changes: Vec<(String, Change)> = changes
        .into_iter()
        .map(|(name, change)| (name, change.cut(keep)))
        .collect();
    let mut seen: FxHashSet<(String, Change)> = changes.iter().cloned().collect();
    let mut pending = changes;
    while let Some((name, change)) = pending.pop() {
        for edge in edges_by_name.get(name.as_str()).into_iter().flatten() {
            steps += 1;
            if steps > MAX_STEPS {
                count_steps(steps);
                return None;
            }
            let Some(next) = edge.translate(&change, keep) else {
                continue;
            };
            let next = (edge.root.clone(), next);
            if seen.insert(next.clone()) {
                pending.push(next);
            }
        }
    }
    count_steps(steps);
    Some(seen.into_iter().map(|(name, _)| name).collect())
}

/// The functions of the program that a name binds, with the argument index
/// of the rest parameter, and the number of bindings of each name.
#[derive(Default)]
struct FunctionFinder {
    functions: Vec<(String, Option<usize>)>,
    bindings: FxHashMap<String, usize>,
}

impl<'a> Visit<'a> for FunctionFinder {
    fn visit_binding_identifier(&mut self, identifier: &BindingIdentifier<'a>) {
        *self
            .bindings
            .entry(identifier.name.to_string())
            .or_default() += 1;
    }

    fn visit_function(&mut self, func: &Function<'a>, flags: ScopeFlags) {
        if let Some(id) = &func.id
            && func.is_declaration()
        {
            self.functions
                .push((id.name.to_string(), rest_index(&func.params)));
        }
        walk::walk_function(self, func, flags);
    }

    fn visit_variable_declarator(&mut self, declarator: &VariableDeclarator<'a>) {
        if let (BindingPattern::BindingIdentifier(id), Some(init)) =
            (&declarator.id, &declarator.init)
            && let Some(params) = function_params(init)
        {
            self.functions
                .push((id.name.to_string(), rest_index(params)));
        }
        walk::walk_variable_declarator(self, declarator);
    }
}

/// The parameters of a function or arrow value.
fn function_params<'e, 'a>(value: &'e Expression<'a>) -> Option<&'e FormalParameters<'a>> {
    match value.get_inner_expression() {
        Expression::ArrowFunctionExpression(arrow) => Some(&arrow.params),
        Expression::FunctionExpression(func) => Some(&func.params),
        _ => None,
    }
}

fn rest_index(params: &FormalParameters<'_>) -> Option<usize> {
    params.rest.as_ref().map(|_| params.items.len())
}

/// The name of the value that a call passes to argument `index` of a local
/// function. No source identifier contains `#`.
fn argument_name(function: &str, index: usize) -> String {
    format!("{function}#{index}")
}

/// Collects the aliases and the direct changes of the program.
///
/// Inside the body of a local function, each parameter name stands for the
/// value of its argument: a change of the parameter is a change of the
/// argument name of that function. A call of the function binds each
/// argument name to the argument value. So a body that changes its parameter
/// changes the value that each call passes, and a body that only reads it
/// changes nothing.
struct Collector<'l> {
    /// The rest argument index of each local function, by name.
    local: &'l FxHashMap<String, Option<usize>>,
    /// The name that each parameter stands for, one map for each function
    /// that the walk is in.
    scopes: Vec<FxHashMap<String, String>>,
    /// The name of the local function that the next function value binds.
    next_function: Option<String>,
    edges: Vec<Edge>,
    changes: Vec<(String, Change)>,
    /// Set when the program has more than [`MAX_EDGES`] aliases.
    overflow: bool,
}

impl Collector<'_> {
    fn push_edge(&mut self, edge: Edge) {
        if self.edges.len() >= MAX_EDGES {
            self.overflow = true;
            return;
        }
        count_steps(1);
        self.edges.push(edge);
    }

    /// The name that `name` stands for in the current function.
    fn resolve(&self, name: &str) -> String {
        self.scopes
            .iter()
            .rev()
            .find_map(|scope| scope.get(name))
            .map_or_else(|| name.to_owned(), Clone::clone)
    }

    /// Record that `name`, at `prefix`, holds `value`.
    fn bind(&mut self, name: &str, prefix: &Chain, value: &Expression<'_>) {
        let name = self.resolve(name);
        self.bind_held(&name, prefix, value, &mut Vec::new(), &[]);
    }

    fn bind_held(
        &mut self,
        name: &str,
        prefix: &Chain,
        value: &Expression<'_>,
        holds: &mut Vec<Hold>,
        suffix: &[Option<String>],
    ) {
        match value.without_parentheses().get_inner_expression() {
            Expression::LogicalExpression(logical) => {
                self.bind_held(name, prefix, &logical.left, holds, suffix);
                self.bind_held(name, prefix, &logical.right, holds, suffix);
            }
            Expression::ConditionalExpression(conditional) => {
                self.bind_held(name, prefix, &conditional.consequent, holds, suffix);
                self.bind_held(name, prefix, &conditional.alternate, holds, suffix);
            }
            Expression::AssignmentExpression(assign) => {
                self.bind_held(name, prefix, &assign.right, holds, suffix);
            }
            Expression::SequenceExpression(sequence) => {
                if let Some(last) = sequence.expressions.last() {
                    self.bind_held(name, prefix, last, holds, suffix);
                }
            }
            Expression::ArrayExpression(array) => {
                holds.push(Hold::Any);
                for element in &array.elements {
                    match element {
                        ArrayExpressionElement::SpreadElement(spread) => {
                            // `[...list]` holds each element of `list`.
                            let suffix: Chain = std::iter::once(None)
                                .chain(suffix.iter().cloned())
                                .collect();
                            self.bind_held(name, prefix, &spread.argument, holds, &suffix);
                        }
                        element => {
                            if let Some(expr) = element.as_expression() {
                                self.bind_held(name, prefix, expr, holds, suffix);
                            }
                        }
                    }
                }
                holds.pop();
            }
            Expression::ObjectExpression(object) => {
                for (position, property) in object.properties.iter().enumerate() {
                    match property {
                        ObjectPropertyKind::ObjectProperty(property) => {
                            let hold = static_property_key(property)
                                .map_or(Hold::Any, |key| Hold::Key(key.to_owned()));
                            holds.push(hold);
                            self.bind_held(name, prefix, &property.value, holds, suffix);
                            holds.pop();
                        }
                        ObjectPropertyKind::SpreadProperty(spread) => {
                            let literal = matches!(
                                spread.argument.get_inner_expression(),
                                Expression::ObjectExpression(_)
                            );
                            if !literal {
                                let overridden = object.properties[position + 1..]
                                    .iter()
                                    .filter_map(|later| match later {
                                        ObjectPropertyKind::ObjectProperty(later) => {
                                            static_property_key(later).map(str::to_owned)
                                        }
                                        ObjectPropertyKind::SpreadProperty(_) => None,
                                    })
                                    .collect();
                                holds.push(Hold::Spread(overridden));
                            }
                            self.bind_held(name, prefix, &spread.argument, holds, suffix);
                            if !literal {
                                holds.pop();
                            }
                        }
                    }
                }
            }
            other => {
                if let Some((root, keys)) = member_chain(other) {
                    let root = self.resolve(root);
                    self.push_edge(Edge {
                        name: name.to_owned(),
                        root,
                        prefix: prefix.clone(),
                        holds: holds.clone(),
                        part: keys.into_iter().chain(suffix.iter().cloned()).collect(),
                    });
                }
            }
        }
    }

    /// Bind each name of a binding pattern to `value`.
    fn bind_pattern(&mut self, pattern: &BindingPattern<'_>, value: &Expression<'_>) {
        let mut names = Vec::new();
        pattern_names(pattern, &mut Vec::new(), &mut names);
        for (name, prefix) in names {
            self.bind(&name, &prefix, value);
        }
    }

    fn push_change(&mut self, root: &str, change: Change) {
        let root = self.resolve(root);
        self.changes.push((root, change));
    }

    fn record_mutation(&mut self, expr: &Expression<'_>) {
        if let Some((root, chain)) = member_chain(expr) {
            self.push_change(root, Change::Mutation(chain));
        }
    }

    fn record_member_write(&mut self, member: &MemberExpression<'_>) {
        let Some((root, mut chain)) = member_chain(member.object()) else {
            return;
        };
        chain.push(member_key(member));
        self.push_change(root, Change::Write(chain));
    }

    /// A call of a local function binds each argument name to its value.
    fn bind_arguments(&mut self, callee: &Expression<'_>, arguments: &[Argument<'_>]) {
        let Expression::Identifier(id) = callee.without_parentheses() else {
            return;
        };
        if self.resolve(id.name.as_str()) != id.name.as_str() {
            return;
        }
        let Some(&rest) = self.local.get(id.name.as_str()) else {
            return;
        };
        for (index, argument) in arguments.iter().enumerate() {
            let value = match argument {
                Argument::SpreadElement(spread) => &spread.argument,
                argument => match argument.as_expression() {
                    Some(expr) => expr,
                    None => continue,
                },
            };
            let target = rest.map_or(index, |rest| index.min(rest));
            let name = argument_name(id.name.as_str(), target);
            self.bind_held(&name, &Vec::new(), value, &mut Vec::new(), &[]);
        }
    }

    /// Enter a function: map each parameter name to the argument name of a
    /// local function, or to itself for any other function, so that an outer
    /// parameter with the same name does not apply.
    fn enter_function(&mut self, local: Option<&str>, params: &FormalParameters<'_>) {
        let mut scope = FxHashMap::default();
        let rest = rest_index(params);
        let items = params
            .items
            .iter()
            .map(|item| &item.pattern)
            .chain(params.rest.iter().map(|rest| &rest.rest.argument));
        for (index, pattern) in items.enumerate() {
            let mut names = Vec::new();
            pattern_names(pattern, &mut Vec::new(), &mut names);
            for (name, prefix) in names {
                let Some(function) = local else {
                    scope.insert(name.clone(), name);
                    continue;
                };
                let argument = argument_name(function, index);
                let stand_in = format!("{argument}#{name}");
                // A rest parameter holds each later argument as an element.
                let holds = if rest == Some(index) {
                    vec![Hold::Any]
                } else {
                    Vec::new()
                };
                self.push_edge(Edge {
                    name: stand_in.clone(),
                    root: argument,
                    prefix,
                    holds,
                    part: Vec::new(),
                });
                scope.insert(name, stand_in);
            }
        }
        self.scopes.push(scope);
    }

    /// The local function name of a function: its own declared name, or the
    /// name of the binding whose value it is.
    fn local_name(&mut self, own: Option<&BindingIdentifier<'_>>) -> Option<String> {
        let bound = self.next_function.take();
        own.map(|id| id.name.to_string())
            .or(bound)
            .filter(|name| self.local.contains_key(name))
    }
}

impl<'a> Visit<'a> for Collector<'_> {
    fn visit_variable_declarator(&mut self, declarator: &VariableDeclarator<'a>) {
        if let Some(init) = &declarator.init {
            self.bind_pattern(&declarator.id, init);
            if let BindingPattern::BindingIdentifier(id) = &declarator.id
                && function_params(init).is_some()
            {
                self.next_function = Some(id.name.to_string());
            }
        }
        walk::walk_variable_declarator(self, declarator);
        self.next_function = None;
    }

    fn visit_function(&mut self, func: &Function<'a>, flags: ScopeFlags) {
        let own = func.id.as_ref().filter(|_| func.is_declaration());
        let local = self.local_name(own);
        self.enter_function(local.as_deref(), &func.params);
        walk::walk_function(self, func, flags);
        self.scopes.pop();
    }

    fn visit_arrow_function_expression(&mut self, arrow: &ArrowFunctionExpression<'a>) {
        let local = self.local_name(None);
        self.enter_function(local.as_deref(), &arrow.params);
        walk::walk_arrow_function_expression(self, arrow);
        self.scopes.pop();
    }

    fn visit_assignment_expression(&mut self, assign: &AssignmentExpression<'a>) {
        if let AssignmentTarget::AssignmentTargetIdentifier(target) = &assign.left {
            self.bind(target.name.as_str(), &Vec::new(), &assign.right);
        } else if let Some(member) = assign
            .left
            .as_simple_assignment_target()
            .and_then(SimpleAssignmentTarget::as_member_expression)
        {
            self.record_member_write(member);
        } else if assign.left.as_assignment_target_pattern().is_some() {
            // A destructuring assignment binds each target name to a part of
            // the value, and writes each member target.
            let mut names = Vec::new();
            let mut members = Vec::new();
            pattern_targets(&assign.left, &mut names, &mut members);
            for name in names {
                self.bind(&name, &vec![None], &assign.right);
            }
            for member in members {
                self.record_member_write(member);
            }
        }
        walk::walk_assignment_expression(self, assign);
    }

    fn visit_update_expression(&mut self, expression: &UpdateExpression<'a>) {
        if let Some(member) = expression.argument.as_member_expression() {
            self.record_member_write(member);
        }
        walk::walk_update_expression(self, expression);
    }

    fn visit_unary_expression(&mut self, expression: &UnaryExpression<'a>) {
        if expression.operator == UnaryOperator::Delete
            && let Some(member) = expression.argument.as_member_expression()
        {
            self.record_member_write(member);
        }
        walk::walk_unary_expression(self, expression);
    }

    fn visit_call_expression(&mut self, call: &CallExpression<'a>) {
        if let Some(target) = mutated_target(call) {
            self.record_mutation(target);
        }
        self.bind_arguments(&call.callee, &call.arguments);
        walk::walk_call_expression(self, call);
    }

    fn visit_for_of_statement(&mut self, statement: &ForOfStatement<'a>) {
        // `for (const c of list)` binds `c` to each element of `list`.
        let mut names = Vec::new();
        match &statement.left {
            ForStatementLeft::VariableDeclaration(decl) => {
                for declarator in &decl.declarations {
                    pattern_names(&declarator.id, &mut Vec::new(), &mut names);
                }
            }
            left => {
                if let Some(target) = left.as_assignment_target() {
                    names.extend(
                        target_names(target)
                            .into_iter()
                            .map(|name| (name, Vec::new())),
                    );
                }
            }
        }
        for (name, prefix) in names {
            let name = self.resolve(&name);
            match statement.right.get_inner_expression() {
                Expression::ArrayExpression(array) => {
                    for element in &array.elements {
                        let value = match element {
                            ArrayExpressionElement::SpreadElement(spread) => &spread.argument,
                            element => match element.as_expression() {
                                Some(expr) => expr,
                                None => continue,
                            },
                        };
                        self.bind_held(&name, &prefix, value, &mut Vec::new(), &[]);
                    }
                }
                right => self.bind_held(&name, &prefix, right, &mut Vec::new(), &[None]),
            }
        }
        walk::walk_for_of_statement(self, statement);
    }
}

/// The names that a binding pattern binds, each with its key chain in the
/// bound value. A rest element can hold any part, so it keeps the chain of
/// its pattern.
fn pattern_names(
    pattern: &BindingPattern<'_>,
    prefix: &mut Chain,
    names: &mut Vec<(String, Chain)>,
) {
    match pattern {
        BindingPattern::BindingIdentifier(id) => {
            names.push((id.name.to_string(), prefix.clone()));
        }
        BindingPattern::ObjectPattern(object) => {
            for property in &object.properties {
                let key = if property.computed {
                    None
                } else {
                    match &property.key {
                        PropertyKey::StaticIdentifier(id) => Some(id.name.to_string()),
                        PropertyKey::StringLiteral(literal) => {
                            literal.value.as_str().map(str::to_owned)
                        }
                        _ => None,
                    }
                };
                prefix.push(key);
                pattern_names(&property.value, prefix, names);
                prefix.pop();
            }
            if let Some(rest) = &object.rest {
                pattern_names(&rest.argument, prefix, names);
            }
        }
        BindingPattern::ArrayPattern(array) => {
            for element in array.elements.iter().flatten() {
                prefix.push(None);
                pattern_names(element, prefix, names);
                prefix.pop();
            }
            if let Some(rest) = &array.rest {
                pattern_names(&rest.argument, prefix, names);
            }
        }
        BindingPattern::AssignmentPattern(assignment) => {
            pattern_names(&assignment.left, prefix, names);
        }
    }
}

/// The identifier names in an assignment target, such as `r` and `a` in
/// `{ resolve: r, a }`. A name in a default value or a computed key is only
/// read, so it is not a target.
fn target_names(target: &AssignmentTarget<'_>) -> Vec<String> {
    let mut names = Vec::new();
    pattern_targets(target, &mut names, &mut Vec::new());
    names
}

/// The identifier targets and the member targets of an assignment target.
fn pattern_targets<'e>(
    target: &'e AssignmentTarget<'_>,
    names: &mut Vec<String>,
    members: &mut Vec<&'e MemberExpression<'e>>,
) {
    match target {
        AssignmentTarget::AssignmentTargetIdentifier(id) => names.push(id.name.to_string()),
        AssignmentTarget::ArrayAssignmentTarget(array) => {
            for element in array.elements.iter().flatten() {
                maybe_default_targets(element, names, members);
            }
            if let Some(rest) = &array.rest {
                pattern_targets(&rest.target, names, members);
            }
        }
        AssignmentTarget::ObjectAssignmentTarget(object) => {
            for property in &object.properties {
                match property {
                    AssignmentTargetProperty::AssignmentTargetPropertyIdentifier(shorthand) => {
                        names.push(shorthand.binding.name.to_string());
                    }
                    AssignmentTargetProperty::AssignmentTargetPropertyProperty(property) => {
                        maybe_default_targets(&property.binding, names, members);
                    }
                }
            }
            if let Some(rest) = &object.rest {
                pattern_targets(&rest.target, names, members);
            }
        }
        other => {
            if let Some(member) = other
                .as_simple_assignment_target()
                .and_then(SimpleAssignmentTarget::as_member_expression)
            {
                members.push(member);
            }
        }
    }
}

fn maybe_default_targets<'e>(
    target: &'e AssignmentTargetMaybeDefault<'_>,
    names: &mut Vec<String>,
    members: &mut Vec<&'e MemberExpression<'e>>,
) {
    match target {
        AssignmentTargetMaybeDefault::AssignmentTargetWithDefault(with_default) => {
            pattern_targets(&with_default.binding, names, members);
        }
        other => {
            if let Some(target) = other.as_assignment_target() {
                pattern_targets(target, names, members);
            }
        }
    }
}

/// The key of a property that is not computed, such as `entry` in `{ entry }`
/// or in `{ "entry": value }`.
fn static_property_key<'p>(property: &'p ObjectProperty<'_>) -> Option<&'p str> {
    if property.computed {
        return None;
    }
    match &property.key {
        PropertyKey::StaticIdentifier(id) => Some(id.name.as_str()),
        PropertyKey::StringLiteral(literal) => literal.value.as_str(),
        _ => None,
    }
}

/// The root name of a member chain and its keys in source order, such as
/// `config` and `[entry, admin]` for `config.entry.admin`. A computed key with
/// a string literal is a static key, and another computed key is `None`.
fn member_chain<'e>(expr: &'e Expression<'_>) -> Option<(&'e str, Chain)> {
    let mut keys = Vec::new();
    let mut current = expr.without_parentheses().get_inner_expression();
    loop {
        let member = match current {
            Expression::Identifier(identifier) => {
                keys.reverse();
                return Some((identifier.name.as_str(), keys));
            }
            Expression::ChainExpression(chain) => chain.expression.as_member_expression()?,
            other => other.as_member_expression()?,
        };
        keys.push(member_key(member));
        current = member.object().without_parentheses().get_inner_expression();
    }
}

fn member_key(member: &MemberExpression<'_>) -> Option<String> {
    member
        .static_property_name()
        .and_then(|name| name.as_str().map(str::to_owned))
}

#[cfg(test)]
mod tests {
    use oxc_allocator::Allocator;
    use oxc_parser::Parser;
    use oxc_span::SourceType;

    use super::{changed_names, changed_names_uncut};

    /// Configs from the alias tests, each without recursion, so the analysis
    /// without the chain cut ends too.
    const SOURCES: &[&str] = &[
        "const entry = {}; const config = { mode: 1, entry, plugins: [] }; const plugins = config.plugins; plugins.push(1);",
        "const entry = {}; const config = { mode: 1, entry, plugins: [] }; const { plugins } = config; plugins.push(1);",
        "const entry = {}; const config = { entry }; const c2 = config; c2.devtool = 1; c2.entry.admin = 2;",
        "const entry = {}; const config = { entry, plugins: [] }; function add(cfg) { cfg.plugins.push(1); } add(config);",
        "const entry = {}; const config = { entry }; function set(cfg) { cfg.entry.admin = 1; } set(config);",
        "const entry = {}; const holder = { a: { entry, list: [] } }; holder.a.list.push(1); holder.a.entry.x = 1;",
        "const common = { entry: {} }; const prod = { ...common, plugins: [] }; prod.plugins.push(1); prod.entry.a = 1;",
        "const entry = {}; const config = { entry }; Object.assign(config.entry, {}); config?.entry?.push?.(1);",
        "const entry = {}; const config = { entry }; const { entry: e } = config; e.admin = 1; config['entry'].x = 2;",
        "const entry = {}; const list = [entry]; for (const c of list) c.x = 1; list[0].y = 2;",
        "const entry = {}; let mode; ({ mode = entry.mode, a: entry.b } = {});",
        "const a = {}; const b = { k: { l: { m: a } } }; const c = { n: b }; c.n.k.l.m.x = 1; c.n.k.other = 1;",
    ];

    #[test]
    fn chain_cut_gives_the_same_changed_names() {
        for source in SOURCES {
            let allocator = Allocator::default();
            let parsed = Parser::new(&allocator, source, SourceType::cjs()).parse();
            let uncut = changed_names_uncut(&parsed.program).expect("no limit applies");
            assert_eq!(changed_names(&parsed.program), uncut, "{source}");
        }
    }
}