|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +//! WokeLang Linter |
| 3 | +//! |
| 4 | +//! Static analysis tool for WokeLang code quality and style checking. |
| 5 | +
|
| 6 | +use crate::ast::*; |
| 7 | +use std::collections::HashSet; |
| 8 | + |
| 9 | +/// Lint severity level |
| 10 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 11 | +pub enum Severity { |
| 12 | + Error, |
| 13 | + Warning, |
| 14 | + Info, |
| 15 | +} |
| 16 | + |
| 17 | +/// Lint diagnostic |
| 18 | +#[derive(Debug, Clone)] |
| 19 | +pub struct Diagnostic { |
| 20 | + pub severity: Severity, |
| 21 | + pub message: String, |
| 22 | + pub span: Span, |
| 23 | + pub code: String, |
| 24 | +} |
| 25 | + |
| 26 | +/// Linter for WokeLang code |
| 27 | +pub struct Linter { |
| 28 | + diagnostics: Vec<Diagnostic>, |
| 29 | + defined_vars: HashSet<String>, |
| 30 | + used_vars: HashSet<String>, |
| 31 | +} |
| 32 | + |
| 33 | +impl Linter { |
| 34 | + pub fn new() -> Self { |
| 35 | + Self { |
| 36 | + diagnostics: Vec::new(), |
| 37 | + defined_vars: HashSet::new(), |
| 38 | + used_vars: HashSet::new(), |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /// Lint a program and return diagnostics |
| 43 | + pub fn lint(&mut self, program: &Program) -> Vec<Diagnostic> { |
| 44 | + self.diagnostics.clear(); |
| 45 | + self.defined_vars.clear(); |
| 46 | + self.used_vars.clear(); |
| 47 | + |
| 48 | + // Lint all top-level items |
| 49 | + for item in &program.items { |
| 50 | + self.lint_top_level_item(item); |
| 51 | + } |
| 52 | + |
| 53 | + // Check for unused variables |
| 54 | + for var in &self.defined_vars { |
| 55 | + if !self.used_vars.contains(var) { |
| 56 | + self.diagnostics.push(Diagnostic { |
| 57 | + severity: Severity::Warning, |
| 58 | + message: format!("Unused variable: {}", var), |
| 59 | + span: Span::default(), |
| 60 | + code: "W001".to_string(), |
| 61 | + }); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + self.diagnostics.clone() |
| 66 | + } |
| 67 | + |
| 68 | + fn lint_top_level_item(&mut self, item: &TopLevelItem) { |
| 69 | + match item { |
| 70 | + TopLevelItem::Function(func) => self.lint_function(func), |
| 71 | + TopLevelItem::ConsentBlock(_) => {}, |
| 72 | + TopLevelItem::GratitudeDecl(_) => {}, |
| 73 | + TopLevelItem::WorkerDef(_) => {}, |
| 74 | + TopLevelItem::SideQuestDef(_) => {}, |
| 75 | + TopLevelItem::SuperpowerDecl(_) => {}, |
| 76 | + TopLevelItem::ModuleImport(_) => {}, |
| 77 | + TopLevelItem::Pragma(_) => {}, |
| 78 | + TopLevelItem::TypeDef(_) => {}, |
| 79 | + TopLevelItem::ConstDef(_) => {}, |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + fn lint_function(&mut self, func: &FunctionDef) { |
| 84 | + // Check function name style |
| 85 | + if !func.name.chars().next().map_or(false, |c| c.is_lowercase()) { |
| 86 | + self.diagnostics.push(Diagnostic { |
| 87 | + severity: Severity::Warning, |
| 88 | + message: format!("Function '{}' should start with lowercase", func.name), |
| 89 | + span: func.span.clone(), |
| 90 | + code: "W002".to_string(), |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + // Track parameters as defined variables |
| 95 | + for param in &func.params { |
| 96 | + self.defined_vars.insert(param.name.clone()); |
| 97 | + } |
| 98 | + |
| 99 | + // Lint function body |
| 100 | + for stmt in &func.body { |
| 101 | + self.lint_statement(stmt); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + fn lint_statement(&mut self, stmt: &Statement) { |
| 106 | + match stmt { |
| 107 | + Statement::VarDecl(var_decl) => { |
| 108 | + self.defined_vars.insert(var_decl.name.clone()); |
| 109 | + self.lint_expr(&var_decl.value); |
| 110 | + } |
| 111 | + Statement::Assignment(assignment) => { |
| 112 | + if !self.defined_vars.contains(&assignment.target) { |
| 113 | + self.diagnostics.push(Diagnostic { |
| 114 | + severity: Severity::Error, |
| 115 | + message: format!("Assignment to undefined variable: {}", assignment.target), |
| 116 | + span: assignment.span.clone(), |
| 117 | + code: "E001".to_string(), |
| 118 | + }); |
| 119 | + } |
| 120 | + self.used_vars.insert(assignment.target.clone()); |
| 121 | + self.lint_expr(&assignment.value); |
| 122 | + } |
| 123 | + Statement::Return(ret) => { |
| 124 | + self.lint_expr(&ret.value); |
| 125 | + } |
| 126 | + Statement::Conditional(cond) => { |
| 127 | + self.lint_expr(&cond.condition); |
| 128 | + for stmt in &cond.then_branch { |
| 129 | + self.lint_statement(stmt); |
| 130 | + } |
| 131 | + if let Some(else_branch) = &cond.else_branch { |
| 132 | + for stmt in else_branch { |
| 133 | + self.lint_statement(stmt); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + Statement::Loop(loop_stmt) => { |
| 138 | + self.lint_expr(&loop_stmt.count); |
| 139 | + for stmt in &loop_stmt.body { |
| 140 | + self.lint_statement(stmt); |
| 141 | + } |
| 142 | + } |
| 143 | + Statement::AttemptBlock(attempt) => { |
| 144 | + for stmt in &attempt.body { |
| 145 | + self.lint_statement(stmt); |
| 146 | + } |
| 147 | + } |
| 148 | + Statement::ConsentBlock(consent) => { |
| 149 | + for stmt in &consent.body { |
| 150 | + self.lint_statement(stmt); |
| 151 | + } |
| 152 | + } |
| 153 | + Statement::Expression(expr) => { |
| 154 | + self.lint_expr(expr); |
| 155 | + } |
| 156 | + Statement::WorkerSpawn(_) => {} |
| 157 | + Statement::Complain(_) => {} |
| 158 | + Statement::EmoteAnnotated(emote) => { |
| 159 | + self.lint_statement(&emote.statement); |
| 160 | + } |
| 161 | + Statement::Decide(decide) => { |
| 162 | + self.lint_expr(&decide.scrutinee); |
| 163 | + for arm in &decide.arms { |
| 164 | + for stmt in &arm.body { |
| 165 | + self.lint_statement(stmt); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + fn lint_expr(&mut self, expr: &Spanned<Expr>) { |
| 173 | + match &expr.node { |
| 174 | + Expr::Literal(_) => {} |
| 175 | + Expr::Identifier(name) => { |
| 176 | + self.used_vars.insert(name.clone()); |
| 177 | + if !self.defined_vars.contains(name) { |
| 178 | + self.diagnostics.push(Diagnostic { |
| 179 | + severity: Severity::Warning, |
| 180 | + message: format!("Use of possibly undefined variable: {}", name), |
| 181 | + span: expr.span.clone(), |
| 182 | + code: "W003".to_string(), |
| 183 | + }); |
| 184 | + } |
| 185 | + } |
| 186 | + Expr::Binary(_, left, right) => { |
| 187 | + self.lint_expr(left); |
| 188 | + self.lint_expr(right); |
| 189 | + } |
| 190 | + Expr::Unary(_, operand) => { |
| 191 | + self.lint_expr(operand); |
| 192 | + } |
| 193 | + Expr::Call(_, args) => { |
| 194 | + for arg in args { |
| 195 | + self.lint_expr(arg); |
| 196 | + } |
| 197 | + } |
| 198 | + Expr::CallExpr(callee, args) => { |
| 199 | + self.lint_expr(callee); |
| 200 | + for arg in args { |
| 201 | + self.lint_expr(arg); |
| 202 | + } |
| 203 | + } |
| 204 | + Expr::Array(elements) => { |
| 205 | + for elem in elements { |
| 206 | + self.lint_expr(elem); |
| 207 | + } |
| 208 | + } |
| 209 | + Expr::Index(array, index) => { |
| 210 | + self.lint_expr(array); |
| 211 | + self.lint_expr(index); |
| 212 | + } |
| 213 | + Expr::Okay(val) => self.lint_expr(val), |
| 214 | + Expr::Oops(val) => self.lint_expr(val), |
| 215 | + Expr::Unwrap(val) => self.lint_expr(val), |
| 216 | + Expr::Lambda(lambda) => { |
| 217 | + // Track lambda params |
| 218 | + for param in &lambda.params { |
| 219 | + self.defined_vars.insert(param.name.clone()); |
| 220 | + } |
| 221 | + |
| 222 | + match &lambda.body { |
| 223 | + LambdaBody::Expr(expr) => self.lint_expr(expr), |
| 224 | + LambdaBody::Block(stmts) => { |
| 225 | + for stmt in stmts { |
| 226 | + self.lint_statement(stmt); |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | + Expr::UnitMeasurement(value, _unit) => { |
| 232 | + self.lint_expr(value); |
| 233 | + } |
| 234 | + Expr::GratitudeLiteral(_) => {} |
| 235 | + } |
| 236 | + } |
| 237 | +} |
| 238 | + |
| 239 | +impl Default for Linter { |
| 240 | + fn default() -> Self { |
| 241 | + Self::new() |
| 242 | + } |
| 243 | +} |
| 244 | + |
| 245 | +#[cfg(test)] |
| 246 | +mod tests { |
| 247 | + use super::*; |
| 248 | + |
| 249 | + #[test] |
| 250 | + fn test_linter_unused_variable() { |
| 251 | + let mut linter = Linter::new(); |
| 252 | + |
| 253 | + let program = Program { |
| 254 | + items: vec![TopLevelItem::Function(FunctionDef { |
| 255 | + name: "test".to_string(), |
| 256 | + params: vec![], |
| 257 | + body: vec![Statement::VarDecl(VarDecl { |
| 258 | + name: "unused".to_string(), |
| 259 | + value: Spanned { |
| 260 | + node: Expr::Literal(Literal::Integer(42)), |
| 261 | + span: Span::default(), |
| 262 | + }, |
| 263 | + unit: None, |
| 264 | + span: Span::default(), |
| 265 | + })], |
| 266 | + return_type: None, |
| 267 | + type_params: vec![], |
| 268 | + emote: None, |
| 269 | + hello: None, |
| 270 | + goodbye: None, |
| 271 | + span: Span::default(), |
| 272 | + })], |
| 273 | + }; |
| 274 | + |
| 275 | + let diagnostics = linter.lint(&program); |
| 276 | + assert!(diagnostics.iter().any(|d| d.code == "W001")); |
| 277 | + } |
| 278 | + |
| 279 | + #[test] |
| 280 | + fn test_linter_uppercase_function() { |
| 281 | + let mut linter = Linter::new(); |
| 282 | + |
| 283 | + let program = Program { |
| 284 | + items: vec![TopLevelItem::Function(FunctionDef { |
| 285 | + name: "Test".to_string(), // Should be lowercase |
| 286 | + params: vec![], |
| 287 | + body: vec![], |
| 288 | + return_type: None, |
| 289 | + type_params: vec![], |
| 290 | + emote: None, |
| 291 | + hello: None, |
| 292 | + goodbye: None, |
| 293 | + span: Span::default(), |
| 294 | + })], |
| 295 | + }; |
| 296 | + |
| 297 | + let diagnostics = linter.lint(&program); |
| 298 | + assert!(diagnostics.iter().any(|d| d.code == "W002")); |
| 299 | + } |
| 300 | + |
| 301 | + #[test] |
| 302 | + fn test_linter_undefined_assignment() { |
| 303 | + let mut linter = Linter::new(); |
| 304 | + |
| 305 | + let program = Program { |
| 306 | + items: vec![TopLevelItem::Function(FunctionDef { |
| 307 | + name: "test".to_string(), |
| 308 | + params: vec![], |
| 309 | + body: vec![Statement::Assignment(Assignment { |
| 310 | + target: "undefined".to_string(), |
| 311 | + value: Spanned { |
| 312 | + node: Expr::Literal(Literal::Integer(42)), |
| 313 | + span: Span::default(), |
| 314 | + }, |
| 315 | + span: Span::default(), |
| 316 | + })], |
| 317 | + return_type: None, |
| 318 | + type_params: vec![], |
| 319 | + emote: None, |
| 320 | + hello: None, |
| 321 | + goodbye: None, |
| 322 | + span: Span::default(), |
| 323 | + })], |
| 324 | + }; |
| 325 | + |
| 326 | + let diagnostics = linter.lint(&program); |
| 327 | + assert!(diagnostics.iter().any(|d| d.code == "E001")); |
| 328 | + } |
| 329 | +} |
0 commit comments