Skip to content

Commit 4900b05

Browse files
committed
feat: add config strict.require_export_global
用于控制第三方包的默认导出可见性. 默认为 false, 即不启用, 此时允许导入第三包的所有文件.
1 parent f06b80b commit 4900b05

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

crates/emmylua_code_analysis/resources/schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
"arrayIndex": true,
139139
"docBaseConstMatchBaseType": true,
140140
"metaOverrideFileDefine": true,
141+
"requireExportGlobal": false,
141142
"requirePath": false,
142143
"typeCall": false
143144
}
@@ -1044,6 +1045,11 @@
10441045
"type": "boolean",
10451046
"default": true
10461047
},
1048+
"requireExportGlobal": {
1049+
"description": "This option limits the visibility of third-party libraries.\n\nWhen enabled, third-party libraries must use `---@export global` annotation to be importable (i.e., no diagnostic errors and visible in auto-import).",
1050+
"type": "boolean",
1051+
"default": false
1052+
},
10471053
"requirePath": {
10481054
"description": "Whether to enable strict mode require path.",
10491055
"type": "boolean",

crates/emmylua_code_analysis/src/config/configs/strict.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ pub struct EmmyrcStrict {
2727
/// Base constant types defined in doc can match base types, allowing int to match `---@alias id 1|2|3`, same for string.
2828
#[serde(default = "default_false")]
2929
pub doc_base_const_match_base_type: bool,
30+
/// This option limits the visibility of third-party libraries.
31+
///
32+
/// When enabled, third-party libraries must use `---@export global` annotation to be importable (i.e., no diagnostic errors and visible in auto-import).
33+
#[serde(default = "default_false")]
34+
pub require_export_global: bool,
3035
}
3136

3237
impl Default for EmmyrcStrict {
@@ -37,6 +42,7 @@ impl Default for EmmyrcStrict {
3742
array_index: true,
3843
meta_override_file_define: true,
3944
doc_base_const_match_base_type: true,
45+
require_export_global: false,
4046
}
4147
}
4248
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@ pub fn check_export_visibility(
4747
}
4848

4949
/// 检查默认导出作用域下的可见性
50-
///
51-
/// 默认情况下, 如果被声明为库文件, 则我们不认为是可见的.
52-
/// 否则认为是可见的.
5350
fn check_default_export_visibility(
5451
semantic_model: &SemanticModel,
5552
module_info: &ModuleInfo,
5653
) -> Option<bool> {
54+
// 如果没有启用 require_export_global, 则默认认为是可见的.
55+
if !semantic_model.emmyrc.strict.require_export_global {
56+
return Some(true);
57+
}
58+
59+
// 如果被声明为库文件, 则我们不认为是可见的.
5760
if semantic_model
5861
.db
5962
.get_module_index()

0 commit comments

Comments
 (0)