cuke-dedup 0.4.0

Static analysis for duplicate and reusable Cucumber step definitions
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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
use super::ast::{
    import_has_runtime_bindings, import_module, is_type_only_declaration, is_type_only_specifier,
    string_literal,
};
use super::node_text;
use super::registrations::{registration_callee, RegistrationCallee, RegistrationNames};
use std::borrow::Cow;
use std::collections::{BTreeMap, BTreeSet};
use tree_sitter::Node;

// These modules expose the Jest-compatible `expect` API CukeDedup can identify without module
// resolution. Exact matching avoids treating unrelated package-name prefixes as trusted APIs.
const ASSERTION_MODULES: [&str; 4] = [
    "@playwright/test",
    "playwright/test",
    "@jest/globals",
    "expect",
];

#[derive(Default)]
pub(super) struct AssertionBindings {
    identifiers: BTreeSet<String>,
    namespaces: BTreeSet<String>,
    shadow_ranges: BTreeMap<String, Vec<(usize, usize)>>,
    mutated_matcher_factories: BTreeSet<String>,
}

impl AssertionBindings {
    pub(super) fn discover(
        root: Node<'_>,
        source: &[u8],
        registrations: &RegistrationNames,
    ) -> Self {
        let facade_modules = resolved_facade_modules(root, source, registrations);
        let (scopes, scope_ranges) = collect_binding_scopes(root, source);
        let shadow_ranges = collect_scoped_bindings(scopes.clone(), scope_ranges);
        let require_shadowed =
            module_runtime_binding_exists(root, source, "require", &shadow_ranges);
        let alias_writes = namespace_alias_writes(root, source, &scopes, false, require_shadowed);
        let mutated_matcher_factories =
            namespace_alias_writes(root, source, &scopes, true, require_shadowed);
        let mut bindings = Self::default();
        let mut shadowed = BTreeSet::new();
        let mut factory_writes = BTreeSet::new();
        let mut stack = vec![root];
        while let Some(node) = stack.pop() {
            match node.kind() {
                "import_statement" if import_has_runtime_bindings(node) => {
                    let trusted = bindings.collect_import(node, source, &facade_modules);
                    extend_untrusted_bindings(node, source, &trusted, &mut shadowed);
                }
                "variable_declarator" if is_top_level_variable(node) => {
                    // Binding names are file-scoped in this conservative model. Trust only
                    // module-scope CommonJS declarations so a nested helper cannot leak an
                    // assertion alias into unrelated step handlers.
                    let trusted = if !require_shadowed {
                        bindings.collect_require(node, source)
                    } else {
                        BTreeSet::new()
                    };
                    if let Some(name) = node.child_by_field_name("name") {
                        extend_untrusted_bindings(name, source, &trusted, &mut shadowed);
                    }
                }
                "variable_declarator" if is_module_var(node) => {
                    if let Some(name) = node.child_by_field_name("name") {
                        collect_binding_names(name, source, &mut shadowed);
                    }
                }
                "for_in_statement"
                    if node.child_by_field_name("left").is_some_and(|left| {
                        loop_binding_keyword(node, left) == Some("var")
                            && nearest_function_scope(node.parent()).is_none()
                    }) =>
                {
                    if let Some(left) = node.child_by_field_name("left") {
                        collect_binding_names(left, source, &mut shadowed);
                    }
                }
                "function_declaration"
                | "generator_function_declaration"
                | "class_declaration"
                | "abstract_class_declaration"
                | "enum_declaration"
                    if is_module_declaration(node) =>
                {
                    if let Some(name) = node.child_by_field_name("name") {
                        collect_binding_names(name, source, &mut shadowed);
                    }
                }
                "internal_module" | "module"
                    if !is_erased_declaration(node) && is_module_declaration(node) =>
                {
                    if let Some(name) = node.child_by_field_name("name") {
                        collect_binding_names(name, source, &mut shadowed);
                    }
                }
                _ => {
                    if let Some(left) = assignment_target(node) {
                        let mut assigned = BTreeSet::new();
                        let mut writes = BTreeMap::new();
                        collect_assignment_targets(left, source, &mut assigned, &mut writes, false);
                        factory_writes.extend(
                            writes
                                .into_keys()
                                .filter(|name| !position_is_shadowed(&shadow_ranges, name, node)),
                        );
                        shadowed.extend(
                            assigned
                                .into_iter()
                                .filter(|name| !position_is_shadowed(&shadow_ranges, name, node)),
                        );
                    }
                }
            }
            super::ast::push_named_children_reverse(node, &mut stack);
        }
        // Runtime declarations override trusted imports and aliases conservatively for the whole
        // file, matching step-registration discovery. A false negative is safer than assigning
        // assertion semantics to an unrelated function and manufacturing similarity evidence.
        shadowed.extend(factory_writes.intersection(&bindings.namespaces).cloned());
        shadowed.extend(alias_writes.intersection(&bindings.namespaces).cloned());
        bindings
            .identifiers
            .retain(|identifier| !shadowed.contains(identifier));
        bindings
            .namespaces
            .retain(|namespace| !shadowed.contains(namespace));
        // With no runtime declaration, retain the conventional ambient Jest/Playwright global.
        // A trusted namespace called `expect` still occupies the runtime name and is not itself
        // callable. Do not restore the ambient callable merely because the namespace provenance
        // is trusted; `expect.expect(...)` remains recognized through `namespaces`.
        if !shadowed.contains("expect") && !bindings.namespaces.contains("expect") {
            bindings.identifiers.insert("expect".to_owned());
        }
        bindings.shadow_ranges = shadow_ranges;
        bindings.mutated_matcher_factories = mutated_matcher_factories;
        bindings
    }

    pub(super) fn is_factory(&self, node: Node<'_>, source: &[u8]) -> bool {
        let Some(node) = super::registrations::unwrap_registration_callee(node) else {
            return false;
        };
        if node.kind() == "identifier" {
            let name = node_text(node, source);
            return self.identifiers.contains(name) && !self.is_locally_shadowed(node, name);
        }
        let Some(RegistrationCallee::Property { object, name }) = registration_callee(node, source)
        else {
            return false;
        };
        let namespace = node_text(object, source);
        object.kind() == "identifier"
            && self.namespaces.contains(namespace)
            && !self.is_locally_shadowed(object, namespace)
            && name == "expect"
    }

    pub(super) fn is_asymmetric_matcher(&self, node: Node<'_>, source: &[u8]) -> bool {
        self.asymmetric_matcher(node, source).is_some()
    }

    pub(super) fn asymmetric_matcher<'source>(
        &self,
        node: Node<'_>,
        source: &'source [u8],
    ) -> Option<(bool, Cow<'source, str>)> {
        if node.kind() != "call_expression" {
            return None;
        }
        let function = node
            .child_by_field_name("function")
            .and_then(super::registrations::unwrap_registration_callee)?;
        let RegistrationCallee::Property {
            mut object,
            name: matcher,
        } = registration_callee(function, source)?
        else {
            return None;
        };
        if !matches!(
            matcher.as_ref(),
            "objectContaining"
                | "arrayContaining"
                | "stringContaining"
                | "stringMatching"
                | "anything"
                | "any"
                | "closeTo"
        ) {
            return None;
        }
        let mut negated = false;
        if let Some(RegistrationCallee::Property { object: base, name }) =
            registration_callee(object, source)
        {
            if name == "not" {
                object = base;
                negated = true;
            }
        }
        let mut receiver = super::registrations::unwrap_registration_callee(object)?;
        while matches!(
            receiver.kind(),
            "member_expression" | "subscript_expression"
        ) {
            receiver = super::registrations::unwrap_registration_callee(
                receiver.child_by_field_name("object")?,
            )?;
        }
        (self.is_factory(object, source)
            && !self
                .mutated_matcher_factories
                .contains(node_text(receiver, source)))
        .then_some((negated, matcher))
    }

    fn is_locally_shadowed(&self, node: Node<'_>, expected: &str) -> bool {
        position_is_shadowed(&self.shadow_ranges, expected, node)
    }

    fn collect_import(
        &mut self,
        import: Node<'_>,
        source: &[u8],
        facade_modules: &BTreeSet<String>,
    ) -> BTreeSet<String> {
        let mut trusted = BTreeSet::new();
        if is_type_only_declaration(import) {
            return trusted;
        }
        let Some(module) = assertion_import_module(import, source) else {
            return trusted;
        };
        let exact_assertion_module = ASSERTION_MODULES.contains(&module);
        // Registration provenance is collected by module before this pass, so split imports from
        // one custom fixture retain the same trust without trusting unrelated local modules.
        let framework_facade = facade_modules.contains(module);
        if !exact_assertion_module && !framework_facade {
            return trusted;
        }

        let mut stack = vec![import];
        while let Some(node) = stack.pop() {
            match node.kind() {
                "import_specifier" if !is_type_only_specifier(node) => {
                    let Some(name) = node.child_by_field_name("name") else {
                        continue;
                    };
                    if node_text(name, source) == "expect"
                        || (module == "expect" && node_text(name, source) == "default")
                    {
                        let local = node.child_by_field_name("alias").unwrap_or(name);
                        let local = node_text(local, source).to_owned();
                        self.identifiers.insert(local.clone());
                        trusted.insert(local);
                    }
                }
                "namespace_import" if exact_assertion_module => {
                    if let Some(identifier) = node.named_child(0) {
                        let local = node_text(identifier, source).to_owned();
                        self.namespaces.insert(local.clone());
                        trusted.insert(local);
                    }
                }
                // The standalone `expect` package also provides a callable default export.
                "import_clause" if module == "expect" => {
                    if let Some(identifier) = node
                        .named_child(0)
                        .filter(|child| child.kind() == "identifier")
                    {
                        let local = node_text(identifier, source).to_owned();
                        self.identifiers.insert(local.clone());
                        trusted.insert(local);
                    }
                }
                "import_require_clause" => {
                    if let Some(local) = node
                        .named_child(0)
                        .filter(|child| child.kind() == "identifier")
                    {
                        let local = node_text(local, source).to_owned();
                        if module == "expect" {
                            self.identifiers.insert(local.clone());
                        } else if exact_assertion_module {
                            self.namespaces.insert(local.clone());
                        } else {
                            continue;
                        }
                        trusted.insert(local);
                    }
                }
                _ => {}
            }
            super::ast::push_named_children_reverse(node, &mut stack);
        }
        trusted
    }

    fn collect_require(&mut self, declaration: Node<'_>, source: &[u8]) -> BTreeSet<String> {
        let mut trusted = BTreeSet::new();
        let (Some(name), Some(value)) = (
            declaration.child_by_field_name("name"),
            declaration.child_by_field_name("value"),
        ) else {
            return trusted;
        };
        // A direct property read of the module is the factory when it names `expect`. Route it
        // through the shared resolver so the computed spelling is recognized as the dotted one is.
        let expect_property = (name.kind() == "identifier")
            .then(|| registration_callee(value, source))
            .flatten()
            .and_then(|callee| match callee {
                RegistrationCallee::Property { object, name } if name == "expect" => Some(object),
                _ => None,
            });
        let member_expect = expect_property.is_some();
        let required = expect_property.unwrap_or(value);
        let Some(module) = required_module(required, source) else {
            return trusted;
        };
        if !ASSERTION_MODULES.contains(&module) {
            return trusted;
        }
        match name.kind() {
            "identifier" if module == "expect" || member_expect => {
                let local = node_text(name, source).to_owned();
                self.identifiers.insert(local.clone());
                trusted.insert(local);
            }
            "identifier" => {
                let local = node_text(name, source).to_owned();
                self.namespaces.insert(local.clone());
                trusted.insert(local);
            }
            "object_pattern" => {
                // The trust pass wants names only; the distances matter to the mutation pass.
                let mut destructured = BTreeMap::new();
                collect_expect_pattern(
                    name,
                    source,
                    &mut destructured,
                    &mut trusted,
                    &mut BTreeMap::new(),
                    false,
                );
                self.identifiers.extend(destructured.into_keys());
            }
            _ => {}
        }
        trusted
    }
}

fn assertion_import_module<'a>(import: Node<'_>, source: &'a [u8]) -> Option<&'a str> {
    if let Some(module) = import_module(import, source) {
        return Some(module);
    }
    let mut cursor = import.walk();
    let module = import
        .named_children(&mut cursor)
        .find(|child| child.kind() == "import_require_clause")
        .and_then(|clause| clause.child_by_field_name("source"))
        .and_then(|literal| string_literal(literal, source));
    module
}

fn resolved_facade_modules(
    root: Node<'_>,
    source: &[u8],
    registrations: &RegistrationNames,
) -> BTreeSet<String> {
    let mut modules = BTreeSet::new();
    let mut stack = vec![root];
    while let Some(node) = stack.pop() {
        if node.kind() == "import_statement"
            && import_has_runtime_bindings(node)
            && import_has_resolved_registration(node, source, registrations)
        {
            if let Some(module) = import_module(node, source)
                .filter(|module| registrations.module_paths.contains_key(*module))
            {
                // Built-in registration packages are not evidence of an assertion facade.
                modules.insert(module.to_owned());
            }
        }
        super::ast::push_named_children_reverse(node, &mut stack);
    }
    let paths: BTreeSet<_> = modules
        .iter()
        .filter_map(|module| registrations.module_paths.get(module))
        .collect();
    modules.extend(
        registrations
            .module_paths
            .iter()
            .filter_map(|(module, path)| paths.contains(path).then_some(module.clone())),
    );
    modules
}

fn import_has_resolved_registration(
    import: Node<'_>,
    source: &[u8],
    registrations: &RegistrationNames,
) -> bool {
    let mut stack = vec![import];
    while let Some(node) = stack.pop() {
        match node.kind() {
            "import_specifier" if !is_type_only_specifier(node) => {
                if node
                    .child_by_field_name("alias")
                    .or_else(|| node.child_by_field_name("name"))
                    .is_some_and(|local| registrations.recognizes_alias(node_text(local, source)))
                {
                    return true;
                }
                continue;
            }
            "namespace_import"
                if node.named_child(0).is_some_and(|local| {
                    registrations.recognizes_namespace(node_text(local, source))
                }) =>
            {
                return true
            }
            _ => {}
        }
        super::ast::push_named_children_reverse(node, &mut stack);
    }
    false
}

fn extend_untrusted_bindings(
    root: Node<'_>,
    source: &[u8],
    trusted: &BTreeSet<String>,
    shadowed: &mut BTreeSet<String>,
) {
    let mut locals = BTreeSet::new();
    collect_binding_names(root, source, &mut locals);
    shadowed.extend(locals.into_iter().filter(|local| !trusted.contains(local)));
}

fn collect_binding_names(root: Node<'_>, source: &[u8], output: &mut BTreeSet<String>) {
    let mut stack = vec![root];
    while let Some(node) = stack.pop() {
        match node.kind() {
            "identifier"
            | "type_identifier"
            | "shorthand_property_identifier_pattern"
            | "namespace_import" => {
                output.insert(
                    node_text(node, source)
                        .trim_start_matches("* as ")
                        .to_owned(),
                );
            }
            "import_specifier" => {
                if !is_type_only_specifier(node) {
                    if let Some(local) = node
                        .child_by_field_name("alias")
                        .or_else(|| node.child_by_field_name("name"))
                    {
                        output.insert(node_text(local, source).to_owned());
                    }
                }
                continue;
            }
            // Object keys are property names; only the value introduces a local binding.
            "pair_pattern" => {
                if let Some(value) = node.child_by_field_name("value") {
                    stack.push(value);
                }
                continue;
            }
            "assignment_pattern" | "object_assignment_pattern" => {
                if let Some(left) = node
                    .child_by_field_name("left")
                    .or_else(|| node.named_child(0))
                {
                    stack.push(left);
                }
                continue;
            }
            "required_parameter" | "optional_parameter" => {
                if let Some(binding) = node
                    .child_by_field_name("name")
                    .or_else(|| node.child_by_field_name("pattern"))
                {
                    stack.push(binding);
                }
                continue;
            }
            "type_annotation" => continue,
            _ => {}
        }
        super::ast::push_named_children_reverse(node, &mut stack);
    }
}

/// Collects the bindings an assignment target names, and for each receiver the **depth** of the
/// property path written through it: `copy.not = x` is depth 1, `copy.not.objectContaining = x` is
/// depth 2. Depth separates replacing a slot from mutating the object held in it, which is exactly
/// the difference between a copy and the thing it was copied from. Where one receiver is written
/// at several depths the largest wins: a deep write reaches shared state whatever else the file
/// does.
fn collect_assignment_targets(
    root: Node<'_>,
    source: &[u8],
    output: &mut BTreeSet<String>,
    factory_writes: &mut BTreeMap<String, usize>,
    matcher_members: bool,
) {
    let mut stack = vec![(root, 0usize)];
    while let Some((node, depth)) = stack.pop() {
        match node.kind() {
            "identifier" | "shorthand_property_identifier_pattern" => {
                output.insert(node_text(node, source).to_owned());
            }
            // Only writes that can replace the factory invalidate namespace trust.
            "member_expression" | "subscript_expression" => {
                let property = node
                    .child_by_field_name("property")
                    .filter(|property| !node_text(*property, source).contains('\\'))
                    .map(|property| node_text(property, source).to_owned())
                    .or_else(|| {
                        // An unreadable key must invalidate trust, not look like an unrelated key.
                        node.child_by_field_name("index")
                            .and_then(|index| super::matcher::static_string_key(index, source))
                    });
                if property.is_none_or(|property| {
                    property == "expect" || matcher_members && is_matcher_member(property.as_str())
                }) {
                    if let Some(object) = node
                        .child_by_field_name("object")
                        .and_then(super::registrations::unwrap_registration_callee)
                    {
                        if matcher_members
                            && matches!(object.kind(), "member_expression" | "subscript_expression")
                        {
                            stack.push((object, depth + 1));
                        } else if object.kind() == "identifier" {
                            let reached = depth + 1;
                            factory_writes
                                .entry(node_text(object, source).to_owned())
                                .and_modify(|deepest| *deepest = (*deepest).max(reached))
                                .or_insert(reached);
                        }
                    }
                }
                continue;
            }
            "pair" | "pair_pattern" => {
                if let Some(value) = node.child_by_field_name("value") {
                    stack.push((value, depth));
                }
                continue;
            }
            "assignment_pattern" | "object_assignment_pattern" => {
                if let Some(left) = node
                    .child_by_field_name("left")
                    .or_else(|| node.named_child(0))
                {
                    stack.push((left, depth));
                }
                continue;
            }
            "type_annotation" => continue,
            _ => {}
        }
        let mut children = Vec::new();
        super::ast::push_named_children_reverse(node, &mut children);
        stack.extend(children.into_iter().map(|child| (child, depth)));
    }
}

fn assignment_target(node: Node<'_>) -> Option<Node<'_>> {
    match node.kind() {
        "assignment_expression" | "augmented_assignment_expression" => {
            node.child_by_field_name("left")
        }
        "update_expression" => node.child_by_field_name("argument"),
        "unary_expression"
            if node
                .child_by_field_name("operator")
                .is_some_and(|op| op.kind() == "delete") =>
        {
            node.child_by_field_name("argument")
        }
        "for_in_statement" => node
            .child_by_field_name("left")
            .filter(|left| loop_binding_keyword(node, *left).is_none()),
        _ => None,
    }
}

fn module_runtime_binding_exists(
    root: Node<'_>,
    source: &[u8],
    expected: &str,
    shadow_ranges: &BTreeMap<String, Vec<(usize, usize)>>,
) -> bool {
    let mut stack = vec![root];
    while let Some(node) = stack.pop() {
        let assignment = assignment_target(node);
        let pattern = match node.kind() {
            "import_statement" if import_has_runtime_bindings(node) => Some(node),
            "variable_declarator" if is_top_level_variable(node) => {
                node.child_by_field_name("name")
            }
            "variable_declarator" if is_module_var(node) => node.child_by_field_name("name"),
            "for_in_statement"
                if node.child_by_field_name("left").is_some_and(|left| {
                    loop_binding_keyword(node, left) == Some("var")
                        && nearest_function_scope(node.parent()).is_none()
                }) =>
            {
                node.child_by_field_name("left")
            }
            "function_declaration"
            | "generator_function_declaration"
            | "class_declaration"
            | "abstract_class_declaration"
            | "enum_declaration"
                if is_module_declaration(node) =>
            {
                node.child_by_field_name("name")
            }
            "internal_module" | "module"
                if !is_erased_declaration(node) && is_module_declaration(node) =>
            {
                node.child_by_field_name("name")
            }
            _ => assignment.filter(|left| {
                matches!(
                    left.kind(),
                    "identifier" | "object_pattern" | "array_pattern"
                ) && !position_is_shadowed(shadow_ranges, expected, node)
            }),
        };
        if let Some(pattern) = pattern {
            let mut names = BTreeSet::new();
            if assignment.is_some() {
                collect_assignment_targets(
                    pattern,
                    source,
                    &mut names,
                    &mut BTreeMap::new(),
                    false,
                );
            } else {
                collect_binding_names(pattern, source, &mut names);
            }
            if names.contains(expected) {
                return true;
            }
        }
        super::ast::push_named_children_reverse(node, &mut stack);
    }
    false
}

type BindingScopes = BTreeMap<usize, BTreeSet<String>>;
type ScopeRanges = BTreeMap<usize, (usize, usize)>;

fn namespace_alias_writes(
    root: Node<'_>,
    source: &[u8],
    scopes: &BindingScopes,
    matcher_members: bool,
    require_shadowed: bool,
) -> BTreeSet<String> {
    // A may-alias graph only removes trust; it never promotes an alias to an assertion
    // factory. Keep prior assignments conservatively and identify lexical bindings, not
    // spellings, so a shadowed local receiver cannot contaminate a module namespace.
    let key = |name: &str, mut node: Node<'_>| loop {
        if scopes
            .get(&node.id())
            .is_some_and(|names| names.contains(name))
        {
            return (node.id(), name.to_owned());
        }
        let Some(parent) = node.parent() else {
            return (root.id(), name.to_owned());
        };
        node = parent;
    };
    let mut required_aliases = BTreeMap::new();
    // Each edge records how many property levels the local sits below its owner, and whether the
    // step is a copy. An object rest binding holds the same property *values* as the object it
    // copied, so a write that reaches past its own slots reaches that object too, while a write to
    // a slot itself does not. Depth has to travel along the edges: `const negated = copy.not`
    // makes a depth-1 write on `negated` a depth-2 write on `copy`.
    let mut aliases = BTreeMap::<_, BTreeSet<(_, usize, bool)>>::new();
    let mut writes = BTreeMap::<_, usize>::new();
    let mut stack = vec![root];
    while let Some(node) = stack.pop() {
        let edge = match node.kind() {
            "variable_declarator" => node
                .child_by_field_name("name")
                .zip(node.child_by_field_name("value")),
            "assignment_pattern" | "object_assignment_pattern" => node
                .child_by_field_name("left")
                .zip(node.child_by_field_name("right")),
            "required_parameter" | "optional_parameter" => node
                .child_by_field_name("name")
                .or_else(|| node.child_by_field_name("pattern"))
                .zip(node.child_by_field_name("value")),
            "assignment_expression" => node
                .child_by_field_name("left")
                .zip(node.child_by_field_name("right")),
            "augmented_assignment_expression"
                if node
                    .child_by_field_name("operator")
                    .is_some_and(|operator| matches!(operator.kind(), "||=" | "&&=" | "??=")) =>
            {
                node.child_by_field_name("left")
                    .zip(node.child_by_field_name("right"))
            }
            _ => None,
        };
        if let Some((left, right)) = edge {
            if let Some(right) = super::registrations::unwrap_registration_callee(right) {
                let mut locals = BTreeMap::new();
                let mut shallow_locals = BTreeMap::new();
                let mut owners = BTreeMap::new();
                if left.kind() == "identifier" {
                    locals.insert(node_text(left, source).to_owned(), 0);
                } else if matcher_members && left.kind() == "object_pattern" {
                    collect_expect_pattern(
                        left,
                        source,
                        &mut locals,
                        &mut BTreeSet::new(),
                        &mut shallow_locals,
                        true,
                    );
                }
                let required = match registration_callee(right, source) {
                    Some(RegistrationCallee::Property { object, name }) if name == "expect" => {
                        object
                    }
                    _ => right,
                };
                // Same-module loads share mutation provenance, never callable factory trust.
                // Respect both module-level replacement and lexical shadowing of require.
                if matcher_members && !require_shadowed && key("require", required).0 == root.id() {
                    if let Some(module) = required_module(required, source)
                        .filter(|module| ASSERTION_MODULES.contains(module))
                    {
                        for (local, copied) in locals
                            .keys()
                            .map(|local| (local, false))
                            .chain(shallow_locals.keys().map(|local| (local, true)))
                        {
                            let local = key(local, left);
                            let owner = required_aliases
                                .entry(module)
                                .or_insert_with(|| local.clone());
                            aliases.entry(owner.clone()).or_default().insert((
                                local.clone(),
                                0,
                                false,
                            ));
                            aliases
                                .entry(local)
                                .or_default()
                                .insert((owner.clone(), 0, copied));
                        }
                    }
                }
                if right.kind() == "identifier" {
                    owners.insert(node_text(right, source).to_owned(), 0);
                } else if matcher_members
                    && matches!(right.kind(), "member_expression" | "subscript_expression")
                {
                    // Only direct property access establishes an alias, not a call's arguments.
                    let mut owner_writes = BTreeMap::new();
                    collect_assignment_targets(
                        right,
                        source,
                        &mut BTreeSet::new(),
                        &mut owner_writes,
                        true,
                    );
                    owners.extend(owner_writes);
                }
                for (local, local_delta, copied) in locals
                    .iter()
                    .map(|(local, delta)| (local, delta, false))
                    .chain(
                        shallow_locals
                            .iter()
                            .map(|(local, delta)| (local, delta, true)),
                    )
                {
                    for (owner, owner_delta) in &owners {
                        aliases.entry(key(local, left)).or_default().insert((
                            key(owner, right),
                            local_delta + owner_delta,
                            copied,
                        ));
                    }
                }
            }
        }
        if let Some(target) = assignment_target(node) {
            let mut receivers = BTreeMap::new();
            collect_assignment_targets(
                target,
                source,
                &mut BTreeSet::new(),
                &mut receivers,
                matcher_members,
            );
            for (name, depth) in receivers {
                writes
                    .entry(key(&name, node))
                    .and_modify(|deepest| *deepest = (*deepest).max(depth))
                    .or_insert(depth);
            }
        }
        super::ast::push_named_children_reverse(node, &mut stack);
    }
    // Record the deepest write already propagated from each binding rather than merely that it was
    // seen. A binding reached first by a shallow path and later by a deeper one must be revisited,
    // or the answer would depend on the order the file happens to be walked in.
    let mut deepest = BTreeMap::<_, usize>::new();
    let mut modules = BTreeSet::new();
    let mut pending: Vec<_> = writes.into_iter().collect();
    while let Some((binding, depth)) = pending.pop() {
        if deepest
            .get(&binding)
            .is_some_and(|reached| *reached >= depth)
        {
            continue;
        }
        deepest.insert(binding.clone(), depth);
        if binding.0 == root.id() {
            modules.insert(binding.1.clone());
        }
        if let Some(sources) = aliases.get(&binding) {
            for (source, delta, copied) in sources {
                // Replacing a slot on a copy changes nothing the copied object can observe, so a
                // write has to reach past that slot before it crosses. Without this, one
                // `copy.not = x` would revoke trust for every assertion the module reaches.
                if *copied && depth < 2 {
                    continue;
                }
                pending.push((source.clone(), depth + delta));
            }
        }
    }
    modules
}

fn collect_binding_scopes(root: Node<'_>, source: &[u8]) -> (BindingScopes, ScopeRanges) {
    let mut scopes = BTreeMap::<usize, BTreeSet<String>>::new();
    let mut scope_ranges = BTreeMap::new();
    let mut stack = vec![root];
    while let Some(node) = stack.pop() {
        if is_local_scope(node) {
            scope_ranges.insert(node.id(), (node.start_byte(), node.end_byte()));
        }
        match node.kind() {
            "function_declaration"
            | "generator_function_declaration"
            | "function_expression"
            | "generator_function"
            | "arrow_function"
            | "method_definition" => {
                if let Some(name) = node.child_by_field_name("name") {
                    // A method name is a class property, not a lexical binding visible in its
                    // body. Function and generator names, by contrast, support self-reference.
                    if node.kind() != "method_definition" {
                        add_scope_binding(&mut scopes, node, name, source);
                    }
                    if matches!(
                        node.kind(),
                        "function_declaration" | "generator_function_declaration"
                    ) {
                        if let Some(scope) = nearest_lexical_scope(node.parent()) {
                            add_scope_binding(&mut scopes, scope, name, source);
                        }
                    }
                }
                if let Some(parameters) = node
                    .child_by_field_name("parameters")
                    .or_else(|| node.child_by_field_name("parameter"))
                {
                    add_scope_binding(&mut scopes, node, parameters, source);
                }
            }
            "class_declaration" | "abstract_class_declaration" | "class" => {
                if let Some(name) = node.child_by_field_name("name") {
                    add_scope_binding(&mut scopes, node, name, source);
                    if node.kind() != "class" {
                        if let Some(scope) = nearest_lexical_scope(node.parent()) {
                            add_scope_binding(&mut scopes, scope, name, source);
                        }
                    }
                }
            }
            "catch_clause" => {
                if let Some(parameter) = node.child_by_field_name("parameter") {
                    add_scope_binding(&mut scopes, node, parameter, source);
                }
            }
            "for_statement" | "for_in_statement" => {
                if let Some(binding) = node
                    .child_by_field_name("initializer")
                    .or_else(|| node.child_by_field_name("left"))
                {
                    let keyword = match binding.kind() {
                        "variable_declaration" => Some("var"),
                        "lexical_declaration" => Some("let"),
                        _ => loop_binding_keyword(node, binding),
                    };
                    let target = if keyword == Some("var") {
                        nearest_function_scope(node.parent())
                    } else if matches!(keyword, Some("let" | "const" | "using")) {
                        Some(node)
                    } else {
                        None
                    };
                    if let Some(target) = target {
                        if matches!(
                            binding.kind(),
                            "lexical_declaration" | "variable_declaration"
                        ) {
                            collect_declaration_bindings(&mut scopes, target, binding, source);
                        } else {
                            add_scope_binding(&mut scopes, target, binding, source);
                        }
                    }
                }
            }
            "variable_declarator" if !is_top_level_variable(node) => {
                if let (Some(declaration), Some(name)) =
                    (node.parent(), node.child_by_field_name("name"))
                {
                    let scope = if declaration.kind() == "variable_declaration" {
                        nearest_function_scope(declaration.parent())
                    } else {
                        nearest_lexical_scope(declaration.parent())
                    };
                    if let Some(scope) = scope {
                        add_scope_binding(&mut scopes, scope, name, source);
                    }
                }
            }
            "enum_declaration" if !is_module_declaration(node) => {
                if let (Some(scope), Some(name)) = (
                    nearest_lexical_scope(node.parent()),
                    node.child_by_field_name("name"),
                ) {
                    add_scope_binding(&mut scopes, scope, name, source);
                }
            }
            "internal_module" | "module"
                if !is_erased_declaration(node) && !is_module_declaration(node) =>
            {
                if let (Some(scope), Some(name)) = (
                    nearest_lexical_scope(node.parent()),
                    node.child_by_field_name("name"),
                ) {
                    add_scope_binding(&mut scopes, scope, name, source);
                }
            }
            _ => {}
        }
        super::ast::push_named_children_reverse(node, &mut stack);
    }
    (scopes, scope_ranges)
}

fn collect_scoped_bindings(
    scopes: BindingScopes,
    scope_ranges: ScopeRanges,
) -> BTreeMap<String, Vec<(usize, usize)>> {
    let mut ranges = BTreeMap::<String, Vec<(usize, usize)>>::new();
    for (scope, names) in scopes {
        let Some(range) = scope_ranges.get(&scope).copied() else {
            continue;
        };
        for name in names {
            ranges.entry(name).or_default().push(range);
        }
    }
    for entries in ranges.values_mut() {
        entries.sort_unstable();
        let mut merged = Vec::<(usize, usize)>::with_capacity(entries.len());
        for &(start, end) in entries.iter() {
            if let Some(previous) = merged.last_mut().filter(|previous| start <= previous.1) {
                previous.1 = previous.1.max(end);
            } else {
                merged.push((start, end));
            }
        }
        *entries = merged;
    }
    ranges
}

fn position_is_shadowed(
    shadow_ranges: &BTreeMap<String, Vec<(usize, usize)>>,
    expected: &str,
    node: Node<'_>,
) -> bool {
    let Some(ranges) = shadow_ranges.get(expected) else {
        return false;
    };
    let index = ranges.partition_point(|(start, _)| *start <= node.start_byte());
    index > 0 && ranges[index - 1].1 >= node.end_byte()
}

fn add_scope_binding(
    scopes: &mut BTreeMap<usize, BTreeSet<String>>,
    scope: Node<'_>,
    pattern: Node<'_>,
    source: &[u8],
) {
    collect_binding_names(pattern, source, scopes.entry(scope.id()).or_default());
}

fn collect_declaration_bindings(
    scopes: &mut BTreeMap<usize, BTreeSet<String>>,
    scope: Node<'_>,
    declaration: Node<'_>,
    source: &[u8],
) {
    let mut cursor = declaration.walk();
    for declarator in declaration.named_children(&mut cursor) {
        if declarator.kind() == "variable_declarator" {
            if let Some(name) = declarator.child_by_field_name("name") {
                add_scope_binding(scopes, scope, name, source);
            }
        } else if matches!(
            declarator.kind(),
            "identifier" | "object_pattern" | "array_pattern"
        ) {
            add_scope_binding(scopes, scope, declarator, source);
        }
    }
}

fn loop_binding_keyword(loop_node: Node<'_>, binding: Node<'_>) -> Option<&'static str> {
    (0..loop_node.child_count()).find_map(|index| {
        let child = loop_node.child(index)?;
        (child.end_byte() <= binding.start_byte()).then(|| match child.kind() {
            "const" => Some("const"),
            "let" => Some("let"),
            "using" => Some("using"),
            "var" => Some("var"),
            _ => None,
        })?
    })
}

fn is_module_var(declarator: Node<'_>) -> bool {
    declarator
        .parent()
        .is_some_and(|declaration| declaration.kind() == "variable_declaration")
        && !is_erased_declaration(declarator)
        && nearest_function_scope(declarator.parent()).is_none()
}

fn is_local_scope(node: Node<'_>) -> bool {
    matches!(
        node.kind(),
        "function_declaration"
            | "generator_function_declaration"
            | "function_expression"
            | "generator_function"
            | "arrow_function"
            | "method_definition"
            | "class_declaration"
            | "abstract_class_declaration"
            | "class"
            | "class_static_block"
            | "internal_module"
            | "module"
            | "statement_block"
            | "for_statement"
            | "for_in_statement"
            | "switch_body"
            | "catch_clause"
    )
}

pub(super) fn nearest_function_scope(mut node: Option<Node<'_>>) -> Option<Node<'_>> {
    while let Some(candidate) = node {
        if matches!(
            candidate.kind(),
            "function_declaration"
                | "generator_function_declaration"
                | "function_expression"
                | "generator_function"
                | "arrow_function"
                | "method_definition"
                | "class_static_block"
                | "internal_module"
                | "module"
        ) {
            return Some(candidate);
        }
        if candidate.kind() == "program" {
            return None;
        }
        node = candidate.parent();
    }
    None
}

fn nearest_lexical_scope(mut node: Option<Node<'_>>) -> Option<Node<'_>> {
    while let Some(candidate) = node {
        if matches!(
            candidate.kind(),
            "statement_block"
                | "for_statement"
                | "for_in_statement"
                | "switch_body"
                | "catch_clause"
                | "class_static_block"
                | "internal_module"
                | "module"
        ) {
            return Some(candidate);
        }
        if candidate.kind() == "program" {
            return None;
        }
        node = candidate.parent();
    }
    None
}

fn is_top_level_variable(declarator: Node<'_>) -> bool {
    let Some(declaration) = declarator.parent() else {
        return false;
    };
    match declaration.parent() {
        Some(parent) if parent.kind() == "program" => true,
        Some(parent) if parent.kind() == "export_statement" => parent
            .parent()
            .is_some_and(|ancestor| ancestor.kind() == "program"),
        _ => false,
    }
}

fn is_module_declaration(node: Node<'_>) -> bool {
    let mut parent = node.parent();
    while let Some(candidate) = parent {
        match candidate.kind() {
            "program" => return true,
            // These grammar wrappers do not introduce a lexical scope for the declaration.
            "expression_statement" | "export_statement" => parent = candidate.parent(),
            _ => return false,
        }
    }
    false
}

fn is_erased_declaration(mut node: Node<'_>) -> bool {
    loop {
        if node.kind() == "ambient_declaration" {
            return true;
        }
        if node.kind() == "program" {
            return false;
        }
        let Some(parent) = node.parent() else {
            return false;
        };
        node = parent;
    }
}

fn required_module<'a>(call: Node<'_>, source: &'a [u8]) -> Option<&'a str> {
    if call.kind() != "call_expression" {
        return None;
    }
    let function = call.child_by_field_name("function")?;
    if function.kind() != "identifier" || node_text(function, source) != "require" {
        return None;
    }
    let arguments = call.child_by_field_name("arguments")?;
    string_literal(arguments.named_child(0)?, source)
}

/// Property names that reach a matcher builder from a trusted assertion factory.
///
/// Shared so the nested destructuring collector supports exactly the path the flat member walker
/// supports; a name handled by one and not the other makes equivalent spellings disagree.
fn is_matcher_member(name: &str) -> bool {
    matches!(
        name,
        "not"
            | "objectContaining"
            | "arrayContaining"
            | "stringContaining"
            | "stringMatching"
            | "anything"
            | "any"
            | "closeTo"
    )
}

/// Names the property a shorthand binding reads, or `None` when the spelling cannot be read.
///
/// An identifier may carry unicode escapes — `n\u006ft` is the identifier `not` — and nothing in
/// this module decodes them; every binding, write and registration is matched by raw spelling. An
/// unreadable spelling is therefore unknown, never a known unrelated property, so the mutation
/// pass keeps it as a possible alias instead of leaving trust in place. Two *different* spellings
/// of one identifier still do not match each other, which is the same limitation the flat alias
/// path has; normalizing here alone would make this view disagree with registration and shadowing
/// and silently lose real mutations.
fn shorthand_key_name(node: Node<'_>, source: &[u8]) -> Option<String> {
    let text = node_text(node, source);
    (!text.contains('\\')).then(|| text.to_owned())
}

fn collect_expect_pattern(
    pattern: Node<'_>,
    source: &[u8],
    identifiers: &mut BTreeMap<String, usize>,
    trusted: &mut BTreeSet<String>,
    shallow: &mut BTreeMap<String, usize>,
    include_defaults: bool,
) {
    // An explicit worklist rather than recursion. A bound here would have to decide what the
    // bindings below it mean, and both answers are wrong: stopping silently keeps trust that a
    // write should have removed, while sweeping everything below ignores the property filter the
    // rest of this walk applies and revokes trust a write never touched. Walking iteratively keeps
    // one rule at every depth and costs no stack.
    //
    // Each local is reported with how many property levels it sits below the destructured value,
    // so an alias edge can carry that distance: `const { not: negated } = copy` makes a depth-1
    // write on `negated` a depth-2 write on `copy`. Only the outermost level can name a trusted
    // factory — reaching deeper may remove trust but never establishes it.
    let mut pending = vec![(pattern, 0usize)];
    while let Some((pattern, depth)) = pending.pop() {
        let top_level = depth == 0;
        let mut cursor = pattern.walk();
        for property in pattern.named_children(&mut cursor) {
            match property.kind() {
                "object_assignment_pattern" if include_defaults => {
                    if let Some(left) = property.child_by_field_name("left") {
                        let name = shorthand_key_name(left, source);
                        if name
                            .as_deref()
                            .is_none_or(|name| name == "expect" || is_matcher_member(name))
                        {
                            identifiers.insert(node_text(left, source).to_owned(), depth + 1);
                        }
                    }
                }
                // An object rest binding copies the remaining property *values* into a fresh
                // object, so it holds the very objects the source holds. It is reported apart
                // from a direct alias because only writes that reach past its own slots can be
                // seen by the source.
                "rest_pattern" if include_defaults => {
                    if let Some(binding) = property.named_child(0) {
                        if binding.kind() == "identifier" {
                            // A rest binding holds the same properties as the value it copied,
                            // so it sits at that value's own level, not one below it.
                            shallow.insert(node_text(binding, source).to_owned(), depth);
                        }
                    }
                }
                "shorthand_property_identifier_pattern" => {
                    let name = shorthand_key_name(property, source);
                    let known_expect = name.as_deref() == Some("expect");
                    let known_not = name.as_deref().is_some_and(is_matcher_member);
                    if known_expect || (include_defaults && (known_not || name.is_none())) {
                        let local = node_text(property, source).to_owned();
                        identifiers.insert(local.clone(), depth + 1);
                        if known_expect && top_level {
                            trusted.insert(local);
                        }
                    }
                }
                "pair_pattern" => {
                    let (Some(key), Some(value)) = (
                        property.child_by_field_name("key"),
                        property.child_by_field_name("value"),
                    ) else {
                        continue;
                    };
                    let computed = key.kind() == "computed_property_name";
                    let key = if computed {
                        key.named_child(0).unwrap_or(key)
                    } else {
                        key
                    };
                    // Defaults can create mutation aliases, but cannot establish factory trust.
                    let value = if include_defaults
                        && matches!(
                            value.kind(),
                            "assignment_pattern" | "object_assignment_pattern"
                        ) {
                        value.child_by_field_name("left").unwrap_or(value)
                    } else {
                        value
                    };
                    // Mirror the property classification the flat alias path applies: a key the
                    // decoder cannot read is *unknown*, never a known unrelated property. An
                    // unknown key may still name the matcher, so the mutation pass has to keep it
                    // as a possible alias instead of stopping the descent and leaving trust in
                    // place.
                    let text = node_text(key, source);
                    let name = if key.kind() == "string" {
                        super::matcher::static_string_key(key, source)
                    } else if computed || text.contains('\\') {
                        None
                    } else {
                        Some(text.to_owned())
                    };
                    let known_expect = name.as_deref() == Some("expect");
                    let known_not = name.as_deref().is_some_and(is_matcher_member);
                    // Unknown computed keys may alias the factory, but cannot establish trust.
                    // include_defaults is used only by the trust-removing mutation pass.
                    let supported =
                        known_expect || (include_defaults && (known_not || name.is_none()));
                    if supported && value.kind() == "identifier" {
                        let local = node_text(value, source).to_owned();
                        identifiers.insert(local.clone(), depth + 1);
                        if known_expect && top_level {
                            trusted.insert(local);
                        }
                    } else if supported && include_defaults && value.kind() == "object_pattern" {
                        // `{ expect: { not: negated } }` names the same object as
                        // `api.expect.not`, so a write through either must revoke trust
                        // identically. An unsupported key stops the descent, matching the property
                        // filter the flat path applies.
                        pending.push((value, depth + 1));
                    }
                }
                _ => {}
            }
        }
    }
}