diff --git a/crates/emmylua_code_analysis/src/diagnostic/checker/incomplete_signature_doc.rs b/crates/emmylua_code_analysis/src/diagnostic/checker/incomplete_signature_doc.rs index 9a9b0fca7..53b038b08 100644 --- a/crates/emmylua_code_analysis/src/diagnostic/checker/incomplete_signature_doc.rs +++ b/crates/emmylua_code_analysis/src/diagnostic/checker/incomplete_signature_doc.rs @@ -44,18 +44,26 @@ fn check_doc( let comment = get_closure_expr_comment(closure_expr); - if comment.is_none() && is_global { + let code = if is_global { + DiagnosticCode::MissingGlobalDoc + } else { + DiagnosticCode::IncompleteSignatureDoc + }; + + if comment.is_none() { + let message = if is_global { + t!( + "Missing comment for global function `%{name}`.", + name = function_name + ) + } else { + t!( + "Missing comment for function `%{name}`.", + name = function_name + ) + }; if let Some(stat) = closure_expr.get_parent::() { - context.add_diagnostic( - DiagnosticCode::MissingGlobalDoc, - stat.get_range(), - t!( - "Missing comment for global function `%{name}`.", - name = function_name - ) - .to_string(), - None, - ); + context.add_diagnostic(code, stat.get_range(), message.to_string(), None); } return Some(()); } @@ -64,12 +72,6 @@ fn check_doc( return Some(()); }; - let code = if is_global { - DiagnosticCode::MissingGlobalDoc - } else { - DiagnosticCode::IncompleteSignatureDoc - }; - let doc_param_names: HashSet = comment .children::() .filter_map(|param| { @@ -84,11 +86,6 @@ fn check_doc( .map(|return_doc| return_doc.get_types().count()) .sum(); - // 如果文档中没有参数和返回值注解, 且不是全局函数, 则不检查 - if doc_param_names.is_empty() && doc_return_len == 0 && !is_global { - return Some(()); - } - check_params( context, closure_expr, diff --git a/crates/emmylua_code_analysis/src/diagnostic/test/incomplete_signature_doc_test.rs b/crates/emmylua_code_analysis/src/diagnostic/test/incomplete_signature_doc_test.rs index 4986fd185..5a1df2ab0 100644 --- a/crates/emmylua_code_analysis/src/diagnostic/test/incomplete_signature_doc_test.rs +++ b/crates/emmylua_code_analysis/src/diagnostic/test/incomplete_signature_doc_test.rs @@ -24,12 +24,29 @@ mod tests { let mut ws = VirtualWorkspace::new(); ws.enable_full_diagnostic(); + // TODO: closures are not detected as functions in the semantic model + // assert!(!ws.check_code_for( + // DiagnosticCode::MissingGlobalDoc, + // r#" + // local c = function(x, y) + // return x + y + // end + // "# + // )); + assert!(!ws.check_code_for( DiagnosticCode::IncompleteSignatureDoc, r#" - ---@param p number - local function FLPR3(p, e) - return 0 + local function do_add(x, y) + return x + y + end + "# + )); + + assert!(!ws.check_code_for( + DiagnosticCode::IncompleteSignatureDoc, + r#" + local function noop() end "# )); @@ -38,24 +55,26 @@ mod tests { DiagnosticCode::IncompleteSignatureDoc, r#" ---@param p number - local function FLPR3(p) + local function FLPR3(p, e) return 0 end "# )); - assert!(ws.check_code_for( + assert!(!ws.check_code_for( DiagnosticCode::IncompleteSignatureDoc, r#" + ---@param p number local function FLPR3(p) return 0 end "# )); - assert!(ws.check_code_for( + + assert!(!ws.check_code_for( DiagnosticCode::IncompleteSignatureDoc, r#" - --- + --- function without param signature local function FLPR3(p) return 0 end