|
| 1 | +use emmylua_parser::{LuaAstNode, LuaAstToken, LuaCallExpr, LuaGeneralToken}; |
| 2 | + |
| 3 | +use crate::{DiagnosticCode, SemanticModel}; |
| 4 | + |
| 5 | +use super::DiagnosticContext; |
| 6 | + |
| 7 | +pub const CODES: &[DiagnosticCode] = &[DiagnosticCode::MissingParameter]; |
| 8 | + |
| 9 | +pub fn check(context: &mut DiagnosticContext, semantic_model: &mut SemanticModel) -> Option<()> { |
| 10 | + let root = semantic_model.get_root().clone(); |
| 11 | + for call_expr in root.descendants::<LuaCallExpr>() { |
| 12 | + check_call_expr(context, semantic_model, call_expr); |
| 13 | + } |
| 14 | + |
| 15 | + Some(()) |
| 16 | +} |
| 17 | + |
| 18 | +fn check_call_expr( |
| 19 | + context: &mut DiagnosticContext, |
| 20 | + semantic_model: &mut SemanticModel, |
| 21 | + call_expr: LuaCallExpr, |
| 22 | +) -> Option<()> { |
| 23 | + let func = semantic_model.infer_call_expr_func(call_expr.clone(), None)?; |
| 24 | + let params = func.get_params(); |
| 25 | + let args_count = call_expr.get_args_list()?.get_args().count(); |
| 26 | + if args_count < params.len() { |
| 27 | + let mut miss_parameter_info = Vec::new(); |
| 28 | + for i in args_count..params.len() { |
| 29 | + let param_info = params.get(i)?; |
| 30 | + if param_info.0 == "..." { |
| 31 | + break; |
| 32 | + } |
| 33 | + |
| 34 | + let typ = param_info.1.clone(); |
| 35 | + if typ.is_none() || !typ.unwrap().is_optional() { |
| 36 | + miss_parameter_info.push(t!("missing parameter: %{name}", name = param_info.0)); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + let right_paren = call_expr |
| 41 | + .get_args_list()? |
| 42 | + .tokens::<LuaGeneralToken>() |
| 43 | + .last()?; |
| 44 | + if !miss_parameter_info.is_empty() { |
| 45 | + context.add_diagnostic( |
| 46 | + DiagnosticCode::MissingParameter, |
| 47 | + right_paren.get_range(), |
| 48 | + t!( |
| 49 | + "expected %{num} but founded %{found_num}.\n%{infos}", |
| 50 | + num = params.len(), |
| 51 | + found_num = args_count, |
| 52 | + infos = miss_parameter_info.join("\n") |
| 53 | + ).to_string(), |
| 54 | + None, |
| 55 | + ); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + Some(()) |
| 60 | +} |
0 commit comments