codanna 0.9.19

Code Intelligence for Large Language Models
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
//! JavaScript-specific resolution and inheritance implementation
//!
//! This module implements JavaScript's resolution rules including:
//! - Hoisting of functions and var declarations
//! - Block scoping for let/const
//! - Namespace resolution (for ES modules)
//! - Class inheritance (extends only, no interfaces)
//!
//! Note: JavaScript is similar to TypeScript but lacks:
//! - Type-only imports
//! - Interfaces
//! - Protected visibility
//! - Abstract classes

use crate::parsing::resolution::ImportBinding;
use crate::parsing::{InheritanceResolver, ResolutionScope, ScopeLevel, ScopeType};
use crate::{FileId, SymbolId};
use std::collections::HashMap;

/// JavaScript-specific resolution context handling hoisting, namespaces
///
/// JavaScript has similar resolution to TypeScript but without type spaces:
/// 1. Hoisting (functions and var declarations)
/// 2. Block scoping (let/const)
/// 3. Namespace scoping (ES modules)
pub struct JavaScriptResolutionContext {
    #[allow(dead_code)]
    file_id: FileId,

    /// Local block-scoped bindings (let/const)
    local_scope: HashMap<String, SymbolId>,

    /// Hoisted declarations (functions and var)
    hoisted_scope: HashMap<String, SymbolId>,

    /// Module-level exports and declarations
    module_symbols: HashMap<String, SymbolId>,

    /// Imported symbols from other modules
    imported_symbols: HashMap<String, SymbolId>,

    /// Global/ambient declarations
    global_symbols: HashMap<String, SymbolId>,

    /// Track nested scopes (blocks, functions, etc.)
    scope_stack: Vec<ScopeType>,

    /// Import tracking (path -> alias)
    imports: Vec<(String, Option<String>)>,

    /// Precomputed qualified name resolution for namespace imports
    /// e.g., alias "Utils" → { "Utils.helper" => SymbolId }
    qualified_names: HashMap<String, SymbolId>,
    /// Namespace alias to target module path (normalized, dots)
    namespace_aliases: HashMap<String, String>,

    /// Binding info for imports keyed by visible name
    import_bindings: HashMap<String, ImportBinding>,
}

impl JavaScriptResolutionContext {
    pub fn new(file_id: FileId) -> Self {
        Self {
            file_id,
            local_scope: HashMap::new(),
            hoisted_scope: HashMap::new(),
            module_symbols: HashMap::new(),
            imported_symbols: HashMap::new(),
            global_symbols: HashMap::new(),
            scope_stack: Vec::new(),
            imports: Vec::new(),
            qualified_names: HashMap::new(),
            namespace_aliases: HashMap::new(),
            import_bindings: HashMap::new(),
        }
    }

    /// Add an import (import statement)
    pub fn add_import(&mut self, path: String, alias: Option<String>) {
        self.imports.push((path, alias));
    }

    /// Record a namespace alias mapping (e.g., import * as React from 'react')
    pub fn add_namespace_alias(&mut self, alias: String, target_module: String) {
        self.namespace_aliases.insert(alias, target_module);
    }

    /// Add a qualified name binding for fast resolution (e.g., "Utils.helper" -> id)
    pub fn add_qualified_name(&mut self, qualified: String, symbol_id: SymbolId) {
        self.qualified_names.insert(qualified, symbol_id);
    }

    /// Add an imported symbol to the context
    ///
    /// JavaScript doesn't have type-only imports, so all imports are value imports
    pub fn add_import_symbol(&mut self, name: String, symbol_id: SymbolId, _is_type_only: bool) {
        // All imports are regular imports in JavaScript (no type-only imports)
        self.imported_symbols.insert(name, symbol_id);
    }

    /// Add a symbol with proper scope context
    ///
    /// This method uses the symbol's scope_context to determine proper placement.
    /// Functions are hoisted, let/const are block-scoped, var is function-scoped.
    pub fn add_symbol_with_context(
        &mut self,
        name: String,
        symbol_id: SymbolId,
        scope_context: Option<&crate::symbol::ScopeContext>,
    ) {
        use crate::symbol::ScopeContext;

        match scope_context {
            Some(ScopeContext::Local { hoisted: true, .. }) => {
                // Hoisted declarations (functions, var)
                self.hoisted_scope.insert(name, symbol_id);
            }
            Some(ScopeContext::Local { hoisted: false, .. }) => {
                // Block-scoped declarations (let, const, arrow functions)
                self.local_scope.insert(name, symbol_id);
            }
            Some(ScopeContext::ClassMember { .. }) => {
                // Class members go to local scope within the class
                self.local_scope.insert(name, symbol_id);
            }
            Some(ScopeContext::Parameter) => {
                // Function parameters are local
                self.local_scope.insert(name, symbol_id);
            }
            Some(ScopeContext::Module) => {
                // Module-level declarations
                self.module_symbols.insert(name, symbol_id);
            }
            Some(ScopeContext::Package) => {
                // Imported symbols
                self.imported_symbols.insert(name, symbol_id);
            }
            Some(ScopeContext::Global) => {
                // Global/ambient declarations
                self.global_symbols.insert(name, symbol_id);
            }
            None => {
                // Default to local scope if no context
                self.local_scope.insert(name, symbol_id);
            }
        }
    }
}

impl ResolutionScope for JavaScriptResolutionContext {
    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }

    fn add_symbol(&mut self, name: String, symbol_id: SymbolId, scope_level: ScopeLevel) {
        match scope_level {
            ScopeLevel::Local => {
                // For proper hoisting behavior, use add_symbol_with_context() instead
                self.local_scope.insert(name, symbol_id);
            }
            ScopeLevel::Module => {
                self.module_symbols.insert(name, symbol_id);
            }
            ScopeLevel::Package => {
                self.imported_symbols.insert(name, symbol_id);
            }
            ScopeLevel::Global => {
                self.global_symbols.insert(name, symbol_id);
            }
        }
    }

    fn resolve(&self, name: &str) -> Option<SymbolId> {
        // JavaScript resolution order:
        // 1. Local block scope (let/const)
        // 2. Hoisted scope (functions, var)
        // 3. Imported symbols
        // 4. Module symbols
        // 5. Global/ambient

        tracing::debug!("[javascript] resolve looking up name='{name}'");

        // 1. Check local block scope
        if let Some(&id) = self.local_scope.get(name) {
            tracing::debug!("[javascript] found in local_scope: {id:?}");
            return Some(id);
        }

        // 2. Check hoisted scope
        if let Some(&id) = self.hoisted_scope.get(name) {
            tracing::debug!("[javascript] found in hoisted_scope: {id:?}");
            return Some(id);
        }

        // 3. Check imported symbols
        if let Some(&id) = self.imported_symbols.get(name) {
            tracing::debug!("[javascript] found in imported_symbols: {id:?}");
            return Some(id);
        }

        // 4. Check module-level symbols
        if let Some(&id) = self.module_symbols.get(name) {
            return Some(id);
        }

        // 5. Check global/ambient
        if let Some(&id) = self.global_symbols.get(name) {
            return Some(id);
        }

        // 6. Check if it's a qualified name (contains .)
        if name.contains('.') {
            // First try to resolve the full qualified path directly
            if let Some(&id) = self.imported_symbols.get(name) {
                return Some(id);
            }
            if let Some(&id) = self.module_symbols.get(name) {
                return Some(id);
            }
            if let Some(&id) = self.global_symbols.get(name) {
                return Some(id);
            }

            // If full path not found, try to resolve as a 2-part path
            let parts: Vec<&str> = name.split('.').collect();
            if parts.len() == 2 {
                let class_or_module = parts[0];
                let method_or_prop = parts[1];
                // Namespace import alias (e.g., React.useEffect)
                if let Some(_module) = self.namespace_aliases.get(class_or_module) {
                    // For namespace imports, attempt to resolve the member by name
                    if let Some(&id) = self
                        .local_scope
                        .get(method_or_prop)
                        .or_else(|| self.hoisted_scope.get(method_or_prop))
                        .or_else(|| self.imported_symbols.get(method_or_prop))
                        .or_else(|| self.module_symbols.get(method_or_prop))
                        .or_else(|| self.global_symbols.get(method_or_prop))
                    {
                        return Some(id);
                    }
                }

                // Check if type exists in our codebase (class or module symbol)
                if self.resolve(class_or_module).is_some() {
                    // Type exists, resolve the method/property by name
                    return self.resolve(method_or_prop);
                }
                // External library or unresolved alias - return None
                return None;
            }
        }

        tracing::debug!("[javascript] resolve not found: '{name}'");
        None
    }

    fn clear_local_scope(&mut self) {
        self.local_scope.clear();
    }

    fn enter_scope(&mut self, scope_type: ScopeType) {
        self.scope_stack.push(scope_type);
    }

    fn exit_scope(&mut self) {
        self.scope_stack.pop();
        // Clear locals when exiting function scope
        if matches!(
            self.scope_stack.last(),
            None | Some(ScopeType::Module | ScopeType::Global)
        ) {
            self.clear_local_scope();
            self.hoisted_scope.clear();
        }
    }

    fn symbols_in_scope(&self) -> Vec<(String, SymbolId, ScopeLevel)> {
        let mut symbols = Vec::new();

        for (name, &id) in &self.local_scope {
            symbols.push((name.clone(), id, ScopeLevel::Local));
        }
        for (name, &id) in &self.hoisted_scope {
            symbols.push((name.clone(), id, ScopeLevel::Local));
        }
        for (name, &id) in &self.imported_symbols {
            symbols.push((name.clone(), id, ScopeLevel::Package));
        }
        for (name, &id) in &self.module_symbols {
            symbols.push((name.clone(), id, ScopeLevel::Module));
        }
        for (name, &id) in &self.global_symbols {
            symbols.push((name.clone(), id, ScopeLevel::Global));
        }

        symbols
    }

    fn resolve_relationship(
        &self,
        _from_name: &str,
        to_name: &str,
        kind: crate::RelationKind,
        _from_file: FileId,
    ) -> Option<SymbolId> {
        use crate::RelationKind;

        match kind {
            RelationKind::Extends => {
                // JavaScript: classes can extend other classes
                self.resolve(to_name)
            }
            RelationKind::Calls => {
                // JavaScript: handle Class.method patterns and module.function
                if to_name.contains('.') {
                    // Try to resolve the full qualified name first
                    if let Some(id) = self.resolve(to_name) {
                        return Some(id);
                    }
                    // If not found, try just the method/function name
                    if let Some(last_part) = to_name.rsplit('.').next() {
                        return self.resolve(last_part);
                    }
                }
                // Simple function or method call
                self.resolve(to_name)
            }
            RelationKind::Uses => {
                // JavaScript: imports, variable usage, etc.
                self.resolve(to_name)
            }
            _ => {
                // For other relationship types, use standard resolution
                self.resolve(to_name)
            }
        }
    }

    fn is_compatible_relationship(
        &self,
        from_kind: crate::SymbolKind,
        to_kind: crate::SymbolKind,
        rel_kind: crate::RelationKind,
    ) -> bool {
        use crate::RelationKind::*;
        use crate::SymbolKind::*;

        match rel_kind {
            Calls => {
                // JavaScript: Functions/Methods/Constants/Variables can call
                let caller_can_call = matches!(
                    from_kind,
                    Function | Method | Macro | Module | Constant | Variable
                );
                // JavaScript: Constants and Variables can be callable (React patterns)
                let callee_can_be_called = matches!(
                    to_kind,
                    Function | Method | Macro | Class | Constant | Variable
                );
                caller_can_call && callee_can_be_called
            }
            CalledBy => {
                let caller_can_call = matches!(
                    to_kind,
                    Function | Method | Macro | Module | Constant | Variable
                );
                let callee_can_be_called = matches!(
                    from_kind,
                    Function | Method | Macro | Class | Constant | Variable
                );
                callee_can_be_called && caller_can_call
            }
            Extends => {
                // JavaScript: only classes can extend classes (no interfaces)
                matches!(from_kind, Class) && matches!(to_kind, Class)
            }
            ExtendedBy => matches!(from_kind, Class) && matches!(to_kind, Class),
            Uses => {
                let can_use = matches!(
                    from_kind,
                    Function | Method | Struct | Class | Trait | Interface | Module | Enum
                );
                let can_be_used = matches!(
                    to_kind,
                    Struct
                        | Enum
                        | Class
                        | Trait
                        | Interface
                        | TypeAlias
                        | Constant
                        | Variable
                        | Function
                        | Method
                );
                can_use && can_be_used
            }
            UsedBy => {
                let can_be_used = matches!(
                    from_kind,
                    Struct
                        | Enum
                        | Class
                        | Trait
                        | Interface
                        | TypeAlias
                        | Constant
                        | Variable
                        | Function
                        | Method
                );
                let can_use = matches!(
                    to_kind,
                    Function | Method | Struct | Class | Trait | Interface | Module | Enum
                );
                can_be_used && can_use
            }
            Defines => {
                let container = matches!(
                    from_kind,
                    Trait | Interface | Module | Struct | Enum | Class
                );
                let member = matches!(to_kind, Method | Function | Constant | Field | Variable);
                container && member
            }
            DefinedIn => {
                let member = matches!(from_kind, Method | Function | Constant | Field | Variable);
                let container =
                    matches!(to_kind, Trait | Interface | Module | Struct | Enum | Class);
                member && container
            }
            References => true,
            ReferencedBy => true,
            // JavaScript doesn't support Implements/ImplementedBy (no interfaces)
            Implements | ImplementedBy => false,
        }
    }

    fn populate_imports(&mut self, imports: &[crate::parsing::Import]) {
        for import in imports {
            self.add_import(import.path.clone(), import.alias.clone());
        }
    }

    fn register_import_binding(&mut self, binding: ImportBinding) {
        self.import_bindings
            .insert(binding.exposed_name.clone(), binding);
    }

    fn import_binding(&self, name: &str) -> Option<ImportBinding> {
        self.import_bindings.get(name).cloned()
    }
}

/// JavaScript inheritance resolution system
///
/// This handles:
/// - Class single inheritance (extends only)
/// - No interfaces (JavaScript doesn't have them)
pub struct JavaScriptInheritanceResolver {
    /// Maps class names to their parent class
    class_extends: HashMap<String, String>,

    /// Tracks methods on classes
    type_methods: HashMap<String, Vec<String>>,
}

impl Default for JavaScriptInheritanceResolver {
    fn default() -> Self {
        Self::new()
    }
}

impl JavaScriptInheritanceResolver {
    pub fn new() -> Self {
        Self {
            class_extends: HashMap::new(),
            type_methods: HashMap::new(),
        }
    }
}

impl InheritanceResolver for JavaScriptInheritanceResolver {
    fn add_inheritance(&mut self, child: String, parent: String, kind: &str) {
        match kind {
            "extends" => {
                // Class extends class (single inheritance)
                self.class_extends.insert(child, parent);
            }
            _ => {
                // JavaScript only supports extends, ignore other relationships
            }
        }
    }

    fn resolve_method(&self, type_name: &str, method_name: &str) -> Option<String> {
        // Check if the type has this method
        if let Some(methods) = self.type_methods.get(type_name) {
            if methods.iter().any(|m| m == method_name) {
                return Some(type_name.to_string());
            }
        }

        // Check parent class
        if let Some(parent) = self.class_extends.get(type_name) {
            if let Some(resolved) = self.resolve_method(parent, method_name) {
                return Some(resolved);
            }
        }

        None
    }

    fn get_inheritance_chain(&self, type_name: &str) -> Vec<String> {
        let mut chain = vec![type_name.to_string()];
        let mut visited = std::collections::HashSet::new();
        visited.insert(type_name.to_string());

        // Add parent class
        if let Some(parent) = self.class_extends.get(type_name) {
            if visited.insert(parent.clone()) {
                chain.push(parent.clone());
                // Recursively get parent's chain
                for ancestor in self.get_inheritance_chain(parent) {
                    if visited.insert(ancestor.clone()) {
                        chain.push(ancestor);
                    }
                }
            }
        }

        chain
    }

    fn is_subtype(&self, child: &str, parent: &str) -> bool {
        // Check direct class extension
        if let Some(direct_parent) = self.class_extends.get(child) {
            if direct_parent == parent {
                return true;
            }
            // Recursive check
            if self.is_subtype(direct_parent, parent) {
                return true;
            }
        }

        false
    }

    fn add_type_methods(&mut self, type_name: String, methods: Vec<String>) {
        self.type_methods
            .entry(type_name)
            .or_default()
            .extend(methods);
    }

    fn get_all_methods(&self, type_name: &str) -> Vec<String> {
        let mut all_methods = Vec::new();
        let mut visited = std::collections::HashSet::new();

        fn collect_methods(
            resolver: &JavaScriptInheritanceResolver,
            type_name: &str,
            all_methods: &mut Vec<String>,
            visited: &mut std::collections::HashSet<String>,
        ) {
            if !visited.insert(type_name.to_string()) {
                return;
            }

            // Add this type's methods
            if let Some(methods) = resolver.type_methods.get(type_name) {
                for method in methods {
                    if !all_methods.contains(method) {
                        all_methods.push(method.clone());
                    }
                }
            }

            // Check parent class
            if let Some(parent) = resolver.class_extends.get(type_name) {
                collect_methods(resolver, parent, all_methods, visited);
            }
        }

        collect_methods(self, type_name, &mut all_methods, &mut visited);
        all_methods
    }
}

/// JavaScript project resolution enhancer
///
/// Applies jsconfig.json path mappings to transform import paths.
/// Mirrors TypeScript's TypeScriptProjectEnhancer architecture.
pub struct JavaScriptProjectEnhancer {
    /// Compiled path alias resolver (built from the resolution rules)
    resolver: Option<crate::parsing::javascript::jsconfig::PathAliasResolver>,
}

impl JavaScriptProjectEnhancer {
    /// Create a new enhancer from resolution rules
    pub fn new(rules: crate::project_resolver::persist::ResolutionRules) -> Self {
        // Build the PathAliasResolver from rules
        let resolver = if !rules.paths.is_empty() || rules.base_url.is_some() {
            // Create a minimal JsConfig to use from_jsconfig
            let config = crate::parsing::javascript::jsconfig::JsConfig {
                extends: None,
                compilerOptions: crate::parsing::javascript::jsconfig::CompilerOptions {
                    baseUrl: rules.base_url.clone(),
                    paths: rules.paths.clone(),
                },
            };

            // Use from_jsconfig to create the resolver
            crate::parsing::javascript::jsconfig::PathAliasResolver::from_jsconfig(&config).ok()
        } else {
            None
        };

        Self { resolver }
    }
}

impl crate::parsing::resolution::ProjectResolutionEnhancer for JavaScriptProjectEnhancer {
    fn enhance_import_path(&self, import_path: &str, _from_file: FileId) -> Option<String> {
        // Skip relative imports - they don't need enhancement
        if import_path.starts_with("./") || import_path.starts_with("../") {
            return None;
        }

        // Use the resolver to transform the path
        if let Some(ref resolver) = self.resolver {
            // Get candidates and return the first one
            let candidates = resolver.resolve_import(import_path);
            candidates.into_iter().next()
        } else {
            None
        }
    }

    fn get_import_candidates(&self, import_path: &str, _from_file: FileId) -> Vec<String> {
        // Skip relative imports
        if import_path.starts_with("./") || import_path.starts_with("../") {
            return vec![import_path.to_string()];
        }

        // Use the resolver to get all candidates
        if let Some(ref resolver) = self.resolver {
            let candidates = resolver.resolve_import(import_path);
            if !candidates.is_empty() {
                candidates
            } else {
                vec![import_path.to_string()]
            }
        } else {
            vec![import_path.to_string()]
        }
    }
}

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

    #[test]
    fn test_javascript_function_can_call_constant() {
        // React pattern: const Button = () => {}
        let context = JavaScriptResolutionContext::new(FileId::new(1).unwrap());
        assert!(context.is_compatible_relationship(
            crate::SymbolKind::Function,
            crate::SymbolKind::Constant,
            crate::RelationKind::Calls
        ));
    }

    #[test]
    fn test_javascript_hoisting() {
        let mut context = JavaScriptResolutionContext::new(FileId::new(1).unwrap());

        // Add hoisted function
        context.add_symbol(
            "function myFunc".to_string(),
            SymbolId::new(1).unwrap(),
            ScopeLevel::Local,
        );

        assert_eq!(
            context.resolve("function myFunc"),
            Some(SymbolId::new(1).unwrap())
        );
    }

    #[test]
    fn test_javascript_no_implements() {
        // JavaScript doesn't support implements relationship
        let context = JavaScriptResolutionContext::new(FileId::new(1).unwrap());
        assert!(!context.is_compatible_relationship(
            crate::SymbolKind::Class,
            crate::SymbolKind::Interface,
            crate::RelationKind::Implements
        ));
    }

    #[test]
    fn test_class_inheritance() {
        let mut resolver = JavaScriptInheritanceResolver::new();

        // Class extends class
        resolver.add_inheritance(
            "ChildClass".to_string(),
            "ParentClass".to_string(),
            "extends",
        );

        // Check subtype relationship
        assert!(resolver.is_subtype("ChildClass", "ParentClass"));

        // Check inheritance chain
        let chain = resolver.get_inheritance_chain("ChildClass");
        assert!(chain.contains(&"ParentClass".to_string()));
    }
}