Skip to content

Commit 5608d36

Browse files
committed
fix @export UndefinedField
1 parent 3942894 commit 5608d36

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
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::{
@@ -509,6 +510,13 @@ fn check_require_table_const_with_export<'a>(
509510
) -> Option<&'a ModuleInfo> {
510511
// 获取前缀表达式的语义信息
511512
let prefix_expr = index_expr.get_prefix_expr()?;
513+
if let Some(call_expr) = LuaCallExpr::cast(prefix_expr.syntax().clone()) {
514+
let module_info = parse_require_expr_module_info(semantic_model, &call_expr)?;
515+
if module_info.is_export(semantic_model.get_db()) {
516+
return Some(module_info);
517+
}
518+
}
519+
512520
let semantic_info = semantic_model.get_semantic_info(prefix_expr.syntax().clone().into())?;
513521

514522
// 检查是否是声明引用
@@ -529,3 +537,23 @@ fn check_require_table_const_with_export<'a>(
529537
}
530538
None
531539
}
540+
541+
pub fn parse_require_expr_module_info<'a>(
542+
semantic_model: &'a SemanticModel,
543+
call_expr: &LuaCallExpr,
544+
) -> Option<&'a ModuleInfo> {
545+
let arg_list = call_expr.get_args_list()?;
546+
let first_arg = arg_list.get_args().next()?;
547+
let require_path_type = semantic_model.infer_expr(first_arg.clone()).ok()?;
548+
let module_path: String = match &require_path_type {
549+
LuaType::StringConst(module_path) => module_path.as_ref().to_string(),
550+
_ => {
551+
return None;
552+
}
553+
};
554+
555+
semantic_model
556+
.get_db()
557+
.get_module_index()
558+
.find_module(&module_path)
559+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,5 +755,12 @@ mod test {
755755
a.func()
756756
"#,
757757
));
758+
759+
assert!(!ws.check_code_for(
760+
DiagnosticCode::UndefinedField,
761+
r#"
762+
local a = require("a").ABC
763+
"#,
764+
));
758765
}
759766
}

0 commit comments

Comments
 (0)