Skip to content

Commit 56b8847

Browse files
committed
diagnostic: clean code
1 parent de9a2e6 commit 56b8847

File tree

10 files changed

+87
-91
lines changed

10 files changed

+87
-91
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use emmylua_parser::{
22
LuaAstNode, LuaAstToken, LuaBlock, LuaClosureExpr, LuaReturnStat, LuaTokenKind,
33
};
44

5-
use crate::{DiagnosticCode, LuaSignatureId, SemanticModel};
5+
use crate::{DiagnosticCode, LuaSignatureId, SemanticModel, SignatureReturnStatus};
66

7-
use super::{return_type_mismatch::has_doc_return_annotation, DiagnosticContext};
7+
use super::DiagnosticContext;
88

99
pub const CODES: &[DiagnosticCode] = &[DiagnosticCode::MissingReturnValue];
1010

@@ -28,13 +28,16 @@ fn check_return_stat(
2828

2929
let signature_id = LuaSignatureId::from_closure(semantic_model.get_file_id(), &closure_expr);
3030
let signature = context.db.get_signature_index().get(&signature_id)?;
31-
let min_return_types = signature.get_return_types().iter()
32-
.filter(|ty| !ty.is_nullable())
31+
let min_return_types = signature
32+
.get_return_types()
33+
.iter()
34+
.filter(|ty| !ty.is_optional())
3335
.cloned()
3436
.collect::<Vec<_>>();
3537

36-
// 如果没有返回值注解, 则不检查
37-
has_doc_return_annotation(&closure_expr)?;
38+
if signature.resolve_return != SignatureReturnStatus::DocResolve {
39+
return None;
40+
}
3841

3942
let disable_return_count_check = min_return_types.iter().any(|ty| ty.is_variadic());
4043

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use emmylua_parser::{LuaAstNode, LuaBlock, LuaClosureExpr, LuaReturnStat};
22

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

5-
use super::{return_type_mismatch::has_doc_return_annotation, DiagnosticContext};
5+
use super::DiagnosticContext;
66

77
pub const CODES: &[DiagnosticCode] = &[DiagnosticCode::RedundantReturnValue];
88

@@ -28,8 +28,10 @@ fn check_return_stat(
2828
let signature = context.db.get_signature_index().get(&signature_id)?;
2929
let return_types = signature.get_return_types();
3030

31-
// 如果没有返回值注解, 则不检查
32-
has_doc_return_annotation(&closure_expr)?;
31+
if signature.resolve_return != SignatureReturnStatus::DocResolve {
32+
return None;
33+
}
34+
3335
let disable_return_count_check = return_types.iter().any(|ty| ty.is_variadic());
3436
let expr_return_len = return_stat.get_expr_list().collect::<Vec<_>>().len();
3537
let return_types_len = return_types.len();

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
use std::ops::Deref;
22

3-
use emmylua_parser::{
4-
LuaAstNode, LuaBlock, LuaClosureExpr, LuaDocTagReturn, LuaExpr, LuaReturnStat,
5-
};
3+
use emmylua_parser::{LuaAstNode, LuaBlock, LuaClosureExpr, LuaExpr, LuaReturnStat};
64
use rowan::TextRange;
75

86
use crate::{
97
humanize_type, DiagnosticCode, LuaMultiReturn, LuaSignatureId, LuaType, RenderLevel,
10-
SemanticModel, TypeCheckFailReason, TypeCheckResult,
8+
SemanticModel, SignatureReturnStatus, TypeCheckFailReason, TypeCheckResult,
119
};
1210

13-
use super::{get_closure_expr_comment, DiagnosticContext};
11+
use super::DiagnosticContext;
1412

1513
pub const CODES: &[DiagnosticCode] = &[DiagnosticCode::ReturnTypeMismatch];
1614

@@ -35,8 +33,9 @@ fn check_return_stat(
3533
let signature_id = LuaSignatureId::from_closure(semantic_model.get_file_id(), &closure_expr);
3634
let signature = context.db.get_signature_index().get(&signature_id)?;
3735
let return_types = signature.get_return_types();
38-
// 如果没有返回值注解, 则不检查
39-
has_doc_return_annotation(&closure_expr)?;
36+
if signature.resolve_return != SignatureReturnStatus::DocResolve {
37+
return None;
38+
}
4039

4140
for (index, expr) in return_stat.get_expr_list().enumerate() {
4241
let return_type = return_types.get(index).unwrap_or(&LuaType::Any);
@@ -175,10 +174,3 @@ fn add_type_check_diagnostic(
175174
},
176175
}
177176
}
178-
179-
pub fn has_doc_return_annotation(closure_expr: &LuaClosureExpr) -> Option<()> {
180-
get_closure_expr_comment(closure_expr)?
181-
.children::<LuaDocTagReturn>()
182-
.next()
183-
.map(|_| ())
184-
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,7 @@ fn check_dots_literal_error(
171171
context.add_diagnostic(
172172
DiagnosticCode::LuaSyntaxError,
173173
literal_expr.get_range(),
174-
t!(
175-
"Cannot use `...` outside a vararg function."
176-
)
177-
.to_string(),
174+
t!("Cannot use `...` outside a vararg function.").to_string(),
178175
None,
179176
);
180177
}

crates/emmylua_code_analysis/src/diagnostic/lua_diagnostic_code.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use serde::{Deserialize, Serialize};
88
)]
99
#[serde(rename_all = "kebab-case")]
1010
pub enum DiagnosticCode {
11-
/// Syntax error. 可能包含 doc 语法错误, 允许抑制
11+
/// Syntax error
1212
SyntaxError,
13-
/// Lua syntax error. lua 的语法错误, 禁止抑制
13+
/// Lua syntax error
1414
LuaSyntaxError,
1515
/// Type not found
1616
TypeNotFound,
@@ -70,7 +70,7 @@ pub enum DiagnosticCode {
7070
UndefinedDocParam,
7171
/// Duplicate doc field
7272
DuplicateDocField,
73-
73+
7474
#[serde(other)]
7575
None,
7676
}
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
#[cfg(test)]
22
mod test {
3-
use crate::{DiagnosticCode, VirtualWorkspace};
3+
// use crate::{DiagnosticCode, VirtualWorkspace};
44

5-
#[test]
6-
fn test() {
7-
let mut ws = VirtualWorkspace::new();
5+
// #[test]
6+
// fn test() {
7+
// let mut ws = VirtualWorkspace::new();
88

9-
assert!(!ws.check_code_for(
10-
DiagnosticCode::DuplicateDocField,
11-
r#"
12-
---@class (partial) Test
13-
---@field name string
14-
---@field name string
15-
local Test = {}
16-
"#
17-
));
18-
}
9+
// assert!(!ws.check_code_for(
10+
// DiagnosticCode::DuplicateDocField,
11+
// r#"
12+
// ---@class (partial) Test
13+
// ---@field name string
14+
// ---@field name string
15+
// local Test = {}
16+
// "#
17+
// ));
18+
// }
1919
}

crates/emmylua_code_analysis/src/diagnostic/test/missing_return_value_test.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ mod tests {
4141
return
4242
end
4343
"#
44-
));
44+
));
4545
}
46-
4746
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
mod await_in_sync_test;
22
mod disable_line_test;
3+
mod duplicate_doc_field_test;
34
mod missing_parameter_test;
45
mod missing_return_value_test;
56
mod param_type_check_test;
67
mod redundant_parameter_test;
78
mod redundant_return_value_test;
8-
mod duplicate_doc_field_test;
9-
mod undefined_field_test;
109
mod return_type_mismatch_test;
10+
mod undefined_field_test;
Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
11
#[cfg(test)]
22
mod test {
3-
use crate::{DiagnosticCode, VirtualWorkspace};
3+
// use crate::{DiagnosticCode, VirtualWorkspace};
44

5-
#[test]
6-
fn test() {
7-
let mut ws = VirtualWorkspace::new();
8-
assert!(!ws.check_code_for(
9-
DiagnosticCode::UndefinedField,
10-
r#"
11-
---@class diagnostic.test3
12-
---@field private a number
5+
// #[test]
6+
// fn test() {
7+
// let mut ws = VirtualWorkspace::new();
8+
// assert!(!ws.check_code_for(
9+
// DiagnosticCode::UndefinedField,
10+
// r#"
11+
// ---@class diagnostic.test3
12+
// ---@field private a number
1313

14-
---@type diagnostic.test3
15-
local test = {}
14+
// ---@type diagnostic.test3
15+
// local test = {}
1616

17-
local b = test.b
18-
"#
19-
));
17+
// local b = test.b
18+
// "#
19+
// ));
2020

21-
assert!(!ws.check_code_for(
22-
DiagnosticCode::UndefinedField,
23-
r#"
24-
---@class diagnostic.test3
25-
---@field private a number
26-
local Test3 = {}
21+
// assert!(!ws.check_code_for(
22+
// DiagnosticCode::UndefinedField,
23+
// r#"
24+
// ---@class diagnostic.test3
25+
// ---@field private a number
26+
// local Test3 = {}
2727

28-
local b = Test3.b
29-
"#
30-
));
31-
}
28+
// local b = Test3.b
29+
// "#
30+
// ));
31+
// }
3232

33-
#[test]
34-
fn test_enum() {
35-
let mut ws = VirtualWorkspace::new();
36-
assert!(!ws.check_code_for(
37-
DiagnosticCode::UndefinedField,
38-
r#"
39-
---@enum diagnostic.enum
40-
local Enum = {
41-
A = 1,
42-
}
33+
// #[test]
34+
// fn test_enum() {
35+
// let mut ws = VirtualWorkspace::new();
36+
// assert!(!ws.check_code_for(
37+
// DiagnosticCode::UndefinedField,
38+
// r#"
39+
// ---@enum diagnostic.enum
40+
// local Enum = {
41+
// A = 1,
42+
// }
4343

44-
local enum_b = Enum["B"]
45-
"#
46-
));
47-
}
44+
// local enum_b = Enum["B"]
45+
// "#
46+
// ));
47+
// }
4848
}

crates/emmylua_ls/src/handlers/code_actions/build_actions.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,20 @@ fn add_disable_code_action(
6363
&t!(
6464
"Disable current line diagnostic (%{name})",
6565
name = diagnostic_code.get_name()
66-
).to_string(),
66+
)
67+
.to_string(),
6768
DisableAction::DisableLine,
6869
diagnostic_code,
6970
file_id,
70-
range,
71+
range,
7172
)));
7273

7374
actions.push(CodeActionOrCommand::Command(make_disable_code_command(
7475
&t!(
7576
"Disable all diagnostics in current file (%{name})",
7677
name = diagnostic_code.get_name()
77-
).to_string(),
78+
)
79+
.to_string(),
7880
DisableAction::DisableFile,
7981
diagnostic_code,
8082
file_id,
@@ -85,7 +87,8 @@ fn add_disable_code_action(
8587
&t!(
8688
"Disable all diagnostics in current project (%{name})",
8789
name = diagnostic_code.get_name()
88-
).to_string(),
90+
)
91+
.to_string(),
8992
DisableAction::DisableProject,
9093
diagnostic_code,
9194
file_id,

0 commit comments

Comments
 (0)