Skip to content

Commit 594f66e

Browse files
committed
feat(导出): 添加默认导出作用域并完善导出逻辑
添加 Default 导出作用域,根据配置文件处理默认导出 修改导出检查逻辑,默认情况下库文件不可见 完善导出标签解析,支持更多表达式类型
1 parent 9d4f8c6 commit 594f66e

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

crates/emmylua_code_analysis/src/compilation/analyzer/doc/property_tags.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::compilation::analyzer::doc::tags::report_orphan_tag;
1111
use emmylua_parser::{
1212
LuaAst, LuaAstNode, LuaDocDescriptionOwner, LuaDocTagAsync, LuaDocTagDeprecated,
1313
LuaDocTagExport, LuaDocTagNodiscard, LuaDocTagReadonly, LuaDocTagSource, LuaDocTagVersion,
14-
LuaDocTagVisibility, LuaTableExpr,
14+
LuaDocTagVisibility, LuaExpr,
1515
};
1616

1717
pub fn analyze_visibility(
@@ -121,11 +121,27 @@ pub fn analyze_export(analyzer: &mut DocAnalyzer, tag: LuaDocTagExport) -> Optio
121121
};
122122
let owner_id = match owner {
123123
LuaAst::LuaReturnStat(return_stat) => {
124-
let return_table_expr = return_stat.child::<LuaTableExpr>()?;
125-
LuaSemanticDeclId::LuaDecl(LuaDeclId::new(
126-
analyzer.file_id,
127-
return_table_expr.get_position(),
128-
))
124+
let expr = return_stat.child::<LuaExpr>()?;
125+
match expr {
126+
LuaExpr::NameExpr(name_expr) => {
127+
let name = name_expr.get_name_text()?;
128+
let tree = analyzer
129+
.db
130+
.get_decl_index()
131+
.get_decl_tree(&analyzer.file_id)?;
132+
let decl = tree.find_local_decl(&name, name_expr.get_position())?;
133+
134+
Some(LuaSemanticDeclId::LuaDecl(decl.get_id()))
135+
}
136+
LuaExpr::ClosureExpr(closure) => Some(LuaSemanticDeclId::Signature(
137+
LuaSignatureId::from_closure(analyzer.file_id, &closure),
138+
)),
139+
LuaExpr::TableExpr(table_expr) => Some(LuaSemanticDeclId::LuaDecl(LuaDeclId::new(
140+
analyzer.file_id,
141+
table_expr.get_position(),
142+
))),
143+
_ => None,
144+
}?
129145
}
130146
_ => get_owner_id_or_report(analyzer, &tag)?,
131147
};
@@ -134,10 +150,10 @@ pub fn analyze_export(analyzer: &mut DocAnalyzer, tag: LuaDocTagExport) -> Optio
134150
match scope_text.as_str() {
135151
"namespace" => LuaExportScope::Namespace,
136152
"global" => LuaExportScope::Global,
137-
_ => LuaExportScope::Global, // 默认为 global
153+
_ => LuaExportScope::Default,
138154
}
139155
} else {
140-
LuaExportScope::Global // 没有参数时默认为 global
156+
LuaExportScope::Default
141157
};
142158

143159
let export = LuaExport {

crates/emmylua_code_analysis/src/db_index/property/property.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ pub enum LuaDeprecated {
127127

128128
#[derive(Debug, Clone, PartialEq, Eq)]
129129
pub enum LuaExportScope {
130+
Default, // 默认声明, 会根据配置文件作不同的处理.
130131
Global,
131132
Namespace,
132133
}

crates/emmylua_code_analysis/src/semantic/visibility/export.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn check_export_visibility(
99
) -> Option<bool> {
1010
// 检查模块是否有 export 标记
1111
let Some(export) = module_info.get_export(semantic_model.get_db()) else {
12-
return Some(true);
12+
return check_default_export_visibility(semantic_model, module_info);
1313
};
1414

1515
match export.scope {
@@ -35,10 +35,31 @@ pub fn check_export_visibility(
3535
return Some(true);
3636
}
3737
}
38-
_ => {
38+
LuaExportScope::Global => {
3939
return Some(true);
4040
}
41+
LuaExportScope::Default => {
42+
return check_default_export_visibility(semantic_model, module_info);
43+
}
4144
}
4245

4346
Some(false)
4447
}
48+
49+
/// 检查默认导出作用域下的可见性
50+
///
51+
/// 默认情况下, 如果被声明为库文件, 则我们不认为是可见的.
52+
/// 否则认为是可见的.
53+
fn check_default_export_visibility(
54+
semantic_model: &SemanticModel,
55+
module_info: &ModuleInfo,
56+
) -> Option<bool> {
57+
if semantic_model
58+
.db
59+
.get_module_index()
60+
.is_library(&module_info.file_id)
61+
{
62+
return Some(false);
63+
}
64+
Some(true)
65+
}

0 commit comments

Comments
 (0)