Skip to content

Commit ff11fb0

Browse files
committed
add missing parameter check
1 parent 10cbdb9 commit ff11fb0

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

crates/code_analysis/src/diagnostic/checker/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod unused;
44
mod deprecated;
55
mod undefined_global;
66
mod access_invisible;
7+
mod missing_parameter;
78

89
use lsp_types::{Diagnostic, DiagnosticSeverity, DiagnosticTag, NumberOrString};
910
use rowan::TextRange;
@@ -34,6 +35,7 @@ pub fn check_file(context: &mut DiagnosticContext, semantic_model:&mut SemanticM
3435
check!(deprecated);
3536
check!(undefined_global);
3637
check!(access_invisible);
38+
check!(missing_parameter);
3739

3840
Some(())
3941
}

0 commit comments

Comments
 (0)