Skip to content

Commit bc83e9b

Browse files
committed
clean code
1 parent fc40734 commit bc83e9b

File tree

4 files changed

+84
-81
lines changed

4 files changed

+84
-81
lines changed

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

Lines changed: 63 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use std::collections::HashSet;
22

33
use emmylua_parser::{
4-
LuaAst, LuaAstNode, LuaElseIfClauseStat, LuaForRangeStat, LuaForStat, LuaIfStat, LuaIndexExpr,
5-
LuaIndexKey, LuaRepeatStat, LuaSyntaxKind, LuaTokenKind, LuaVarExpr, LuaWhileStat,
4+
LuaAst, LuaAstNode, LuaCallExpr, LuaElseIfClauseStat, LuaForRangeStat, LuaForStat, LuaIfStat,
5+
LuaIndexExpr, LuaIndexKey, LuaRepeatStat, LuaSyntaxKind, LuaTokenKind, LuaVarExpr,
6+
LuaWhileStat,
67
};
78

89
use crate::{
910
DiagnosticCode, InferFailReason, LuaMemberKey, LuaSemanticDeclId, LuaType, ModuleInfo,
10-
SemanticModel, enum_variable_is_param,
11+
SemanticModel, enum_variable_is_param, parse_require_module_info,
1112
};
1213

1314
use super::{Checker, DiagnosticContext, humanize_lint_type};
@@ -63,7 +64,7 @@ fn check_index_expr(
6364
let prefix_typ = semantic_model
6465
.infer_expr(index_expr.get_prefix_expr()?)
6566
.unwrap_or(LuaType::Unknown);
66-
// let mut module_info = None;
67+
let module_info = None;
6768

6869
if is_invalid_prefix_type(&prefix_typ) {
6970
// if matches!(prefix_typ, LuaType::TableConst(_)) {
@@ -73,7 +74,7 @@ fn check_index_expr(
7374
// return Some(());
7475
// }
7576
// } else {
76-
77+
// return Some(());
7778
// }
7879
return Some(());
7980
}
@@ -86,7 +87,7 @@ fn check_index_expr(
8687
index_expr,
8788
&index_key,
8889
code,
89-
None,
90+
module_info,
9091
)
9192
.is_some()
9293
{
@@ -479,57 +480,59 @@ fn check_enum_is_param(
479480
)
480481
}
481482

482-
// /// 检查导入的表常量
483-
// fn check_require_table_const_with_export<'a>(
484-
// semantic_model: &'a SemanticModel,
485-
// index_expr: &LuaIndexExpr,
486-
// ) -> Option<&'a ModuleInfo> {
487-
// // 获取前缀表达式的语义信息
488-
// let prefix_expr = index_expr.get_prefix_expr()?;
489-
// if let Some(call_expr) = LuaCallExpr::cast(prefix_expr.syntax().clone()) {
490-
// let module_info = parse_require_expr_module_info(semantic_model, &call_expr)?;
491-
// if module_info.is_export(semantic_model.get_db()) {
492-
// return Some(module_info);
493-
// }
494-
// }
495-
496-
// let semantic_info = semantic_model.get_semantic_info(prefix_expr.syntax().clone().into())?;
497-
498-
// // 检查是否是声明引用
499-
// let decl_id = match semantic_info.semantic_decl? {
500-
// LuaSemanticDeclId::LuaDecl(decl_id) => decl_id,
501-
// _ => return None,
502-
// };
503-
504-
// // 获取声明
505-
// let decl = semantic_model
506-
// .get_db()
507-
// .get_decl_index()
508-
// .get_decl(&decl_id)?;
509-
510-
// let module_info = parse_require_module_info(semantic_model, decl)?;
511-
// if module_info.is_export(semantic_model.get_db()) {
512-
// return Some(module_info);
513-
// }
514-
// None
515-
// }
516-
517-
// pub fn parse_require_expr_module_info<'a>(
518-
// semantic_model: &'a SemanticModel,
519-
// call_expr: &LuaCallExpr,
520-
// ) -> Option<&'a ModuleInfo> {
521-
// let arg_list = call_expr.get_args_list()?;
522-
// let first_arg = arg_list.get_args().next()?;
523-
// let require_path_type = semantic_model.infer_expr(first_arg.clone()).ok()?;
524-
// let module_path: String = match &require_path_type {
525-
// LuaType::StringConst(module_path) => module_path.as_ref().to_string(),
526-
// _ => {
527-
// return None;
528-
// }
529-
// };
530-
531-
// semantic_model
532-
// .get_db()
533-
// .get_module_index()
534-
// .find_module(&module_path)
535-
// }
483+
#[allow(unused)]
484+
/// 检查导入的表常量
485+
fn check_require_table_const_with_export<'a>(
486+
semantic_model: &'a SemanticModel,
487+
index_expr: &LuaIndexExpr,
488+
) -> Option<&'a ModuleInfo> {
489+
// 获取前缀表达式的语义信息
490+
let prefix_expr = index_expr.get_prefix_expr()?;
491+
if let Some(call_expr) = LuaCallExpr::cast(prefix_expr.syntax().clone()) {
492+
let module_info = parse_require_expr_module_info(semantic_model, &call_expr)?;
493+
if module_info.is_export(semantic_model.get_db()) {
494+
return Some(module_info);
495+
}
496+
}
497+
498+
let semantic_info = semantic_model.get_semantic_info(prefix_expr.syntax().clone().into())?;
499+
500+
// 检查是否是声明引用
501+
let decl_id = match semantic_info.semantic_decl? {
502+
LuaSemanticDeclId::LuaDecl(decl_id) => decl_id,
503+
_ => return None,
504+
};
505+
506+
// 获取声明
507+
let decl = semantic_model
508+
.get_db()
509+
.get_decl_index()
510+
.get_decl(&decl_id)?;
511+
512+
let module_info = parse_require_module_info(semantic_model, decl)?;
513+
if module_info.is_export(semantic_model.get_db()) {
514+
return Some(module_info);
515+
}
516+
None
517+
}
518+
519+
#[allow(unused)]
520+
pub fn parse_require_expr_module_info<'a>(
521+
semantic_model: &'a SemanticModel,
522+
call_expr: &LuaCallExpr,
523+
) -> Option<&'a ModuleInfo> {
524+
let arg_list = call_expr.get_args_list()?;
525+
let first_arg = arg_list.get_args().next()?;
526+
let require_path_type = semantic_model.infer_expr(first_arg.clone()).ok()?;
527+
let module_path: String = match &require_path_type {
528+
LuaType::StringConst(module_path) => module_path.as_ref().to_string(),
529+
_ => {
530+
return None;
531+
}
532+
};
533+
534+
semantic_model
535+
.get_db()
536+
.get_module_index()
537+
.find_module(&module_path)
538+
}

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,26 +130,26 @@ mod test {
130130
));
131131
}
132132

133-
// #[test]
134-
// fn test_tuple() {
135-
// let mut ws = VirtualWorkspace::new();
136-
// assert!(ws.check_code_for(
137-
// DiagnosticCode::InjectField,
138-
// r#"
139-
// local a = { 'a' }
140-
// a[#a + 1] = 'b'
141-
// "#
142-
// ));
133+
#[test]
134+
fn test_tuple() {
135+
let mut ws = VirtualWorkspace::new();
136+
assert!(ws.check_code_for(
137+
DiagnosticCode::InjectField,
138+
r#"
139+
local a = { 'a' }
140+
a[#a + 1] = 'b'
141+
"#
142+
));
143143

144-
// assert!(!ws.check_code_for(
145-
// DiagnosticCode::InjectField,
146-
// r#"
147-
// ---@type [ 'a' ]
148-
// local a = { 'a' }
149-
// a[#a + 1] = 'b'
150-
// "#
151-
// ));
152-
// }
144+
// assert!(!ws.check_code_for(
145+
// DiagnosticCode::InjectField,
146+
// r#"
147+
// ---@type [ 'a' ]
148+
// local a = { 'a' }
149+
// a[#a + 1] = 'b'
150+
// "#
151+
// ));
152+
}
153153

154154
// #[test]
155155
// fn test_export() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mod test {
1212
---@readonly
1313
local errorCode = {}
1414
15-
errorCode.NOT_FOUND = 10 --- show warnings attemp modify readonly variables.
15+
errorCode.NOT_FOUND = 10 --- show warnings attempt to modify readonly variables.
1616
"#
1717
));
1818
}

crates/emmylua_code_analysis/src/semantic/semantic_info/infer_expr_semantic_decl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ fn infer_name_expr_semantic_decl(
8686

8787
if let Some(value_expr_id) = decl.get_value_syntax_id() {
8888
match value_expr_id.get_kind() {
89-
LuaSyntaxKind::NameExpr | LuaSyntaxKind::IndexExpr => {
89+
LuaSyntaxKind::NameExpr | LuaSyntaxKind::IndexExpr if decl_type.is_function() => {
9090
let file_id = decl.get_file_id();
9191
let tree = db.get_vfs().get_syntax_tree(&file_id)?;
9292
// second infer

0 commit comments

Comments
 (0)