|
| 1 | +use emmylua_parser::{BinaryOperator, LuaAst, LuaAstNode, LuaBinaryExpr, LuaExpr}; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + diagnostic::checker::humanize_lint_type, DiagnosticCode, LuaMemberKey, LuaType, LuaTypeDeclId, |
| 5 | + SemanticModel, |
| 6 | +}; |
| 7 | + |
| 8 | +use super::{Checker, DiagnosticContext}; |
| 9 | + |
| 10 | +pub struct EnumValueMismatchChecker; |
| 11 | + |
| 12 | +impl Checker for EnumValueMismatchChecker { |
| 13 | + const CODES: &[DiagnosticCode] = &[DiagnosticCode::EnumValueMismatch]; |
| 14 | + |
| 15 | + fn check(context: &mut DiagnosticContext, semantic_model: &SemanticModel) { |
| 16 | + let root = semantic_model.get_root().clone(); |
| 17 | + for node in root.descendants::<LuaAst>() { |
| 18 | + let condition_expr = match node { |
| 19 | + LuaAst::LuaIfStat(if_stat) => if_stat.get_condition_expr(), |
| 20 | + LuaAst::LuaElseIfClauseStat(elseif_stat) => elseif_stat.get_condition_expr(), |
| 21 | + _ => None, |
| 22 | + }; |
| 23 | + |
| 24 | + if let Some(expr) = condition_expr { |
| 25 | + check_condition_expr(context, semantic_model, expr); |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +fn check_condition_expr( |
| 32 | + context: &mut DiagnosticContext, |
| 33 | + semantic_model: &SemanticModel, |
| 34 | + condition_expr: LuaExpr, |
| 35 | +) -> Option<()> { |
| 36 | + if let LuaExpr::BinaryExpr(binary_expr) = condition_expr { |
| 37 | + check_binary_expr(context, semantic_model, binary_expr); |
| 38 | + } |
| 39 | + Some(()) |
| 40 | +} |
| 41 | + |
| 42 | +fn check_binary_expr( |
| 43 | + context: &mut DiagnosticContext, |
| 44 | + semantic_model: &SemanticModel, |
| 45 | + binary_expr: LuaBinaryExpr, |
| 46 | +) -> Option<()> { |
| 47 | + let op_token = binary_expr.get_op_token()?; |
| 48 | + let operator = op_token.get_op(); |
| 49 | + |
| 50 | + if !matches!(operator, BinaryOperator::OpEq | BinaryOperator::OpNe) { |
| 51 | + return Some(()); |
| 52 | + } |
| 53 | + |
| 54 | + let (left_expr, right_expr) = binary_expr.get_exprs()?; |
| 55 | + let left_type = semantic_model.infer_expr(left_expr.clone()).ok()?; |
| 56 | + let right_type = semantic_model.infer_expr(right_expr.clone()).ok()?; |
| 57 | + |
| 58 | + if check_enum_value_pair(context, &right_expr, &left_type, &right_type).is_some() { |
| 59 | + return Some(()); |
| 60 | + } |
| 61 | + if check_enum_value_pair(context, &left_expr, &right_type, &left_type).is_some() { |
| 62 | + return Some(()); |
| 63 | + } |
| 64 | + |
| 65 | + Some(()) |
| 66 | +} |
| 67 | + |
| 68 | +fn check_enum_value_pair( |
| 69 | + context: &mut DiagnosticContext, |
| 70 | + value_expr: &LuaExpr, |
| 71 | + enum_type: &LuaType, |
| 72 | + value_type: &LuaType, |
| 73 | +) -> Option<()> { |
| 74 | + let enum_decl_id = match &enum_type { |
| 75 | + LuaType::Ref(id) | LuaType::Def(id) => id, |
| 76 | + _ => return None, |
| 77 | + }; |
| 78 | + let type_decl = context.db.get_type_index().get_type_decl(enum_decl_id)?; |
| 79 | + if !type_decl.is_enum() { |
| 80 | + return None; |
| 81 | + } |
| 82 | + |
| 83 | + let constant_type = get_constant_type(value_type)?; |
| 84 | + let enum_value_types = get_enum_value_types(context, enum_decl_id)?; |
| 85 | + |
| 86 | + if !enum_value_types.contains(constant_type) { |
| 87 | + let constant_value_str = humanize_lint_type(context.db, constant_type); |
| 88 | + let enum_values_str: Vec<String> = enum_value_types |
| 89 | + .iter() |
| 90 | + .map(|typ| humanize_lint_type(context.db, typ)) |
| 91 | + .collect(); |
| 92 | + context.add_diagnostic( |
| 93 | + DiagnosticCode::EnumValueMismatch, |
| 94 | + value_expr.get_range(), |
| 95 | + t!( |
| 96 | + "Value '%{value}' does not match any enum value. Expected one of: %{enum_values}", |
| 97 | + value = constant_value_str, |
| 98 | + enum_values = enum_values_str.join(", ") |
| 99 | + ) |
| 100 | + .to_string(), |
| 101 | + None, |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + Some(()) |
| 106 | +} |
| 107 | + |
| 108 | +fn get_enum_value_types( |
| 109 | + context: &DiagnosticContext, |
| 110 | + enum_decl_id: &LuaTypeDeclId, |
| 111 | +) -> Option<Vec<LuaType>> { |
| 112 | + let type_decl = context.db.get_type_index().get_type_decl(enum_decl_id)?; |
| 113 | + let mut values = Vec::new(); |
| 114 | + let is_enum_key = type_decl.is_enum_key(); |
| 115 | + |
| 116 | + if let Some(members) = context |
| 117 | + .db |
| 118 | + .get_member_index() |
| 119 | + .get_members(&enum_decl_id.clone().into()) |
| 120 | + { |
| 121 | + for member in members { |
| 122 | + if is_enum_key { |
| 123 | + let key = member.get_key(); |
| 124 | + match key { |
| 125 | + LuaMemberKey::Name(name) => { |
| 126 | + values.push(LuaType::StringConst(name.clone().into())); |
| 127 | + } |
| 128 | + LuaMemberKey::Integer(i) => { |
| 129 | + values.push(LuaType::IntegerConst(*i)); |
| 130 | + } |
| 131 | + LuaMemberKey::ExprType(typ) => { |
| 132 | + if let Some(value) = get_constant_type(typ) { |
| 133 | + values.push(value.clone()); |
| 134 | + } |
| 135 | + } |
| 136 | + _ => {} |
| 137 | + } |
| 138 | + } else { |
| 139 | + if let Some(type_cache) = context |
| 140 | + .db |
| 141 | + .get_type_index() |
| 142 | + .get_type_cache(&member.get_id().into()) |
| 143 | + { |
| 144 | + if let Some(value) = get_constant_type(type_cache.as_type()) { |
| 145 | + values.push(value.clone()); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + Some(values) |
| 153 | +} |
| 154 | + |
| 155 | +fn get_constant_type(typ: &LuaType) -> Option<&LuaType> { |
| 156 | + match typ { |
| 157 | + LuaType::StringConst(_) |
| 158 | + | LuaType::DocStringConst(_) |
| 159 | + | LuaType::IntegerConst(_) |
| 160 | + | LuaType::DocIntegerConst(_) |
| 161 | + | LuaType::FloatConst(_) |
| 162 | + | LuaType::BooleanConst(_) |
| 163 | + | LuaType::DocBooleanConst(_) |
| 164 | + | LuaType::TableConst(_) => Some(typ), |
| 165 | + _ => None, |
| 166 | + } |
| 167 | +} |
0 commit comments