perl-semantic-analyzer 0.13.3

Semantic analysis and symbol extraction for Perl
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
//! Export symbol extraction for Exporter-based Perl modules
//!
//! This module provides functionality to extract export information from Perl modules
//! that use the Exporter framework. It detects four inheritance patterns and parses
//! the `@EXPORT`, `@EXPORT_OK`, and `%EXPORT_TAGS` arrays.
//!
//! # Exporter Detection Patterns
//!
//! A module is considered an Exporter if it matches any of:
//! - `use Exporter;` (Use node with module="Exporter" and empty args — bare form)
//! - `use Exporter 'import';` (Use node with module="Exporter" and args containing "import")
//! - `use parent 'Exporter';` or `use parent qw(Exporter);` (Use node with module="parent")
//! - `use base 'Exporter';` or `use base qw(Exporter);` (Use node with module="base")
//! - `our @ISA = qw(Exporter);` (VariableDeclaration with @ISA array containing "Exporter")
//! - `@ISA = qw(Exporter);` (bare Assignment with @ISA array containing "Exporter")
//!
//! # Export Array Format
//!
//! The parser supports all Perl qw() delimiters:
//! - `@EXPORT = qw(foo bar)` — parentheses
//! - `@EXPORT = [qw(foo bar)]` — brackets
//! - `@EXPORT = qw<foo bar>` — angle brackets
//! - `@EXPORT = qw/foo bar/` — slashes
//! - `@EXPORT = qw|foo bar|` — pipes
//!
//! Both `our @EXPORT = ...` (VariableDeclaration) and bare `@EXPORT = ...` (Assignment)
//! forms are extracted.

use crate::ast::{Node, NodeKind};
use perl_semantic_facts::{AnchorId, Confidence, ExportSet, ExportTag, Provenance};
use std::collections::{HashMap, HashSet};

/// Information extracted from an Exporter-based module.
#[derive(Debug, Clone, Default)]
pub struct ExportInfo {
    /// Symbols exported via `@EXPORT` (default exports)
    pub default_export: HashSet<String>,
    /// Symbols exported via `@EXPORT_OK` (optional exports)
    pub optional_export: HashSet<String>,
    /// Tag-based exports via `%EXPORT_TAGS` (tag name -> symbols)
    pub export_tags: HashMap<String, Vec<String>>,
    /// Package name extracted from the AST's `package` declaration.
    pub module_name: Option<String>,
    /// Anchor ID derived from the first export declaration's byte span.
    pub anchor_id: Option<AnchorId>,
}

impl ExportInfo {
    /// Convert extracted Exporter data into canonical semantic export facts.
    #[must_use]
    pub fn to_export_set(&self) -> ExportSet {
        let mut default_exports: Vec<String> = self.default_export.iter().cloned().collect();
        default_exports.sort();

        let mut optional_exports: Vec<String> = self.optional_export.iter().cloned().collect();
        optional_exports.sort();

        let mut tags: Vec<ExportTag> = self
            .export_tags
            .iter()
            .map(|(name, members)| {
                let mut members = members.clone();
                members.sort();
                members.dedup();
                ExportTag { name: name.clone(), members }
            })
            .collect();
        tags.sort_by(|left, right| left.name.cmp(&right.name));

        ExportSet {
            default_exports,
            optional_exports,
            tags,
            provenance: Provenance::ImportExportInference,
            confidence: Confidence::High,
            module_name: self.module_name.clone(),
            anchor_id: self.anchor_id,
        }
    }
}

/// Detection method for Exporter inheritance.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExporterDetector {
    /// Detected via `use Exporter;` or `use Exporter 'import';`
    UseExporterImport,
    /// Detected via `use parent 'Exporter';` or `use parent qw(Exporter ...)`
    UseParentExporter,
    /// Detected via `use base 'Exporter';` or `use base qw(Exporter ...)`
    UseBaseExporter,
    /// Detected via `our @ISA = qw(Exporter ...);` or bare `@ISA = qw(Exporter ...);`
    OurIsaExporter,
}

/// Export symbol extractor for Exporter-based Perl modules.
///
/// This extractor walks the AST to:
/// 1. Detect if a module uses Exporter (via one of four patterns)
/// 2. Parse `@EXPORT`, `@EXPORT_OK`, and `%EXPORT_TAGS` assignments
pub struct ExportSymbolExtractor;

impl ExportSymbolExtractor {
    /// Extract export information from an AST.
    ///
    /// Returns `None` if the module does not use Exporter.
    /// Returns `Some(ExportInfo)` with empty sets if the module uses Exporter
    /// but does not define any export arrays.
    pub fn extract(ast: &Node) -> Option<ExportInfo> {
        let detector = Self::detect_exporter_inheritance(ast)?;

        let mut info = ExportInfo {
            // Extract the package name from the AST.
            module_name: Self::find_package_name(ast),
            // Derive anchor_id from the first export declaration's byte span.
            anchor_id: Self::find_first_export_anchor(ast),
            ..Default::default()
        };

        // Walk the AST to find export array assignments
        Self::walk_and_extract_exports(ast, &detector, &mut info);

        Some(info)
    }

    /// Detect if the AST represents an Exporter-based module.
    ///
    /// Checks for four patterns:
    /// 1. `use Exporter;` or `use Exporter 'import';`
    /// 2. `use parent 'Exporter';` or `use parent qw(Exporter ...)`
    /// 3. `use base 'Exporter';` or `use base qw(Exporter ...)`
    /// 4. `our @ISA = qw(Exporter ...);` or bare `@ISA = qw(Exporter ...);`
    fn detect_exporter_inheritance(ast: &Node) -> Option<ExporterDetector> {
        Self::walk_for_exporter_detection(ast)
    }

    /// Walk the AST to find the first `package` declaration and return its name.
    fn find_package_name(ast: &Node) -> Option<String> {
        match &ast.kind {
            NodeKind::Package { name, .. } => Some(name.clone()),
            _ => {
                for child in ast.children() {
                    if let Some(name) = Self::find_package_name(child) {
                        return Some(name);
                    }
                }
                None
            }
        }
    }

    /// Walk the AST to find the first `@EXPORT`, `@EXPORT_OK`, or `%EXPORT_TAGS`
    /// declaration and derive an [`AnchorId`] from its byte-offset span.
    fn find_first_export_anchor(ast: &Node) -> Option<AnchorId> {
        Self::walk_for_first_export_anchor(ast)
    }

    /// Recursive helper for [`Self::find_first_export_anchor`].
    fn walk_for_first_export_anchor(node: &Node) -> Option<AnchorId> {
        match &node.kind {
            // `our @EXPORT = ...` or `our @EXPORT_OK = ...` or `our %EXPORT_TAGS = ...`
            NodeKind::VariableDeclaration { variable, initializer: Some(_), .. } => {
                if let NodeKind::Variable { sigil, name } = &variable.kind {
                    let is_export_var = (sigil == "@" && (name == "EXPORT" || name == "EXPORT_OK"))
                        || (sigil == "%" && name == "EXPORT_TAGS");
                    if is_export_var {
                        return Some(AnchorId(node.location.start as u64));
                    }
                }
            }
            // `@EXPORT = ...` or `@EXPORT_OK = ...` or `%EXPORT_TAGS = ...` (bare)
            NodeKind::Assignment { lhs, .. } => {
                if let NodeKind::Variable { sigil, name } = &lhs.kind {
                    let is_export_var = (sigil == "@" && (name == "EXPORT" || name == "EXPORT_OK"))
                        || (sigil == "%" && name == "EXPORT_TAGS");
                    if is_export_var {
                        return Some(AnchorId(node.location.start as u64));
                    }
                }
            }
            _ => {}
        }

        for child in node.children() {
            if let Some(anchor) = Self::walk_for_first_export_anchor(child) {
                return Some(anchor);
            }
        }

        None
    }

    /// Walk AST looking for Exporter inheritance patterns.
    fn walk_for_exporter_detection(ast: &Node) -> Option<ExporterDetector> {
        match &ast.kind {
            // Pattern 1: `use Exporter 'import';` or `use Exporter;` (no-args form)
            //
            // `use Exporter;` without 'import' is valid and extremely common in CPAN code —
            // the module is loaded but callers must invoke `Exporter::import` explicitly, or
            // rely on `@EXPORT` being populated before import time.  We treat both forms as
            // Exporter-based so that @EXPORT/@EXPORT_OK are still extracted.
            NodeKind::Use { module, args, .. } if module == "Exporter" => {
                // Accept `use Exporter;` (args empty) or `use Exporter 'import';`
                if args.is_empty()
                    || args.iter().any(|arg| {
                        let arg_stripped = arg.trim_matches('\'');
                        arg_stripped == "import" || arg == "import"
                    })
                {
                    return Some(ExporterDetector::UseExporterImport);
                }
            }
            // Pattern 2: `use parent 'Exporter';` or `use parent qw(Exporter ...)`
            //
            // The parser stores qw-lists as a single normalised string like `"qw(Exporter)"`,
            // so we must check both single-quoted strings and the qw-expanded form.
            NodeKind::Use { module, args, .. } if module == "parent" => {
                if args.iter().any(|arg| Self::arg_contains_exporter(arg)) {
                    return Some(ExporterDetector::UseParentExporter);
                }
            }
            // Pattern 3: `use base 'Exporter';` or `use base qw(Exporter ...)`
            //
            // `use base` is the older form of `use parent` and is still widely used in
            // legacy CPAN code. The same qw-normalisation applies.
            NodeKind::Use { module, args, .. } if module == "base" => {
                if args.iter().any(|arg| Self::arg_contains_exporter(arg)) {
                    return Some(ExporterDetector::UseBaseExporter);
                }
            }
            // Pattern 4a: `our @ISA = qw(Exporter ...);` (declared form)
            NodeKind::VariableDeclaration { variable, initializer: Some(init), .. } => {
                if let NodeKind::Variable { sigil, name } = &variable.kind {
                    if sigil == "@" && name == "ISA" && Self::initializer_contains_exporter(init) {
                        return Some(ExporterDetector::OurIsaExporter);
                    }
                }
            }
            // Pattern 4b: `@ISA = qw(Exporter ...);` (bare assignment without `our`)
            NodeKind::Assignment { lhs, rhs, .. } => {
                if let NodeKind::Variable { sigil, name } = &lhs.kind {
                    if sigil == "@" && name == "ISA" && Self::initializer_contains_exporter(rhs) {
                        return Some(ExporterDetector::OurIsaExporter);
                    }
                }
            }
            _ => {}
        }

        // If no pattern matched at this node, recurse into children.
        // This handles cases where Exporter inheritance is declared in nested scopes
        // or after other statements in the package body.
        for child in ast.children() {
            if let Some(detector) = Self::walk_for_exporter_detection(child) {
                return Some(detector);
            }
        }

        None
    }

    /// Check whether a single `Use` argument string contains `Exporter`.
    ///
    /// The parser normalises `qw(Foo Bar)` forms to the string `"qw(Foo Bar)"`.
    /// Single-quoted module names arrive as `"'Exporter'"`.
    fn arg_contains_exporter(arg: &str) -> bool {
        let arg = arg.trim();
        // Single- or double-quoted: 'Exporter' or "Exporter"
        if arg.trim_matches('\'').trim_matches('"') == "Exporter" {
            return true;
        }
        // qw(...) normalised form: "qw(Exporter)" or "qw(SomeBase Exporter OtherBase)"
        if arg.starts_with("qw") {
            // Find the content between the outer delimiters
            let open_pos = arg.find(|c: char| !c.is_alphanumeric()).unwrap_or(arg.len());
            let close = match arg[open_pos..].chars().next() {
                Some('(') => ')',
                Some('{') => '}',
                Some('[') => ']',
                Some('<') => '>',
                Some(c) => c,
                None => return false,
            };
            if let (Some(start), Some(end)) =
                (arg[open_pos..].find(|c: char| !c.is_whitespace()), arg.rfind(close))
            {
                let content = &arg[open_pos + start + 1..end];
                return content.split_whitespace().any(|w| w == "Exporter");
            }
        }
        false
    }

    /// Check if an initializer node contains 'Exporter'.
    fn initializer_contains_exporter(init: &Node) -> bool {
        match &init.kind {
            // Array or list literal (e.g., qw(Exporter) or [qw(Exporter)])
            NodeKind::ArrayLiteral { elements } => elements.iter().any(Self::node_is_exporter),
            // For simple strings
            NodeKind::String { value, .. } => {
                let s_stripped = value.trim_matches('\'');
                s_stripped == "Exporter" || value == "Exporter"
            }
            _ => false,
        }
    }

    /// Check if a node contains 'Exporter'.
    fn node_is_exporter(node: &Node) -> bool {
        match &node.kind {
            NodeKind::String { value, .. } => {
                let s_stripped = value.trim_matches('\'');
                s_stripped == "Exporter" || value == "Exporter"
            }
            NodeKind::ArrayLiteral { elements } => elements.iter().any(Self::node_is_exporter),
            _ => false,
        }
    }

    /// Walk AST and extract export arrays.
    ///
    /// The `_detector` parameter is accepted but unused (marked with underscore prefix).
    /// It is kept in the signature for API symmetry with the detection phase and to allow
    /// future pattern-specific extraction logic without changing the interface.
    fn walk_and_extract_exports(ast: &Node, _detector: &ExporterDetector, info: &mut ExportInfo) {
        match &ast.kind {
            // `our @EXPORT = qw(...)` (declared form)
            NodeKind::VariableDeclaration { variable, initializer: Some(init), .. } => {
                if let NodeKind::Variable { sigil, name } = &variable.kind {
                    if sigil == "@" {
                        match name.as_str() {
                            "EXPORT" => {
                                let symbols = Self::parse_qw_array(init);
                                info.default_export.extend(symbols);
                            }
                            "EXPORT_OK" => {
                                let symbols = Self::parse_qw_array(init);
                                info.optional_export.extend(symbols);
                            }
                            _ => {}
                        }
                    } else if sigil == "%" && name == "EXPORT_TAGS" {
                        let tags = Self::parse_export_tags(init);
                        info.export_tags.extend(tags);
                    }
                }

                // Continue walking for nested declarations
                Self::walk_and_extract_exports(init, _detector, info);
            }
            // `@EXPORT = qw(...)` (bare assignment without `our`)
            NodeKind::Assignment { lhs, rhs, .. } => {
                if let NodeKind::Variable { sigil, name } = &lhs.kind {
                    if sigil == "@" {
                        match name.as_str() {
                            "EXPORT" => {
                                let symbols = Self::parse_qw_array(rhs);
                                info.default_export.extend(symbols);
                            }
                            "EXPORT_OK" => {
                                let symbols = Self::parse_qw_array(rhs);
                                info.optional_export.extend(symbols);
                            }
                            _ => {}
                        }
                    } else if sigil == "%" && name == "EXPORT_TAGS" {
                        let tags = Self::parse_export_tags(rhs);
                        info.export_tags.extend(tags);
                    }
                }
                // Walk into rhs for nested assignments
                Self::walk_and_extract_exports(rhs, _detector, info);
            }
            _ => {
                // Walk children
                for child in ast.children() {
                    Self::walk_and_extract_exports(child, _detector, info);
                }
            }
        }
    }

    /// Parse a qw() array from an initializer node.
    ///
    /// Handles all Perl qw delimiters: (), [], {}, <>, //, ||
    ///
    /// The input node can be:
    /// - An ArrayLiteral with String elements (from `qw(...)`)
    /// - An ArrayLiteral with one ArrayLiteral element (from `[qw(...)]`)
    /// - A HashLiteral (from `%EXPORT_TAGS = (...)`)
    /// - Other expression types
    fn parse_qw_array(node: &Node) -> Vec<String> {
        match &node.kind {
            // ArrayLiteral: `(1, 2, 3)` or `[1, 2, 3]` containing strings
            NodeKind::ArrayLiteral { elements } => {
                if elements.is_empty() {
                    return Vec::new();
                }
                // Check if this ArrayLiteral contains only one element which is itself an ArrayLiteral
                // This happens with `[qw(tag_a tag_b)]` where the outer [...] creates an ArrayLiteral
                // containing the result of qw()
                if elements.len() == 1 {
                    if let NodeKind::ArrayLiteral { .. } = &elements[0].kind {
                        // Recursively parse the inner array which contains the actual strings
                        return Self::parse_qw_array(&elements[0]);
                    }
                }
                // Normal case: ArrayLiteral with direct String elements
                elements
                    .iter()
                    .filter_map(|elem| {
                        // Handle String nodes from qw()
                        if let NodeKind::String { value, .. } = &elem.kind {
                            Some(value.clone())
                        } else {
                            None
                        }
                    })
                    .collect()
            }
            // Binary expression for concatenation
            NodeKind::Binary { op, left, right } if op == "." => {
                // Handle "foo" . "bar" form (rare, but possible)
                let mut result = Vec::new();
                if let NodeKind::String { value, .. } = &left.kind {
                    result.push(value.clone());
                }
                if let NodeKind::String { value, .. } = &right.kind {
                    result.push(value.clone());
                }
                result
            }
            // Handle parenthesized expressions like `('foo', 'bar')`
            // which might be wrapped in a Block or other node types
            _ => {
                // Try walking children if this node itself isn't a qw array
                let mut symbols = Vec::new();
                for child in node.children() {
                    symbols.extend(Self::parse_qw_array(child));
                }
                symbols
            }
        }
    }

    /// Parse `%EXPORT_TAGS` hash from an initializer node.
    ///
    /// The hash format is:
    /// ```perl
    /// %EXPORT_TAGS = (
    ///     tag1 => [qw(a b c)],
    ///     tag2 => [qw(d e f)],
    /// );
    /// ```
    ///
    /// Returns a map from tag name to list of exported symbols.
    fn parse_export_tags(node: &Node) -> HashMap<String, Vec<String>> {
        let mut tags: HashMap<String, Vec<String>> = HashMap::new();

        match &node.kind {
            // HashLiteral: `{ key => value, ... }`
            NodeKind::HashLiteral { pairs } => {
                for (key_node, value_node) in pairs {
                    if let Some(tag_name) = Self::extract_string_value(key_node) {
                        let symbols = Self::parse_qw_array(value_node);
                        if !symbols.is_empty() {
                            tags.insert(tag_name, symbols);
                        }
                    }
                }
            }
            // If it's not a HashLiteral, try to walk children to find hash pairs
            _ => {
                Self::walk_and_extract_export_tags(node, &mut tags);
            }
        }

        tags
    }

    /// Walk a node to extract export tags.
    fn walk_and_extract_export_tags(node: &Node, tags: &mut HashMap<String, Vec<String>>) {
        match &node.kind {
            NodeKind::HashLiteral { pairs } => {
                for (key_node, value_node) in pairs {
                    if let Some(tag_name) = Self::extract_string_value(key_node) {
                        let symbols = Self::parse_qw_array(value_node);
                        if !symbols.is_empty() {
                            tags.insert(tag_name, symbols);
                        }
                    }
                }
            }
            _ => {
                for child in node.children() {
                    Self::walk_and_extract_export_tags(child, tags);
                }
            }
        }
    }

    /// Extract a string value from a node.
    fn extract_string_value(node: &Node) -> Option<String> {
        match &node.kind {
            NodeKind::String { value, .. } => Some(value.clone()),
            NodeKind::Identifier { name } => Some(name.clone()),
            _ => None,
        }
    }
}

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

    fn parse_and_extract(code: &str) -> Option<ExportInfo> {
        let mut parser = Parser::new(code);
        let ast = parser.parse().ok()?;
        ExportSymbolExtractor::extract(&ast)
    }

    #[test]
    fn test_detect_use_exporter_import() {
        let code = r#"
package MyUtils;
use Exporter 'import';
our @EXPORT = qw(foo bar);
1;
"#;
        let info = parse_and_extract(code);
        assert!(info.is_some(), "Should detect Exporter, got {:?}", info);
        let info = info.unwrap();
        assert!(info.default_export.contains("foo"));
        assert!(info.default_export.contains("bar"));
    }

    #[test]
    fn test_detect_use_parent_exporter() {
        let code = r#"
package MyModule;
use parent 'Exporter';
our @EXPORT = qw(default_func);
1;
"#;
        let info = parse_and_extract(code);
        assert!(info.is_some(), "Should detect parent Exporter");
        let info = info.unwrap();
        assert!(info.default_export.contains("default_func"));
    }

    #[test]
    fn test_detect_use_parent_exporter_qw_form() {
        // `use parent qw(Exporter)` is common; the parser normalises qw to "qw(Exporter)".
        let code = r#"
package MyModule;
use parent qw(Exporter);
our @EXPORT = qw(qw_parent_func);
1;
"#;
        let info = parse_and_extract(code);
        assert!(info.is_some(), "Should detect `use parent qw(Exporter)` as Exporter-based");
        let info = info.unwrap();
        assert!(info.default_export.contains("qw_parent_func"));
    }

    #[test]
    fn test_detect_use_base_exporter() {
        // `use base` is the older equivalent of `use parent`.
        let code = r#"
package Legacy;
use base 'Exporter';
our @EXPORT = qw(legacy_func);
1;
"#;
        let info = parse_and_extract(code);
        assert!(info.is_some(), "Should detect `use base 'Exporter'` as Exporter-based");
        let info = info.unwrap();
        assert!(info.default_export.contains("legacy_func"));
    }

    #[test]
    fn test_detect_use_base_exporter_qw_form() {
        let code = r#"
package Legacy;
use base qw(Exporter SomeOtherBase);
our @EXPORT = qw(base_qw_func);
1;
"#;
        let info = parse_and_extract(code);
        assert!(info.is_some(), "Should detect `use base qw(Exporter ...)` as Exporter-based");
        let info = info.unwrap();
        assert!(info.default_export.contains("base_qw_func"));
    }

    #[test]
    fn test_detect_our_isa_exporter() {
        let code = r#"
package MyClass;
our @ISA = qw(Exporter);
our @EXPORT = qw(inherited_func);
1;
"#;
        let info = parse_and_extract(code);
        assert!(info.is_some(), "Should detect @ISA Exporter");
        let info = info.unwrap();
        assert!(info.default_export.contains("inherited_func"));
    }

    #[test]
    fn test_detect_bare_isa_assignment() {
        // `@ISA = qw(Exporter)` without `our` is common in older Perl code.
        let code = r#"
package OldStyle;
@ISA = qw(Exporter);
@EXPORT = qw(old_func);
1;
"#;
        let info = parse_and_extract(code);
        assert!(info.is_some(), "Should detect bare `@ISA = qw(Exporter)` form");
        let info = info.unwrap();
        assert!(
            info.default_export.contains("old_func"),
            "Should extract @EXPORT from bare assignment form"
        );
    }

    #[test]
    fn test_export_ok() {
        let code = r#"
package MyLib;
use Exporter 'import';
our @EXPORT_OK = qw(optional_a optional_b);
1;
"#;
        let info = parse_and_extract(code).unwrap();
        assert!(info.optional_export.contains("optional_a"));
        assert!(info.optional_export.contains("optional_b"));
    }

    #[test]
    fn test_export_tags() {
        let code = r#"
package Color;
use Exporter 'import';
our @EXPORT_OK = qw(red green blue rgb hex);
our %EXPORT_TAGS = (
    primary => [qw(red green blue)],
    formats => [qw(rgb hex)],
);
1;
"#;
        let info = parse_and_extract(code).unwrap();
        let primary = info.export_tags.get("primary");
        assert!(primary.is_some());
        let primary = primary.unwrap();
        assert!(primary.contains(&"red".to_string()));
        assert!(primary.contains(&"green".to_string()));
        assert!(primary.contains(&"blue".to_string()));

        let formats = info.export_tags.get("formats").unwrap();
        assert!(formats.contains(&"rgb".to_string()));
        assert!(formats.contains(&"hex".to_string()));
    }

    #[test]
    fn test_no_exporter_no_extraction() {
        // Without any Exporter inheritance pattern, the extractor must return None.
        // A bare @EXPORT without use Exporter / use parent / @ISA is not enough.
        let code = r#"
package MyModule;
our @EXPORT = qw(not_exported);
1;
"#;
        let info = parse_and_extract(code);
        assert!(
            info.is_none(),
            "Should return None when no Exporter inheritance is detected, got {:?}",
            info
        );
    }

    #[test]
    fn test_empty_export_arrays() {
        let code = r#"
package MyModule;
use Exporter 'import';
our @EXPORT = ();
our @EXPORT_OK = ();
our %EXPORT_TAGS = ();
1;
"#;
        let info = parse_and_extract(code).unwrap();
        assert!(info.default_export.is_empty());
        assert!(info.optional_export.is_empty());
        assert!(info.export_tags.is_empty());
    }

    #[test]
    fn test_multiple_arrays() {
        let code = r#"
package MyModule;
use Exporter 'import';
our @EXPORT = qw(default_a default_b);
our @EXPORT_OK = qw(optional_c optional_d);
our %EXPORT_TAGS = (
    tag1 => [qw(tag_a tag_b)],
);
1;
"#;
        let info = parse_and_extract(code).unwrap();
        assert_eq!(info.default_export.len(), 2);
        assert!(info.default_export.contains("default_a"));
        assert!(info.default_export.contains("default_b"));

        assert_eq!(info.optional_export.len(), 2);
        assert!(info.optional_export.contains("optional_c"));
        assert!(info.optional_export.contains("optional_d"));

        assert_eq!(info.export_tags.len(), 1);
    }

    #[test]
    fn test_detect_use_exporter_no_args() {
        // `use Exporter;` (no 'import' argument) is common in CPAN code and must
        // also trigger export extraction.
        let code = r#"
package MyUtils;
use Exporter;
our @EXPORT = qw(legacy_func);
1;
"#;
        let info = parse_and_extract(code);
        assert!(info.is_some(), "Should detect bare `use Exporter;` as Exporter-based module");
        let info = info.unwrap();
        assert!(
            info.default_export.contains("legacy_func"),
            "Should extract @EXPORT symbols from bare use Exporter; module"
        );
    }

    #[test]
    fn test_isa_with_multiple_parents_includes_exporter() {
        // When Exporter is one of multiple @ISA entries it must still be detected.
        let code = r#"
package Multi;
our @ISA = qw(SomeBase Exporter OtherBase);
our @EXPORT = qw(multi_func);
1;
"#;
        let info = parse_and_extract(code);
        assert!(info.is_some(), "Should detect Exporter even when mixed with other @ISA parents");
        let info = info.unwrap();
        assert!(info.default_export.contains("multi_func"));
    }
    #[test]
    fn test_regression_exporter_visibility_fixture() {
        let code = r#"
package MyLib;
use Exporter 'import';
our @EXPORT = qw(foo);
our @EXPORT_OK = qw(bar baz);
our %EXPORT_TAGS = (
    all => [qw(foo bar baz)],
);
1;
"#;
        let info = parse_and_extract(code).unwrap();

        assert_eq!(info.default_export.len(), 1);
        assert!(info.default_export.contains("foo"));

        assert_eq!(info.optional_export.len(), 2);
        assert!(info.optional_export.contains("bar"));
        assert!(info.optional_export.contains("baz"));

        let all = info.export_tags.get("all").unwrap();
        assert_eq!(all, &vec!["foo".to_string(), "bar".to_string(), "baz".to_string()]);
    }

    #[test]
    fn test_regression_merges_export_assignments_across_statements() {
        let code = r#"
package MyLib;
use Exporter 'import';
our @EXPORT = qw(foo);
our @EXPORT_OK = qw(bar);
our @EXPORT_OK = qw(bar baz);
our %EXPORT_TAGS = (core => [qw(foo bar)]);
our %EXPORT_TAGS = (all => [qw(foo bar baz)]);
1;
"#;
        let info = parse_and_extract(code).unwrap();

        assert!(info.default_export.contains("foo"));
        assert!(info.optional_export.contains("bar"));
        assert!(info.optional_export.contains("baz"));
        assert_eq!(
            info.export_tags.get("core").unwrap(),
            &vec!["foo".to_string(), "bar".to_string()]
        );
        assert_eq!(
            info.export_tags.get("all").unwrap(),
            &vec!["foo".to_string(), "bar".to_string(), "baz".to_string()]
        );
    }

    #[test]
    fn test_module_name_populated_from_package_declaration() -> Result<(), String> {
        let code = r#"
package My::Utils;
use Exporter 'import';
our @EXPORT = qw(helper);
1;
"#;
        let info = parse_and_extract(code).ok_or("Expected Some(ExportInfo)")?;
        assert_eq!(
            info.module_name.as_deref(),
            Some("My::Utils"),
            "module_name should be extracted from the package declaration"
        );
        Ok(())
    }

    #[test]
    fn test_module_name_propagated_to_export_set() -> Result<(), String> {
        let code = r#"
package Data::Formatter;
use parent 'Exporter';
our @EXPORT_OK = qw(format_csv);
1;
"#;
        let info = parse_and_extract(code).ok_or("Expected Some(ExportInfo)")?;
        let export_set = info.to_export_set();
        assert_eq!(
            export_set.module_name.as_deref(),
            Some("Data::Formatter"),
            "ExportSet.module_name should carry the package name"
        );
        Ok(())
    }

    #[test]
    fn test_anchor_id_populated_from_first_export_declaration() -> Result<(), String> {
        let code = r#"
package MyLib;
use Exporter 'import';
our @EXPORT = qw(foo);
1;
"#;
        let info = parse_and_extract(code).ok_or("Expected Some(ExportInfo)")?;
        assert!(
            info.anchor_id.is_some(),
            "anchor_id should be populated from the first export declaration"
        );
        Ok(())
    }

    #[test]
    fn test_anchor_id_propagated_to_export_set() -> Result<(), String> {
        let code = r#"
package MyLib;
use Exporter 'import';
our @EXPORT_OK = qw(bar baz);
1;
"#;
        let info = parse_and_extract(code).ok_or("Expected Some(ExportInfo)")?;
        let export_set = info.to_export_set();
        assert!(
            export_set.anchor_id.is_some(),
            "ExportSet.anchor_id should carry the first export declaration anchor"
        );
        Ok(())
    }

    #[test]
    fn test_anchor_id_none_when_no_export_arrays() -> Result<(), String> {
        // Module uses Exporter but declares no export arrays — anchor_id should be None.
        let code = r#"
package EmptyExporter;
use Exporter 'import';
1;
"#;
        let info = parse_and_extract(code).ok_or("Expected Some(ExportInfo)")?;
        assert!(
            info.anchor_id.is_none(),
            "anchor_id should be None when no export arrays are declared"
        );
        Ok(())
    }

    #[test]
    fn test_module_name_and_anchor_id_with_bare_assignment() -> Result<(), String> {
        let code = r#"
package OldStyle::Lib;
@ISA = qw(Exporter);
@EXPORT = qw(old_func);
1;
"#;
        let info = parse_and_extract(code).ok_or("Expected Some(ExportInfo)")?;
        assert_eq!(
            info.module_name.as_deref(),
            Some("OldStyle::Lib"),
            "module_name should work with bare assignment style"
        );
        assert!(
            info.anchor_id.is_some(),
            "anchor_id should be populated from bare @EXPORT assignment"
        );
        Ok(())
    }

    #[test]
    fn test_export_set_completeness_with_module_and_anchor() -> Result<(), String> {
        let code = r#"
package Full::Module;
use base 'Exporter';
our @EXPORT = qw(alpha beta);
our @EXPORT_OK = qw(gamma);
our %EXPORT_TAGS = (all => [qw(alpha beta gamma)]);
1;
"#;
        let info = parse_and_extract(code).ok_or("Expected Some(ExportInfo)")?;
        let export_set = info.to_export_set();

        // Verify module_name and anchor_id are present
        assert_eq!(export_set.module_name.as_deref(), Some("Full::Module"));
        assert!(export_set.anchor_id.is_some());

        // Verify export contents are still correct
        assert_eq!(export_set.default_exports, vec!["alpha", "beta"]);
        assert_eq!(export_set.optional_exports, vec!["gamma"]);
        assert_eq!(export_set.tags.len(), 1);
        assert_eq!(export_set.tags[0].name, "all");
        assert_eq!(export_set.tags[0].members, vec!["alpha", "beta", "gamma"]);
        Ok(())
    }
}