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
//! jsconfig.json parser for JavaScript path alias resolution
//!
//! Handles JSONC parsing, extends chain resolution, and path alias compilation
//! for JavaScript projects (Create React App, Next.js, Vite, etc.)

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use crate::project_resolver::{ResolutionError, ResolutionResult};

/// Compiled path rule for efficient pattern matching
#[derive(Debug)]
pub struct PathRule {
    /// Original pattern (e.g., "@components/*")
    pub pattern: String,
    /// Target paths (e.g., ["src/components/*"])
    pub targets: Vec<String>,
    /// Compiled regex for pattern matching
    regex: regex::Regex,
    /// Substitution template for replacements
    substitution_template: String,
}

/// Path alias resolver for JavaScript import resolution
#[derive(Debug)]
#[allow(non_snake_case)]
pub struct PathAliasResolver {
    /// Base URL for relative path resolution
    pub baseUrl: Option<String>,
    /// Compiled path rules in priority order
    pub rules: Vec<PathRule>,
}

/// JavaScript compiler options subset for path resolution
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[allow(non_snake_case)]
#[derive(Default)]
pub struct CompilerOptions {
    /// Base URL for module resolution
    #[serde(rename = "baseUrl")]
    pub baseUrl: Option<String>,

    /// Path mapping for module resolution
    #[serde(default)]
    pub paths: HashMap<String, Vec<String>>,
}

/// Minimal jsconfig.json representation for path resolution
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[allow(non_snake_case)]
#[derive(Default)]
pub struct JsConfig {
    /// Extends another configuration file
    pub extends: Option<String>,

    /// Compiler options
    #[serde(default)]
    pub compilerOptions: CompilerOptions,
}

/// JSONC parsing helper using serde_json5 for comment and trailing comma support
pub fn parse_jsonc_jsconfig(content: &str) -> ResolutionResult<JsConfig> {
    serde_json5::from_str(content).map_err(|e| {
        ResolutionError::invalid_cache(format!(
            "Failed to parse jsconfig.json: {e}\nSuggestion: Check JSON syntax, comments, and trailing commas"
        ))
    })
}

/// Read and parse a jsconfig.json file with JSONC support
pub fn read_jsconfig(path: &Path) -> ResolutionResult<JsConfig> {
    let content = std::fs::read_to_string(path)
        .map_err(|e| ResolutionError::cache_io(path.to_path_buf(), e))?;

    parse_jsonc_jsconfig(&content)
}

/// Resolve extends chain and merge configurations
///
/// Follows jsconfig.json extends resolution rules:
/// 1. Relative paths are resolved relative to the extending config
/// 2. Configurations are merged with child overriding parent
/// 3. Cycle detection prevents infinite recursion
pub fn resolve_extends_chain(
    base_path: &Path,
    visited: &mut std::collections::HashSet<PathBuf>,
) -> ResolutionResult<JsConfig> {
    let canonical_path = base_path
        .canonicalize()
        .map_err(|e| ResolutionError::cache_io(base_path.to_path_buf(), e))?;

    // Cycle detection
    if visited.contains(&canonical_path) {
        return Err(ResolutionError::invalid_cache(format!(
            "Circular extends chain detected: {}\nSuggestion: Remove circular references in jsconfig extends",
            canonical_path.display()
        )));
    }

    visited.insert(canonical_path.clone());

    let mut config = read_jsconfig(&canonical_path)?;

    // If this config extends another, resolve the parent first
    if let Some(extends_path) = &config.extends {
        let parent_path = if Path::new(extends_path).is_absolute() {
            PathBuf::from(extends_path)
        } else {
            canonical_path
                .parent()
                .ok_or_else(|| {
                    ResolutionError::invalid_cache(format!(
                        "Cannot resolve parent directory for: {}",
                        canonical_path.display()
                    ))
                })?
                .join(extends_path)
        };

        // Add .json extension if not present
        let parent_path = if parent_path.extension().is_none() {
            parent_path.with_extension("json")
        } else {
            parent_path
        };

        // Recursively resolve parent
        let parent_config = resolve_extends_chain(&parent_path, visited)?;

        // Merge parent into child (child overrides parent)
        config = merge_jsconfig(parent_config, config);
    }

    visited.remove(&canonical_path);
    Ok(config)
}

/// Merge two jsconfig objects, with child overriding parent
fn merge_jsconfig(parent: JsConfig, child: JsConfig) -> JsConfig {
    JsConfig {
        // Child extends takes precedence (but we don't chain extends)
        extends: child.extends,
        compilerOptions: CompilerOptions {
            // Child baseUrl overrides parent
            baseUrl: child
                .compilerOptions
                .baseUrl
                .or(parent.compilerOptions.baseUrl),
            // Merge paths with child taking precedence
            paths: {
                let mut merged = parent.compilerOptions.paths;
                merged.extend(child.compilerOptions.paths);
                merged
            },
        },
    }
}

impl PathRule {
    /// Create a new path rule from pattern and targets
    pub fn new(pattern: String, targets: Vec<String>) -> ResolutionResult<Self> {
        // Convert glob pattern to regex
        // "@components/*" becomes "^@components/(.*)$"
        let regex_pattern = pattern.replace("*", "(.*)");
        let regex_pattern = format!(
            "^{}$",
            regex::escape(&regex_pattern).replace("\\(\\.\\*\\)", "(.*)")
        );

        let regex = regex::Regex::new(&regex_pattern).map_err(|e| {
            ResolutionError::invalid_cache(format!(
                "Invalid path pattern '{pattern}': {e}\nSuggestion: Check jsconfig.json path patterns for valid syntax"
            ))
        })?;

        // Create substitution template
        // "src/components/*" becomes "src/components/$1"
        let substitution_template = targets
            .first()
            .ok_or_else(|| {
                ResolutionError::invalid_cache(format!(
                    "Path pattern '{pattern}' has no targets\nSuggestion: Add at least one target path"
                ))
            })?
            .replace("*", "$1");

        Ok(Self {
            pattern,
            targets,
            regex,
            substitution_template,
        })
    }

    /// Try to match an import specifier against this rule
    pub fn try_resolve(&self, specifier: &str) -> Option<String> {
        if let Some(captures) = self.regex.captures(specifier) {
            let mut result = self.substitution_template.clone();
            if let Some(captured) = captures.get(1) {
                result = result.replace("$1", captured.as_str());
            }
            Some(result)
        } else {
            None
        }
    }
}

impl PathAliasResolver {
    /// Create a resolver from jsconfig compiler options
    pub fn from_jsconfig(config: &JsConfig) -> ResolutionResult<Self> {
        let mut rules = Vec::new();

        // Compile path patterns in SPECIFICITY order (most specific first)
        // More specific patterns (longer, fewer wildcards) should match before catch-all patterns
        let mut paths: Vec<_> = config.compilerOptions.paths.iter().collect();
        paths.sort_by_key(|(pattern, _)| {
            let wildcard_count = pattern.matches('*').count();
            (-(pattern.len() as isize), wildcard_count)
        });

        for (pattern, targets) in paths {
            let rule = PathRule::new(pattern.clone(), targets.clone())?;
            rules.push(rule);
        }

        Ok(Self {
            baseUrl: config.compilerOptions.baseUrl.clone(),
            rules,
        })
    }

    /// Resolve an import specifier to possible file paths
    pub fn resolve_import(&self, specifier: &str) -> Vec<String> {
        let mut candidates = Vec::new();

        // Try each rule in order
        for rule in &self.rules {
            if let Some(resolved) = rule.try_resolve(specifier) {
                // Apply baseUrl if present
                let final_path = if let Some(ref base) = self.baseUrl {
                    if base == "." {
                        resolved
                    } else {
                        format!("{}/{}", base.trim_end_matches('/'), resolved)
                    }
                } else {
                    resolved
                };
                candidates.push(final_path);
            }
        }

        candidates
    }

    /// Expand a candidate path with JavaScript file extensions
    pub fn expand_extensions(&self, path: &str) -> Vec<String> {
        let mut expanded = Vec::new();

        // Add the path as-is first
        expanded.push(path.to_string());

        // Add common JavaScript extensions
        for ext in &[".js", ".jsx", ".mjs", ".cjs"] {
            expanded.push(format!("{path}{ext}"));
        }

        // Add index file variants
        for ext in &[".js", ".jsx"] {
            expanded.push(format!("{path}/index{ext}"));
        }

        expanded
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn parse_jsconfig_with_comments() {
        let content = r#"{
            // Base configuration
            "compilerOptions": {
                "baseUrl": "./src", // Source directory
                "paths": {
                    /* Path mappings */
                    "@utils/*": ["utils/*"], // Utility modules
                }
            }
        }"#;

        let config = parse_jsonc_jsconfig(content).expect("Should parse JSONC with comments");

        assert_eq!(config.compilerOptions.baseUrl, Some("./src".to_string()));
        assert_eq!(config.compilerOptions.paths.len(), 1);
    }

    #[test]
    fn parse_minimal_jsconfig() {
        let content = r#"{}"#;

        let config = parse_jsonc_jsconfig(content).expect("Should parse empty config");

        assert!(config.extends.is_none());
        assert!(config.compilerOptions.baseUrl.is_none());
        assert!(config.compilerOptions.paths.is_empty());
    }

    #[test]
    fn parse_jsconfig_with_extends() {
        let content = r#"{
            "extends": "./base.json",
            "compilerOptions": {
                "baseUrl": "./src"
            }
        }"#;

        let config = parse_jsonc_jsconfig(content).expect("Should parse config with extends");

        assert_eq!(config.extends, Some("./base.json".to_string()));
        assert_eq!(config.compilerOptions.baseUrl, Some("./src".to_string()));
    }

    #[test]
    fn invalid_json_returns_error() {
        let content = r#"{ invalid json }"#;

        let result = parse_jsonc_jsconfig(content);

        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("Failed to parse jsconfig.json"));
        assert!(error_msg.contains("Suggestion:"));
    }

    #[test]
    fn merge_jsconfig_child_overrides_parent() {
        let parent = JsConfig {
            extends: Some("parent.json".to_string()),
            compilerOptions: CompilerOptions {
                baseUrl: Some("./parent".to_string()),
                paths: HashMap::from([
                    ("@parent/*".to_string(), vec!["parent/*".to_string()]),
                    ("@common/*".to_string(), vec!["parent/common/*".to_string()]),
                ]),
            },
        };

        let child = JsConfig {
            extends: Some("child.json".to_string()),
            compilerOptions: CompilerOptions {
                baseUrl: Some("./child".to_string()),
                paths: HashMap::from([
                    ("@child/*".to_string(), vec!["child/*".to_string()]),
                    ("@common/*".to_string(), vec!["child/common/*".to_string()]),
                ]),
            },
        };

        let merged = merge_jsconfig(parent, child);

        // Child values should take precedence
        assert_eq!(merged.extends, Some("child.json".to_string()));
        assert_eq!(merged.compilerOptions.baseUrl, Some("./child".to_string()));

        // Child should override parent for @common/*
        assert_eq!(
            merged.compilerOptions.paths.get("@common/*"),
            Some(&vec!["child/common/*".to_string()])
        );

        // Parent-only paths should be preserved
        assert!(merged.compilerOptions.paths.contains_key("@parent/*"));

        // Child-only paths should be present
        assert!(merged.compilerOptions.paths.contains_key("@child/*"));
    }

    #[test]
    fn path_rule_resolves_wildcards() {
        let rule = PathRule::new(
            "@components/*".to_string(),
            vec!["src/components/*".to_string()],
        )
        .expect("Should create rule");

        let result = rule.try_resolve("@components/Button");
        assert_eq!(result, Some("src/components/Button".to_string()));

        let result = rule.try_resolve("@components/forms/Input");
        assert_eq!(result, Some("src/components/forms/Input".to_string()));

        let result = rule.try_resolve("@utils/format");
        assert!(result.is_none());
    }

    #[test]
    fn path_alias_resolver_with_base_url() {
        let config = JsConfig {
            extends: None,
            compilerOptions: CompilerOptions {
                baseUrl: Some("./src".to_string()),
                paths: HashMap::from([("@/*".to_string(), vec!["*".to_string()])]),
            },
        };

        let resolver = PathAliasResolver::from_jsconfig(&config).expect("Should create resolver");
        let resolved = resolver.resolve_import("@/components/Button");

        assert_eq!(resolved, vec!["./src/components/Button".to_string()]);
    }

    #[test]
    fn expand_javascript_extensions() {
        let resolver = PathAliasResolver {
            baseUrl: Some("./src".to_string()),
            rules: vec![],
        };

        let base_path = "components/Button";
        let expanded = resolver.expand_extensions(base_path);

        assert!(expanded.contains(&"components/Button".to_string()));
        assert!(expanded.contains(&"components/Button.js".to_string()));
        assert!(expanded.contains(&"components/Button.jsx".to_string()));
        assert!(expanded.contains(&"components/Button.mjs".to_string()));
        assert!(expanded.contains(&"components/Button/index.js".to_string()));
        assert!(expanded.contains(&"components/Button/index.jsx".to_string()));
    }

    #[test]
    fn detect_circular_extends() {
        let temp_dir = TempDir::new().unwrap();

        // Create circular reference: a -> b -> a
        let a_content = r#"{ "extends": "./b.json" }"#;
        let b_content = r#"{ "extends": "./a.json" }"#;

        let a_path = temp_dir.path().join("a.json");
        let b_path = temp_dir.path().join("b.json");

        fs::write(&a_path, a_content).unwrap();
        fs::write(&b_path, b_content).unwrap();

        let mut visited = std::collections::HashSet::new();
        let result = resolve_extends_chain(&a_path, &mut visited);

        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("Circular extends chain detected"));
        assert!(error_msg.contains("Suggestion:"));
    }
}