features-cli 0.13.0

A CLI tool for discovering the features in a folder
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
//! Module for detecting imports/dependencies in source code files
//!
//! This module scans source files for import statements and resolves them
//! to their actual file paths to detect cross-feature dependencies.

use anyhow::Result;
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

use crate::skip_dirs::is_non_source_dir;

#[derive(Debug, Clone)]
pub struct ImportStatement {
    pub file_path: String,
    pub line_number: usize,
    pub line_content: String,
    pub imported_path: String,
}

/// Represents language-specific import patterns
#[derive(Debug, Clone)]
enum ImportPattern {
    /// Rust: use statements
    Rust,
    /// JavaScript/TypeScript: import/require/export from
    JavaScript,
    /// Python: import/from...import
    Python,
    /// Go: import statements
    Go,
    /// Java/C#/Kotlin: import/using statements
    JavaLike,
    /// C/C++: #include statements
    CStyle,
    /// Ruby: require/require_relative
    Ruby,
    /// PHP: use/require/include
    Php,
    /// Shell: source/.
    Shell,
    /// CSS/SCSS/Less: @import
    Css,
}

/// Get import pattern for a file extension
fn get_import_pattern(extension: &str) -> Option<ImportPattern> {
    match extension {
        "rs" => Some(ImportPattern::Rust),
        "js" | "jsx" | "ts" | "tsx" | "mjs" | "cjs" => Some(ImportPattern::JavaScript),
        "py" => Some(ImportPattern::Python),
        "go" => Some(ImportPattern::Go),
        "java" | "kt" | "scala" => Some(ImportPattern::JavaLike),
        "cs" => Some(ImportPattern::JavaLike),
        "c" | "cpp" | "cc" | "cxx" | "h" | "hpp" => Some(ImportPattern::CStyle),
        "rb" => Some(ImportPattern::Ruby),
        "php" => Some(ImportPattern::Php),
        "sh" | "bash" => Some(ImportPattern::Shell),
        "css" | "scss" | "less" => Some(ImportPattern::Css),
        _ => None,
    }
}

/// Extract import path from a Rust use statement
fn extract_rust_import(line: &str) -> Option<String> {
    let trimmed = line.trim();

    // Remove "use " prefix and trailing semicolon
    let import_part = trimmed.strip_prefix("use ")?.trim_end_matches(';').trim();

    // Handle 'use crate::' or 'use super::' or 'use self::'
    if import_part.starts_with("crate::")
        || import_part.starts_with("super::")
        || import_part.starts_with("self::")
    {
        // Extract the module path (remove use blocks like {Type1, Type2})
        let path = if let Some(brace_pos) = import_part.find('{') {
            import_part[..brace_pos].trim()
        } else if let Some(as_pos) = import_part.find(" as ") {
            import_part[..as_pos].trim()
        } else {
            import_part
        };

        return Some(path.to_string());
    }

    None
}

/// Extract import path from JavaScript/TypeScript import/require
fn extract_javascript_import(line: &str) -> Option<String> {
    let trimmed = line.trim();

    // import ... from "path" or import ... from 'path'
    if trimmed.starts_with("import ") {
        if let Some(from_pos) = trimmed.find(" from ") {
            let after_from = &trimmed[from_pos + 6..].trim();
            return extract_quoted_string(after_from);
        }
        // import "path" or import 'path'
        if let Some(quote_pos) = trimmed.find(['"', '\'']) {
            return extract_quoted_string(&trimmed[quote_pos..]);
        }
    }

    // export ... from "path"
    if trimmed.starts_with("export ")
        && trimmed.contains(" from ")
        && let Some(from_pos) = trimmed.find(" from ")
    {
        let after_from = &trimmed[from_pos + 6..].trim();
        return extract_quoted_string(after_from);
    }

    // require("path") or require('path')
    if trimmed.contains("require(")
        && let Some(paren_pos) = trimmed.find("require(")
    {
        let after_paren = &trimmed[paren_pos + 8..];
        return extract_quoted_string(after_paren);
    }

    None
}

/// Extract import path from Python import statements
fn extract_python_import(line: &str) -> Option<String> {
    let trimmed = line.trim();

    // from ... import ...
    if trimmed.starts_with("from ")
        && let Some(import_pos) = trimmed.find(" import ")
    {
        let module_path = trimmed[5..import_pos].trim();
        // Only consider relative imports (starting with .)
        if module_path.starts_with('.') {
            return Some(module_path.to_string());
        }
    }

    // import ... (relative imports only)
    if let Some(import_part) = trimmed.strip_prefix("import ") {
        let import_part = import_part.trim();
        let module_path = if let Some(as_pos) = import_part.find(" as ") {
            &import_part[..as_pos]
        } else {
            import_part
        };

        // Only consider relative imports
        if module_path.starts_with('.') {
            return Some(module_path.trim().to_string());
        }
    }

    None
}

/// Extract import path from Go import statements
fn extract_go_import(line: &str) -> Option<String> {
    let trimmed = line.trim();
    let after_import = trimmed.strip_prefix("import ")?.trim();
    extract_quoted_string(after_import)
}

/// Extract import path from Java-like languages (Java, C#, Kotlin, Scala)
fn extract_javalike_import(line: &str) -> Option<String> {
    let trimmed = line.trim();

    // Java/Kotlin: import package.name
    if let Some(import_part) = trimmed.strip_prefix("import ") {
        let import_part = import_part.trim().trim_end_matches(';');
        // Skip static imports
        if import_part.starts_with("static ") {
            return None;
        }
        return Some(import_part.to_string());
    }

    // C#: using namespace
    if !trimmed.contains('=')
        && let Some(import_part) = trimmed.strip_prefix("using ")
    {
        let import_part = import_part.trim().trim_end_matches(';');
        return Some(import_part.to_string());
    }

    None
}

/// Extract include path from C/C++ #include statements
fn extract_c_include(line: &str) -> Option<String> {
    let trimmed = line.trim();
    let after_include = trimmed.strip_prefix("#include ")?.trim();

    // #include "file.h" (local include)
    if let Some(path) = extract_quoted_string(after_include) {
        return Some(path);
    }

    // #include <file.h> (system include - we'll skip these)
    // But if it starts with a relative path marker, keep it
    if after_include.starts_with('<')
        && after_include.contains('/')
        && let Some(end) = after_include.find('>')
    {
        return Some(after_include[1..end].to_string());
    }

    None
}

/// Extract require path from Ruby
fn extract_ruby_require(line: &str) -> Option<String> {
    let trimmed = line.trim();

    // require_relative "path" or require_relative 'path'
    if let Some(after_require) = trimmed.strip_prefix("require_relative ") {
        return extract_quoted_string(after_require.trim());
    }

    // require "path" or require 'path' (only relative paths with ./)
    if let Some(after_require) = trimmed.strip_prefix("require ")
        && let Some(path) = extract_quoted_string(after_require.trim())
        && path.starts_with('.')
    {
        return Some(path);
    }

    None
}

/// Extract require/include path from PHP
fn extract_php_include(line: &str) -> Option<String> {
    let trimmed = line.trim();

    for keyword in ["require", "require_once", "include", "include_once"] {
        if let Some(after_keyword) = trimmed.strip_prefix(keyword) {
            let after_keyword = after_keyword.trim();
            if let Some(path) = extract_quoted_string(after_keyword) {
                return Some(path);
            }
        }
    }

    None
}

/// Extract source path from Shell scripts
fn extract_shell_source(line: &str) -> Option<String> {
    let trimmed = line.trim();

    // source path or . path
    if let Some(path) = trimmed.strip_prefix("source ") {
        let path = path.trim();
        return extract_quoted_string(path).or_else(|| Some(path.to_string()));
    }

    if let Some(path) = trimmed.strip_prefix(". ")
        && !trimmed.starts_with("..")
    {
        let path = path.trim();
        return extract_quoted_string(path).or_else(|| Some(path.to_string()));
    }

    None
}

/// Extract @import path from CSS/SCSS/Less
fn extract_css_import(line: &str) -> Option<String> {
    let trimmed = line.trim();
    let after_import = trimmed.strip_prefix("@import ")?.trim();
    extract_quoted_string(after_import)
}

/// Extract a quoted string (single or double quotes)
fn extract_quoted_string(s: &str) -> Option<String> {
    let trimmed = s.trim();
    let first_char = trimmed.chars().next()?;

    // Check for quote characters
    if ['"', '\'', '`'].contains(&first_char)
        && let Some(end) = trimmed[1..].find(first_char)
    {
        return Some(trimmed[1..end + 1].to_string());
    }

    None
}

/// Extract import statement based on language pattern
fn extract_import(line: &str, pattern: &ImportPattern) -> Option<String> {
    match pattern {
        ImportPattern::Rust => extract_rust_import(line),
        ImportPattern::JavaScript => extract_javascript_import(line),
        ImportPattern::Python => extract_python_import(line),
        ImportPattern::Go => extract_go_import(line),
        ImportPattern::JavaLike => extract_javalike_import(line),
        ImportPattern::CStyle => extract_c_include(line),
        ImportPattern::Ruby => extract_ruby_require(line),
        ImportPattern::Php => extract_php_include(line),
        ImportPattern::Shell => extract_shell_source(line),
        ImportPattern::Css => extract_css_import(line),
    }
}

/// Scan a single file for import statements
pub fn scan_file_for_imports(file_path: &Path) -> Result<Vec<ImportStatement>> {
    let mut imports = Vec::new();

    let extension = file_path.extension().and_then(|e| e.to_str()).unwrap_or("");
    let pattern = match get_import_pattern(extension) {
        Some(p) => p,
        None => return Ok(imports), // Unsupported file type
    };

    let content = fs::read_to_string(file_path)?;

    for (line_number, line) in content.lines().enumerate() {
        if let Some(imported_path) = extract_import(line, &pattern) {
            imports.push(ImportStatement {
                file_path: file_path.to_string_lossy().to_string(),
                line_number: line_number + 1, // 1-based
                line_content: line.trim().to_string(),
                imported_path,
            });
        }
    }

    Ok(imports)
}

/// Build a map of all files in the project for quick lookup
pub fn build_file_map(base_path: &Path) -> HashMap<String, PathBuf> {
    let mut file_map = HashMap::new();

    for entry in WalkDir::new(base_path)
        .into_iter()
        .filter_entry(|e| {
            if e.file_type().is_dir() {
                let dir_name = e.file_name().to_string_lossy();
                !is_non_source_dir(&dir_name)
            } else {
                true
            }
        })
        .filter_map(|e| e.ok())
    {
        if entry.file_type().is_file() {
            let path = entry.path();
            if let Ok(relative_path) = path.strip_prefix(base_path) {
                let key = relative_path.to_string_lossy().to_string();
                file_map.insert(key, path.to_path_buf());
            }
        }
    }

    file_map
}

/// Resolve an import path to an actual file path
pub fn resolve_import_path(
    import_path: &str,
    source_file: &Path,
    base_path: &Path,
    file_map: &HashMap<String, PathBuf>,
) -> Option<PathBuf> {
    let source_dir = source_file.parent()?;

    // Handle different types of imports
    if import_path.starts_with('.') {
        // Relative import (./file or ../file)
        resolve_relative_import(import_path, source_dir, base_path)
    } else if import_path.contains("::") {
        // Rust-style module path (crate::module::submodule)
        resolve_rust_module_path(import_path, source_file, base_path, file_map)
    } else if import_path.contains('/') {
        // Path-like import
        resolve_path_import(import_path, base_path, file_map)
    } else {
        // Package/module name - we'll skip these as they're external
        None
    }
}

/// Resolve relative imports like ./file or ../file
fn resolve_relative_import(
    import_path: &str,
    source_dir: &Path,
    base_path: &Path,
) -> Option<PathBuf> {
    let _import_path_clean = import_path
        .trim_start_matches("./")
        .trim_start_matches("../");

    // Try to resolve from source directory
    let candidate = source_dir.join(import_path);

    // First, check if the path is a directory and try index files
    // This handles: import foo from './folder' -> './folder/index.ts'
    if candidate.is_dir() {
        for index_name in ["index", "mod", "__init__"] {
            for idx_ext in ["ts", "tsx", "js", "jsx", "rs", "py"] {
                let index_path = candidate.join(format!("{}.{}", index_name, idx_ext));
                if index_path.exists() && index_path.starts_with(base_path) {
                    // Canonicalize to resolve .. and . in the path
                    if let Ok(canonical) = index_path.canonicalize() {
                        return Some(canonical);
                    }
                    return Some(index_path);
                }
            }
        }
    }

    // Try common extensions if no extension provided
    let extensions = [
        "", ".ts", ".tsx", ".js", ".jsx", ".rs", ".py", ".go", ".java", ".rb", ".php",
    ];

    for ext in extensions {
        let path_with_ext = if ext.is_empty() {
            candidate.clone()
        } else {
            // Append extension to the path
            let mut path_str = candidate.to_string_lossy().to_string();
            path_str.push_str(ext);
            PathBuf::from(path_str)
        };

        if path_with_ext.exists() && path_with_ext.starts_with(base_path) {
            // Canonicalize to resolve .. and . in the path
            if let Ok(canonical) = path_with_ext.canonicalize() {
                return Some(canonical);
            }
            return Some(path_with_ext);
        }

        // Also check if path with extension is a directory with index file
        // This handles edge cases like './folder.component' -> './folder.component/index.ts'
        if path_with_ext.is_dir() {
            for index_name in ["index", "mod", "__init__"] {
                for idx_ext in ["ts", "tsx", "js", "jsx", "rs", "py"] {
                    let index_path = path_with_ext.join(format!("{}.{}", index_name, idx_ext));
                    if index_path.exists() && index_path.starts_with(base_path) {
                        return Some(index_path);
                    }
                }
            }
        }
    }

    None
}

/// Resolve Rust module paths like crate::module::submodule
fn resolve_rust_module_path(
    import_path: &str,
    source_file: &Path,
    base_path: &Path,
    _file_map: &HashMap<String, PathBuf>,
) -> Option<PathBuf> {
    // Convert crate::module::submodule to module/submodule.rs or module/submodule/mod.rs
    let path_str = if let Some(stripped) = import_path.strip_prefix("crate::") {
        stripped
    } else if let Some(stripped) = import_path.strip_prefix("super::") {
        // Handle super:: by going up one directory
        return resolve_super_path(stripped, source_file, base_path);
    } else {
        import_path.strip_prefix("self::")?
    };

    // Convert :: to /
    let path_parts: Vec<&str> = path_str.split("::").collect();

    // Find src directory
    let src_dir = find_src_directory(base_path)?;

    // Try module/file.rs
    let mut module_path = src_dir.clone();
    for part in &path_parts {
        module_path = module_path.join(part);
    }

    if module_path.with_extension("rs").exists() {
        return Some(module_path.with_extension("rs"));
    }

    // Try module/mod.rs
    let mod_path = module_path.join("mod.rs");
    if mod_path.exists() {
        return Some(mod_path);
    }

    None
}

/// Resolve super:: paths in Rust
fn resolve_super_path(
    remaining_path: &str,
    source_file: &Path,
    base_path: &Path,
) -> Option<PathBuf> {
    let source_dir = source_file.parent()?;
    let parent_dir = source_dir.parent()?;

    if !parent_dir.starts_with(base_path) {
        return None;
    }

    let path_parts: Vec<&str> = remaining_path.split("::").collect();
    let mut module_path = parent_dir.to_path_buf();

    for part in &path_parts {
        module_path = module_path.join(part);
    }

    if module_path.with_extension("rs").exists() {
        return Some(module_path.with_extension("rs"));
    }

    let mod_path = module_path.join("mod.rs");
    if mod_path.exists() {
        return Some(mod_path);
    }

    None
}

/// Find the src directory in a Rust project
fn find_src_directory(base_path: &Path) -> Option<PathBuf> {
    let src_dir = base_path.join("src");
    if src_dir.is_dir() {
        return Some(src_dir);
    }

    // Look for src in subdirectories
    for entry in (fs::read_dir(base_path).ok()?).flatten() {
        let path = entry.path();
        if path.is_dir() {
            let nested_src = path.join("src");
            if nested_src.is_dir() {
                return Some(nested_src);
            }
        }
    }

    None
}

/// Resolve path-based imports
fn resolve_path_import(
    import_path: &str,
    _base_path: &Path,
    file_map: &HashMap<String, PathBuf>,
) -> Option<PathBuf> {
    // Try direct lookup
    if let Some(path) = file_map.get(import_path) {
        return Some(path.clone());
    }

    // Try with common extensions
    for ext in [
        "ts", "tsx", "js", "jsx", "rs", "py", "go", "java", "rb", "php",
    ] {
        let with_ext = format!("{}.{}", import_path, ext);
        if let Some(path) = file_map.get(&with_ext) {
            return Some(path.clone());
        }
    }

    // Try as directory with index
    for index_name in ["index", "mod", "__init__"] {
        for ext in ["ts", "tsx", "js", "jsx", "rs", "py"] {
            let index_path = format!("{}/{}.{}", import_path, index_name, ext);
            if let Some(path) = file_map.get(&index_path) {
                return Some(path.clone());
            }
        }
    }

    None
}

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

    #[test]
    fn test_extract_rust_import() {
        assert_eq!(
            extract_rust_import("use crate::models::Feature;"),
            Some("crate::models::Feature".to_string())
        );
        assert_eq!(
            extract_rust_import("use super::helper;"),
            Some("super::helper".to_string())
        );
        assert_eq!(
            extract_rust_import("use self::utils;"),
            Some("self::utils".to_string())
        );
    }

    #[test]
    fn test_extract_javascript_import() {
        assert_eq!(
            extract_javascript_import("import { Feature } from './models';"),
            Some("./models".to_string())
        );
        assert_eq!(
            extract_javascript_import("const x = require('../utils');"),
            Some("../utils".to_string())
        );
        assert_eq!(
            extract_javascript_import("export { Feature } from './models';"),
            Some("./models".to_string())
        );
    }

    #[test]
    fn test_extract_python_import() {
        assert_eq!(
            extract_python_import("from .models import Feature"),
            Some(".models".to_string())
        );
        assert_eq!(
            extract_python_import("from ..utils import helper"),
            Some("..utils".to_string())
        );
    }

    #[test]
    fn test_extract_quoted_string() {
        assert_eq!(
            extract_quoted_string("\"./path/to/file\""),
            Some("./path/to/file".to_string())
        );
        assert_eq!(
            extract_quoted_string("'./path/to/file'"),
            Some("./path/to/file".to_string())
        );
    }
}