Skip to content

Commit 2ab91c1

Browse files
committed
refactor(diagnostic): table check
1 parent 281c90a commit 2ab91c1

11 files changed

Lines changed: 659 additions & 332 deletions

File tree

crates/emmylua_code_analysis/src/diagnostic/checker/assign_type_mismatch.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ use super::{
1717
pub struct AssignTypeMismatchChecker;
1818

1919
impl Checker for AssignTypeMismatchChecker {
20-
const CODES: &[DiagnosticCode] = &[DiagnosticCode::AssignTypeMismatch];
20+
const CODES: &[DiagnosticCode] = &[
21+
DiagnosticCode::AssignTypeMismatch,
22+
DiagnosticCode::MissingFields,
23+
];
2124

2225
fn check(context: &mut DiagnosticContext, semantic_model: &SemanticModel) {
2326
for node in semantic_model.get_root().descendants::<LuaAst>() {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub use render_type_mismatch::render_diagnostic_detail;
4444
use emmylua_parser::{
4545
LuaAstNode, LuaClosureExpr, LuaComment, LuaReturnStat, LuaStat, LuaSyntaxKind,
4646
};
47+
use hashbrown::HashMap;
4748
use lsp_types::{Diagnostic, DiagnosticSeverity, DiagnosticTag, NumberOrString};
4849
use rowan::TextRange;
4950
use std::sync::Arc;
@@ -93,7 +94,6 @@ pub fn check_file(context: &mut DiagnosticContext, semantic_model: &SemanticMode
9394
context,
9495
semantic_model,
9596
);
96-
run_check::<table::missing_fields::MissingFieldsChecker>(context, semantic_model);
9797
run_check::<need_check_nil::NeedCheckNilChecker>(context, semantic_model);
9898
run_check::<undefined_doc_param::UndefinedDocParamChecker>(context, semantic_model);
9999
run_check::<redefined_local::RedefinedLocalChecker>(context, semantic_model);
@@ -145,6 +145,8 @@ pub struct DiagnosticContext<'a> {
145145
diagnostics: Vec<Diagnostic>,
146146
diagnostic_ranges: Vec<(TextRange, DiagnosticCode)>,
147147
pub config: Arc<LuaDiagnosticConfig>,
148+
/// 必填字段缓存
149+
required_fields_cache: HashMap<LuaType, Arc<Vec<String>>>,
148150
}
149151

150152
impl<'a> DiagnosticContext<'a> {
@@ -155,6 +157,7 @@ impl<'a> DiagnosticContext<'a> {
155157
diagnostics: Vec::new(),
156158
diagnostic_ranges: Vec::new(),
157159
config,
160+
required_fields_cache: HashMap::new(),
158161
}
159162
}
160163

crates/emmylua_code_analysis/src/diagnostic/checker/param_check/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ impl Checker for ParamCheckChecker {
1515
const CODES: &[DiagnosticCode] = &[
1616
DiagnosticCode::ParamTypeMismatch,
1717
DiagnosticCode::AssignTypeMismatch,
18+
DiagnosticCode::MissingFields,
1819
DiagnosticCode::MissingParameter,
1920
DiagnosticCode::RedundantParameter,
2021
];
@@ -24,7 +25,8 @@ impl Checker for ParamCheckChecker {
2425
let redundant_enabled =
2526
context.is_checker_enable_by_code(&DiagnosticCode::RedundantParameter);
2627
let type_enabled = context.is_checker_enable_by_code(&DiagnosticCode::ParamTypeMismatch)
27-
|| context.is_checker_enable_by_code(&DiagnosticCode::AssignTypeMismatch);
28+
|| context.is_checker_enable_by_code(&DiagnosticCode::AssignTypeMismatch)
29+
|| context.is_checker_enable_by_code(&DiagnosticCode::MissingFields);
2830
let call_check_enabled = missing_enabled || redundant_enabled || type_enabled;
2931

3032
let root = semantic_model.get_root().clone();

crates/emmylua_code_analysis/src/diagnostic/checker/param_check/param_type_mismatch.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ pub(super) fn check_param_type_mismatch(
109109
)
110110
.is_handled()
111111
{
112+
if current_candidates.len() == 1 {
113+
arg_index += 1;
114+
continue;
115+
}
112116
return;
113117
}
114118

crates/emmylua_code_analysis/src/diagnostic/checker/return_type_mismatch.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ impl Checker for ReturnTypeMismatch {
1919
const CODES: &[DiagnosticCode] = &[
2020
DiagnosticCode::ReturnTypeMismatch,
2121
DiagnosticCode::AssignTypeMismatch,
22+
DiagnosticCode::MissingFields,
2223
];
2324

2425
fn check(context: &mut DiagnosticContext, semantic_model: &SemanticModel) {
@@ -93,14 +94,16 @@ fn check_return_stat(
9394
{
9495
if return_expr_type.is_table()
9596
&& let Some(return_expr) = return_exprs.get(index)
96-
{
97-
check_table_assignment_diagnostics(
97+
&& check_table_assignment_diagnostics(
9898
context,
9999
semantic_model,
100100
return_expr,
101101
return_expr_type,
102102
check_type,
103-
);
103+
)
104+
.is_handled()
105+
{
106+
continue;
104107
}
105108

106109
add_type_check_diagnostic(

crates/emmylua_code_analysis/src/diagnostic/checker/table/missing_fields.rs

Lines changed: 0 additions & 238 deletions
This file was deleted.

crates/emmylua_code_analysis/src/diagnostic/checker/table/mod.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
use emmylua_parser::{LuaAstNode, LuaExpr};
1+
use emmylua_parser::LuaExpr;
22

3-
use crate::{DiagnosticCode, LuaType, SemanticModel};
3+
use crate::{LuaType, SemanticModel};
44

55
use super::DiagnosticContext;
66

7-
pub mod missing_fields;
87
pub mod table_field_type_mismatch;
98
pub mod table_type_mismatch;
109

1110
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1211
pub(crate) enum TableAssignmentOutcome {
1312
NotTable,
13+
Fallback,
1414
Assignable,
1515
Reported,
1616
NoDiagnostic,
1717
}
1818

1919
impl TableAssignmentOutcome {
2020
pub(crate) fn is_handled(self) -> bool {
21-
matches!(self, Self::Assignable | Self::Reported)
21+
matches!(self, Self::Assignable | Self::Reported | Self::NoDiagnostic)
2222
}
2323
}
2424

@@ -33,12 +33,6 @@ pub(crate) fn check_table_assignment_diagnostics(
3333
return TableAssignmentOutcome::NotTable;
3434
};
3535

36-
if context
37-
.has_diagnostic_codes_in_range(table_expr.get_range(), &[DiagnosticCode::MissingFields])
38-
{
39-
return TableAssignmentOutcome::Reported;
40-
}
41-
4236
table_type_mismatch::check_table_type_mismatch(
4337
context,
4438
semantic_model,

0 commit comments

Comments
 (0)