|
| 1 | +use std::{ |
| 2 | + collections::{HashMap, HashSet}, |
| 3 | + env, fs, |
| 4 | + path::{Path, PathBuf}, |
| 5 | + process, |
| 6 | +}; |
| 7 | + |
| 8 | +use anyhow::Result; |
| 9 | +use config::get_compiler_options; |
| 10 | +use css_parser::{extract_classes, ClassName}; |
| 11 | +use tsx_parser::{extract_default_css_imports, extract_used_classes}; |
| 12 | +use utils::{process_relative_import, replace_aliases}; |
| 13 | + |
| 14 | +mod config; |
| 15 | +mod css_parser; |
| 16 | +mod tsx_parser; |
| 17 | +mod utils; |
| 18 | + |
| 19 | +fn list_files_in_directory(path: PathBuf, exclude: Vec<String>) -> Vec<String> { |
| 20 | + let mut files = Vec::new(); |
| 21 | + |
| 22 | + if let Ok(entries) = fs::read_dir(&path) { |
| 23 | + for entry in entries.flatten() { |
| 24 | + let path = entry.path(); |
| 25 | + |
| 26 | + if path.is_dir() { |
| 27 | + if let Some(p) = path.file_name() { |
| 28 | + let p_str = p.to_string_lossy(); |
| 29 | + if p_str.starts_with('.') || exclude.iter().any(|i| p_str == *i) { |
| 30 | + continue; |
| 31 | + } |
| 32 | + } |
| 33 | + files.extend(list_files_in_directory(path, exclude.clone())); |
| 34 | + } else if path.is_file() { |
| 35 | + if let Some(path_str) = path.to_str() { |
| 36 | + files.push(path_str.to_string()); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } else { |
| 41 | + eprintln!("Cannot open target dir: {:?}", path); |
| 42 | + } |
| 43 | + |
| 44 | + files |
| 45 | +} |
| 46 | + |
| 47 | +fn main() -> Result<()> { |
| 48 | + const COLOR_BLUE: &str = "\x1b[34m"; |
| 49 | + const COLOR_YELLOW: &str = "\x1b[33m"; |
| 50 | + const COLOR_GREEN: &str = "\x1b[32m"; |
| 51 | + const COLOR_RED: &str = "\x1b[31m"; |
| 52 | + const COLOR_RESET: &str = "\u{001B}[0m"; |
| 53 | + |
| 54 | + let args: Vec<String> = env::args().collect(); |
| 55 | + let path = args.get(1).unwrap_or_else(|| { |
| 56 | + eprintln!( |
| 57 | + "\n{}Error{}: Linting path must be specified", |
| 58 | + COLOR_RED, COLOR_RESET |
| 59 | + ); |
| 60 | + process::exit(1); |
| 61 | + }); |
| 62 | + |
| 63 | + if let Err(e) = env::set_current_dir(Path::new(path)) { |
| 64 | + eprintln!( |
| 65 | + "\n{}Error{}: Failed to set current directory: {}", |
| 66 | + COLOR_RED, COLOR_RESET, e |
| 67 | + ); |
| 68 | + process::exit(1); |
| 69 | + } |
| 70 | + let tsconfig = get_compiler_options(); |
| 71 | + |
| 72 | + let dir = list_files_in_directory(Path::new(".").to_path_buf(), tsconfig.exclude); |
| 73 | + |
| 74 | + let mut used_classnames: HashMap<String, HashSet<String>> = Default::default(); |
| 75 | + let mut defined_classnames: HashMap<String, HashSet<ClassName>> = Default::default(); |
| 76 | + |
| 77 | + for entry in &dir { |
| 78 | + let path = entry.replace("\\", "/"); |
| 79 | + |
| 80 | + if path.ends_with(".tsx") { |
| 81 | + let code = fs::read_to_string(entry)?; |
| 82 | + let imported_css = extract_default_css_imports(&code); |
| 83 | + |
| 84 | + for (mut style_path, class_names) in imported_css { |
| 85 | + process_relative_import(Path::new(entry), &mut style_path)?; |
| 86 | + replace_aliases(&mut style_path, tsconfig.compiler_options.paths.clone()); |
| 87 | + |
| 88 | + let used_fields = extract_used_classes(&code, &class_names); |
| 89 | + used_classnames |
| 90 | + .entry(style_path) |
| 91 | + .or_insert_with(HashSet::new) |
| 92 | + .extend(used_fields); |
| 93 | + } |
| 94 | + } else if path.ends_with(".module.css") { |
| 95 | + let code = fs::read_to_string(entry)?; |
| 96 | + let classes = extract_classes(&code); |
| 97 | + defined_classnames |
| 98 | + .entry(path) |
| 99 | + .or_insert_with(HashSet::new) |
| 100 | + .extend(classes); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + let mut files_count = 0; |
| 105 | + let mut errors_count = 0; |
| 106 | + println!(); // Initial spacing |
| 107 | + |
| 108 | + for (css_file, mut classes) in defined_classnames { |
| 109 | + if let Some(used_css) = used_classnames.get(&css_file) { |
| 110 | + classes.retain(|v| !used_css.contains(&v.class_name)); |
| 111 | + } |
| 112 | + |
| 113 | + if classes.is_empty() { |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + files_count += 1; |
| 118 | + errors_count += classes.len(); |
| 119 | + |
| 120 | + println!("{}{}{}", COLOR_BLUE, css_file, COLOR_RESET); |
| 121 | + for extra in classes { |
| 122 | + println!( |
| 123 | + "{}{}:{} {}Warn{}: Unused class `{}` found", |
| 124 | + COLOR_YELLOW, |
| 125 | + extra.line_index + 1, |
| 126 | + extra.column_index + 1, |
| 127 | + COLOR_YELLOW, |
| 128 | + COLOR_RESET, |
| 129 | + extra.class_name |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + println!(); |
| 134 | + } |
| 135 | + |
| 136 | + if errors_count == 0 { |
| 137 | + println!("{}✔{} No CSS lint warnings found", COLOR_GREEN, COLOR_RESET); |
| 138 | + } else { |
| 139 | + println!( |
| 140 | + "Found {}{} warnings{} in {} files", |
| 141 | + COLOR_YELLOW, errors_count, COLOR_RESET, files_count |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + Ok(()) |
| 146 | +} |
0 commit comments