Skip to content

Commit a03d71e

Browse files
committed
refactor(visibility): improve type visibility
1. 类型默认为 public 2. 添加 internal 可见性修饰符, 即包内可见, 在这里包的概念是`workspace.library`引入的内容 3. 删除 ---@export
1 parent 48ee7d7 commit a03d71e

58 files changed

Lines changed: 1229 additions & 619 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

crates/emmylua_code_analysis/locales/lint.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,10 @@ Cannot use `...` outside a vararg function.:
261261
en: "type recursion"
262262
zh_CN: "类型递归"
263263
zh_HK: "類型遞歸"
264-
"Module '%{module}' is not visible. It has @export restrictions.":
265-
en: "Module '%{module}' is not visible. It has @export restrictions."
266-
zh_CN: "模块 '%{module}' 不可见。它有 @export 限制。"
267-
zh_HK: "模組 '%{module}' 不可見。它有 @export 限制。"
264+
"module '%{module}' visibility is not `public`":
265+
en: "module '%{module}' visibility is not `public`"
266+
zh_CN: "模块 '%{module}' 可见性不为 `public`"
267+
zh_HK: "模組 '%{module}' 可見性不為 `public`"
268268
"Unknown doc tag: `%{name}`":
269269
en: "Unknown doc tag: `%{name}`"
270270
# TODO: translate

crates/emmylua_code_analysis/resources/schema.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@
138138
"arrayIndex": true,
139139
"docBaseConstMatchBaseType": true,
140140
"metaOverrideFileDefine": true,
141-
"requireExportGlobal": false,
142141
"requirePath": false,
143142
"typeCall": false
144143
}
@@ -442,6 +441,16 @@
442441
"description": "Call to a non-callable value",
443442
"type": "string",
444443
"const": "call-non-callable"
444+
},
445+
{
446+
"description": "invisible-type-reference",
447+
"type": "string",
448+
"const": "invisible-type-reference"
449+
},
450+
{
451+
"description": "inconsistent-type-visibility",
452+
"type": "string",
453+
"const": "inconsistent-type-visibility"
445454
}
446455
]
447456
},
@@ -1100,11 +1109,6 @@
11001109
"type": "boolean",
11011110
"default": true
11021111
},
1103-
"requireExportGlobal": {
1104-
"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).",
1105-
"type": "boolean",
1106-
"default": false
1107-
},
11081112
"requirePath": {
11091113
"description": "Whether to enable strict mode require path.",
11101114
"type": "boolean",

crates/emmylua_code_analysis/src/compilation/analyzer/decl/docs.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use flagset::FlagSet;
77
use rowan::TextRange;
88

99
use crate::{
10-
LuaTypeDecl, LuaTypeDeclId,
11-
db_index::{LuaDeclTypeKind, LuaTypeFlag},
10+
LuaTypeDecl, LuaTypeDeclId, ModuleVisibility,
11+
db_index::{LuaDeclTypeKind, LuaTypeFlag, WorkspaceId},
1212
};
1313

1414
use super::DeclAnalyzer;
@@ -30,7 +30,7 @@ fn get_type_flag_value(
3030
let mut attr: FlagSet<LuaTypeFlag> = if analyzer.is_meta {
3131
LuaTypeFlag::Meta.into()
3232
} else {
33-
LuaTypeFlag::None.into()
33+
FlagSet::default()
3434
};
3535

3636
if let Some(flag) = flag {
@@ -48,6 +48,12 @@ fn get_type_flag_value(
4848
"constructor" => {
4949
attr |= LuaTypeFlag::Constructor;
5050
}
51+
"public" => {
52+
attr |= LuaTypeFlag::Public;
53+
}
54+
"internal" => {
55+
attr |= LuaTypeFlag::Internal;
56+
}
5157
"private" => {
5258
attr |= LuaTypeFlag::Private;
5359
}
@@ -91,7 +97,7 @@ pub fn analyze_doc_tag_attribute(
9197
&name,
9298
range,
9399
LuaDeclTypeKind::Attribute,
94-
LuaTypeFlag::None.into(),
100+
FlagSet::default(),
95101
);
96102
Some(())
97103
}
@@ -136,7 +142,7 @@ pub fn analyze_doc_tag_meta(analyzer: &mut DeclAnalyzer, tag: LuaDocTagMeta) ->
136142
analyzer
137143
.db
138144
.get_module_index_mut()
139-
.set_module_visibility(file_id, false);
145+
.set_module_visibility(file_id, ModuleVisibility::Hide);
140146
} else {
141147
let workspace_id = analyzer
142148
.db
@@ -183,6 +189,12 @@ fn add_type_decl(
183189
flag: FlagSet<LuaTypeFlag>,
184190
) {
185191
let file_id = analyzer.get_file_id();
192+
let workspace_id = analyzer
193+
.db
194+
.get_module_index()
195+
.get_workspace_id(file_id)
196+
.or(analyzer.context.workspace_id)
197+
.unwrap_or(WorkspaceId::MAIN);
186198
let type_index = analyzer.db.get_type_index_mut();
187199

188200
let basic_name = name;
@@ -198,6 +210,14 @@ fn add_type_decl(
198210
let simple_name = id.get_simple_name();
199211
type_index.add_type_decl(
200212
file_id,
201-
LuaTypeDecl::new(file_id, range, simple_name.to_string(), kind, flag, id),
213+
LuaTypeDecl::new(
214+
file_id,
215+
range,
216+
workspace_id,
217+
simple_name.to_string(),
218+
kind,
219+
flag,
220+
id,
221+
),
202222
);
203223
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ mod type_ref_tags;
1010

1111
use super::AnalyzeContext;
1212
use crate::{
13-
FileId, LuaSemanticDeclId, WorkspaceId,
13+
FileId, LuaSemanticDeclId,
1414
compilation::analyzer::AnalysisPipeline,
15-
db_index::{DbIndex, LuaTypeDeclId},
15+
db_index::{DbIndex, LuaTypeDeclId, WorkspaceId},
1616
profile::Profile,
1717
};
1818
use emmylua_parser::{LuaAstNode, LuaComment, LuaSyntaxNode};

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

Lines changed: 36 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,55 @@
11
use crate::{
2-
AsyncState, LuaDeclId, LuaExport, LuaExportScope, LuaNoDiscard, LuaSemanticDeclId,
3-
LuaSignatureId, PropertyDeclFeature,
2+
AsyncState, LuaDeclId, LuaNoDiscard, LuaSemanticDeclId, LuaSignatureId, PropertyDeclFeature,
3+
compilation::analyzer::doc::tags::report_orphan_tag,
44
};
55

66
use super::{
77
DocAnalyzer,
88
tags::{find_owner_closure_or_report, get_owner_id_or_report},
99
};
10-
use crate::compilation::analyzer::doc::tags::report_orphan_tag;
1110
use emmylua_parser::{
1211
LuaAst, LuaAstNode, LuaDocDescriptionOwner, LuaDocTagAsync, LuaDocTagDeprecated,
13-
LuaDocTagExport, LuaDocTagNodiscard, LuaDocTagReadonly, LuaDocTagSource, LuaDocTagVersion,
14-
LuaDocTagVisibility, LuaExpr,
12+
LuaDocTagNodiscard, LuaDocTagReadonly, LuaDocTagSource, LuaDocTagVersion, LuaDocTagVisibility,
13+
LuaExpr,
1514
};
1615

1716
pub fn analyze_visibility(
1817
analyzer: &mut DocAnalyzer,
1918
visibility: LuaDocTagVisibility,
2019
) -> Option<()> {
20+
let Some(owner) = analyzer.comment.get_owner() else {
21+
report_orphan_tag(analyzer, &visibility);
22+
return None;
23+
};
24+
let owner_id = match owner {
25+
LuaAst::LuaReturnStat(return_stat) => {
26+
let expr = return_stat.child::<LuaExpr>()?;
27+
match expr {
28+
// 返回变量不能附加可见性
29+
// LuaExpr::NameExpr(name_expr) => {
30+
// let name = name_expr.get_name_text()?;
31+
// let tree = analyzer
32+
// .db
33+
// .get_decl_index()
34+
// .get_decl_tree(&analyzer.file_id)?;
35+
// let decl = tree.find_local_decl(&name, name_expr.get_position())?;
36+
37+
// Some(LuaSemanticDeclId::LuaDecl(decl.get_id()))
38+
// }
39+
LuaExpr::ClosureExpr(closure) => Some(LuaSemanticDeclId::Signature(
40+
LuaSignatureId::from_closure(analyzer.file_id, &closure),
41+
)),
42+
LuaExpr::TableExpr(table_expr) => Some(LuaSemanticDeclId::LuaDecl(LuaDeclId::new(
43+
analyzer.file_id,
44+
table_expr.get_position(),
45+
))),
46+
_ => None,
47+
}?
48+
}
49+
_ => get_owner_id_or_report(analyzer, &visibility)?,
50+
};
51+
2152
let visibility_kind = visibility.get_visibility_token()?.get_visibility()?;
22-
let owner_id = get_owner_id_or_report(analyzer, &visibility)?;
2353

2454
analyzer.db.get_property_index_mut().add_visibility(
2555
analyzer.file_id,
@@ -114,60 +144,6 @@ pub fn analyze_async(analyzer: &mut DocAnalyzer, tag: LuaDocTagAsync) -> Option<
114144
Some(())
115145
}
116146

117-
pub fn analyze_export(analyzer: &mut DocAnalyzer, tag: LuaDocTagExport) -> Option<()> {
118-
let Some(owner) = analyzer.comment.get_owner() else {
119-
report_orphan_tag(analyzer, &tag);
120-
return None;
121-
};
122-
let owner_id = match owner {
123-
LuaAst::LuaReturnStat(return_stat) => {
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-
}?
145-
}
146-
_ => get_owner_id_or_report(analyzer, &tag)?,
147-
};
148-
149-
let export_scope = if let Some(scope_text) = tag.get_export_scope() {
150-
match scope_text.as_str() {
151-
"namespace" => LuaExportScope::Namespace,
152-
"global" => LuaExportScope::Global,
153-
_ => LuaExportScope::Default,
154-
}
155-
} else {
156-
LuaExportScope::Default
157-
};
158-
159-
let export = LuaExport {
160-
scope: export_scope,
161-
};
162-
163-
analyzer
164-
.db
165-
.get_property_index_mut()
166-
.add_export(analyzer.file_id, owner_id, export);
167-
168-
Some(())
169-
}
170-
171147
pub fn analyze_readonly(analyzer: &mut DocAnalyzer, readonly: LuaDocTagReadonly) -> Option<()> {
172148
let owner_id = get_owner_id_or_report(analyzer, &readonly)?;
173149

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use super::{
1616
diagnostic_tags::analyze_diagnostic,
1717
field_or_operator_def_tags::{analyze_field, analyze_operator},
1818
property_tags::{
19-
analyze_async, analyze_deprecated, analyze_export, analyze_nodiscard, analyze_source,
20-
analyze_version, analyze_visibility,
19+
analyze_async, analyze_deprecated, analyze_nodiscard, analyze_source, analyze_version,
20+
analyze_visibility,
2121
},
2222
type_def_tags::{analyze_alias, analyze_class, analyze_enum, analyze_func_generic},
2323
type_ref_tags::{
@@ -114,9 +114,6 @@ pub fn analyze_tag(analyzer: &mut DocAnalyzer, tag: LuaDocTag) -> Option<()> {
114114
LuaDocTag::Other(other) => {
115115
analyze_other(analyzer, other)?;
116116
}
117-
LuaDocTag::Export(export) => {
118-
analyze_export(analyzer, export)?;
119-
}
120117
LuaDocTag::Readonly(readonly) => {
121118
analyze_readonly(analyzer, readonly)?;
122119
}

crates/emmylua_code_analysis/src/compilation/analyzer/lua/module.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,23 @@ pub fn analyze_chunk_return(analyzer: &mut LuaAnalyzer, chunk: LuaChunk) -> Opti
2727

2828
let semantic_id = get_semantic_id(analyzer, expr.clone());
2929

30+
let visibility = semantic_id.as_ref().and_then(|id| {
31+
analyzer
32+
.db
33+
.get_property_index()
34+
.get_property(id)
35+
.map(|p| p.visibility.clone())
36+
});
37+
3038
let module_info = analyzer
3139
.db
3240
.get_module_index_mut()
3341
.get_module_mut(analyzer.file_id)?;
3442
module_info.export_type = Some(expr_type.get_result_slot_type(0).unwrap_or(expr_type));
3543
module_info.semantic_id = semantic_id;
44+
if let Some(visibility) = visibility {
45+
module_info.merge_visibility(visibility);
46+
}
3647
break;
3748
}
3849
}

crates/emmylua_code_analysis/src/compilation/analyzer/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ mod lua;
77
mod unresolve;
88

99
use crate::{
10-
Emmyrc, FileId, InFiled, InferFailReason, WorkspaceId, db_index::DbIndex, profile::Profile,
10+
Emmyrc, FileId, InFiled, InferFailReason,
11+
db_index::{DbIndex, WorkspaceId},
12+
profile::Profile,
1113
};
1214
use emmylua_parser::LuaChunk;
1315
use hashbrown::{HashMap, HashSet};

crates/emmylua_code_analysis/src/compilation/test/export_test.rs

Lines changed: 0 additions & 26 deletions
This file was deleted.

crates/emmylua_code_analysis/src/compilation/test/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ mod closure_param_infer_test;
88
mod closure_return_test;
99
mod decl_test;
1010
mod diagnostic_disable_test;
11-
mod export_test;
1211
mod flow;
1312
mod for_range_var_infer_test;
1413
mod generic_infer_test;
@@ -18,7 +17,7 @@ mod inherit_type;
1817
mod mathlib_test;
1918
mod member_infer_test;
2019
mod metatable_test;
21-
mod module_annotation;
20+
mod module_test;
2221
mod multi_return;
2322
mod out_of_order;
2423
mod overload_field;

0 commit comments

Comments
 (0)