|
| 1 | +use std::collections::HashSet; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + DiagnosticCode, LuaType, SemanticModel, TypeCheckFailReason, TypeCheckResult, |
| 5 | + diagnostic::checker::{generic::infer_doc_type::infer_doc_type, humanize_lint_type}, |
| 6 | +}; |
| 7 | +use emmylua_parser::{ |
| 8 | + LuaAstNode, LuaDocAttributeUse, LuaDocTagAttributeUse, LuaDocType, LuaExpr, LuaLiteralExpr, |
| 9 | +}; |
| 10 | +use rowan::TextRange; |
| 11 | + |
| 12 | +use super::{Checker, DiagnosticContext}; |
| 13 | + |
| 14 | +pub struct AttributeCheckChecker; |
| 15 | + |
| 16 | +impl Checker for AttributeCheckChecker { |
| 17 | + const CODES: &[DiagnosticCode] = &[ |
| 18 | + DiagnosticCode::AttributeParamTypeMismatch, |
| 19 | + DiagnosticCode::AttributeMissingParameter, |
| 20 | + DiagnosticCode::AttributeRedundantParameter, |
| 21 | + ]; |
| 22 | + |
| 23 | + fn check(context: &mut DiagnosticContext, semantic_model: &SemanticModel) { |
| 24 | + let root = semantic_model.get_root().clone(); |
| 25 | + for tag_use in root.descendants::<LuaDocTagAttributeUse>() { |
| 26 | + for attribute_use in tag_use.get_attribute_uses() { |
| 27 | + check_attribute_use(context, semantic_model, &attribute_use); |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +fn check_attribute_use( |
| 34 | + context: &mut DiagnosticContext, |
| 35 | + semantic_model: &SemanticModel, |
| 36 | + attribute_use: &LuaDocAttributeUse, |
| 37 | +) -> Option<()> { |
| 38 | + let attribute_type = |
| 39 | + infer_doc_type(semantic_model, &LuaDocType::Name(attribute_use.get_type()?)); |
| 40 | + let LuaType::Ref(type_id) = attribute_type else { |
| 41 | + return None; |
| 42 | + }; |
| 43 | + let type_decl = semantic_model |
| 44 | + .get_db() |
| 45 | + .get_type_index() |
| 46 | + .get_type_decl(&type_id)?; |
| 47 | + if !type_decl.is_attribute() { |
| 48 | + return None; |
| 49 | + } |
| 50 | + let LuaType::DocAttribute(attr_def) = type_decl.get_attribute_type()? else { |
| 51 | + return None; |
| 52 | + }; |
| 53 | + |
| 54 | + let def_params = attr_def.get_params(); |
| 55 | + let args = match attribute_use.get_arg_list() { |
| 56 | + Some(arg_list) => arg_list.get_args().collect::<Vec<_>>(), |
| 57 | + None => vec![], |
| 58 | + }; |
| 59 | + check_param_count(context, &def_params, &attribute_use, &args); |
| 60 | + check_param(context, semantic_model, &def_params, args); |
| 61 | + |
| 62 | + Some(()) |
| 63 | +} |
| 64 | + |
| 65 | +/// 检查参数数量是否匹配 |
| 66 | +fn check_param_count( |
| 67 | + context: &mut DiagnosticContext, |
| 68 | + def_params: &[(String, Option<LuaType>)], |
| 69 | + attribute_use: &LuaDocAttributeUse, |
| 70 | + args: &Vec<LuaLiteralExpr>, |
| 71 | +) -> Option<()> { |
| 72 | + let call_args_count = args.len(); |
| 73 | + // 调用参数少于定义参数, 需要考虑可空参数 |
| 74 | + if call_args_count < def_params.len() { |
| 75 | + for def_param in def_params[call_args_count..].iter() { |
| 76 | + if def_param.0 == "..." { |
| 77 | + break; |
| 78 | + } |
| 79 | + if def_param.1.as_ref().is_some_and(|typ| is_nullable(typ)) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + context.add_diagnostic( |
| 83 | + DiagnosticCode::AttributeMissingParameter, |
| 84 | + match args.last() { |
| 85 | + Some(arg) => arg.get_range(), |
| 86 | + None => attribute_use.get_range(), |
| 87 | + }, |
| 88 | + t!( |
| 89 | + "expected %{num} parameters but found %{found_num}", |
| 90 | + num = def_params.len(), |
| 91 | + found_num = call_args_count |
| 92 | + ) |
| 93 | + .to_string(), |
| 94 | + None, |
| 95 | + ); |
| 96 | + } |
| 97 | + } |
| 98 | + // 调用参数多于定义参数, 需要考虑可变参数 |
| 99 | + else if call_args_count > def_params.len() { |
| 100 | + // 参数定义中最后一个参数是 `...` |
| 101 | + if def_params.last().is_some_and(|(name, typ)| { |
| 102 | + name == "..." || typ.as_ref().is_some_and(|typ| typ.is_variadic()) |
| 103 | + }) { |
| 104 | + return Some(()); |
| 105 | + } |
| 106 | + for arg in args[def_params.len()..].iter() { |
| 107 | + context.add_diagnostic( |
| 108 | + DiagnosticCode::AttributeRedundantParameter, |
| 109 | + arg.get_range(), |
| 110 | + t!( |
| 111 | + "expected %{num} parameters but found %{found_num}", |
| 112 | + num = def_params.len(), |
| 113 | + found_num = call_args_count |
| 114 | + ) |
| 115 | + .to_string(), |
| 116 | + None, |
| 117 | + ); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + Some(()) |
| 122 | +} |
| 123 | + |
| 124 | +/// 检查参数是否匹配 |
| 125 | +fn check_param( |
| 126 | + context: &mut DiagnosticContext, |
| 127 | + semantic_model: &SemanticModel, |
| 128 | + def_params: &[(String, Option<LuaType>)], |
| 129 | + args: Vec<LuaLiteralExpr>, |
| 130 | +) -> Option<()> { |
| 131 | + let mut call_arg_types = Vec::new(); |
| 132 | + for arg in &args { |
| 133 | + let arg_type = semantic_model |
| 134 | + .infer_expr(LuaExpr::LiteralExpr(arg.clone())) |
| 135 | + .ok()?; |
| 136 | + call_arg_types.push(arg_type); |
| 137 | + } |
| 138 | + |
| 139 | + for (idx, param) in def_params.iter().enumerate() { |
| 140 | + if param.0 == "..." { |
| 141 | + if call_arg_types.len() < idx { |
| 142 | + break; |
| 143 | + } |
| 144 | + if let Some(variadic_type) = param.1.clone() { |
| 145 | + for arg_type in call_arg_types[idx..].iter() { |
| 146 | + let result = semantic_model.type_check_detail(&variadic_type, arg_type); |
| 147 | + if result.is_err() { |
| 148 | + add_type_check_diagnostic( |
| 149 | + context, |
| 150 | + semantic_model, |
| 151 | + args.get(idx)?.get_range(), |
| 152 | + &variadic_type, |
| 153 | + arg_type, |
| 154 | + result, |
| 155 | + ); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + break; |
| 160 | + } |
| 161 | + if let Some(param_type) = param.1.clone() { |
| 162 | + let arg_type = call_arg_types.get(idx).unwrap_or(&LuaType::Any); |
| 163 | + let result = semantic_model.type_check_detail(¶m_type, arg_type); |
| 164 | + if result.is_err() { |
| 165 | + add_type_check_diagnostic( |
| 166 | + context, |
| 167 | + semantic_model, |
| 168 | + args.get(idx)?.get_range(), |
| 169 | + ¶m_type, |
| 170 | + arg_type, |
| 171 | + result, |
| 172 | + ); |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + Some(()) |
| 177 | +} |
| 178 | + |
| 179 | +fn add_type_check_diagnostic( |
| 180 | + context: &mut DiagnosticContext, |
| 181 | + semantic_model: &SemanticModel, |
| 182 | + range: TextRange, |
| 183 | + param_type: &LuaType, |
| 184 | + expr_type: &LuaType, |
| 185 | + result: TypeCheckResult, |
| 186 | +) { |
| 187 | + let db = semantic_model.get_db(); |
| 188 | + match result { |
| 189 | + Ok(_) => (), |
| 190 | + Err(reason) => { |
| 191 | + let reason_message = match reason { |
| 192 | + TypeCheckFailReason::TypeNotMatchWithReason(reason) => reason, |
| 193 | + TypeCheckFailReason::TypeNotMatch | TypeCheckFailReason::DonotCheck => { |
| 194 | + "".to_string() |
| 195 | + } |
| 196 | + TypeCheckFailReason::TypeRecursion => "type recursion".to_string(), |
| 197 | + }; |
| 198 | + context.add_diagnostic( |
| 199 | + DiagnosticCode::AttributeParamTypeMismatch, |
| 200 | + range, |
| 201 | + t!( |
| 202 | + "expected `%{source}` but found `%{found}`. %{reason}", |
| 203 | + source = humanize_lint_type(db, param_type), |
| 204 | + found = humanize_lint_type(db, expr_type), |
| 205 | + reason = reason_message |
| 206 | + ) |
| 207 | + .to_string(), |
| 208 | + None, |
| 209 | + ); |
| 210 | + } |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +fn is_nullable(typ: &LuaType) -> bool { |
| 215 | + let mut stack: Vec<LuaType> = Vec::new(); |
| 216 | + stack.push(typ.clone()); |
| 217 | + let mut visited = HashSet::new(); |
| 218 | + while let Some(typ) = stack.pop() { |
| 219 | + if visited.contains(&typ) { |
| 220 | + continue; |
| 221 | + } |
| 222 | + visited.insert(typ.clone()); |
| 223 | + match typ { |
| 224 | + LuaType::Any | LuaType::Unknown | LuaType::Nil => return true, |
| 225 | + LuaType::Union(u) => { |
| 226 | + for t in u.into_vec() { |
| 227 | + stack.push(t); |
| 228 | + } |
| 229 | + } |
| 230 | + _ => {} |
| 231 | + } |
| 232 | + } |
| 233 | + false |
| 234 | +} |
0 commit comments