Skip to content

Commit ba53bc1

Browse files
authored
Merge pull request #1004 from xuhuanzy/update
refactor(visibility): improve type visibility
2 parents bf9e36e + 0e23dac commit ba53bc1

83 files changed

Lines changed: 2403 additions & 946 deletions

File tree

Some content is hidden

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

crates/emmylua_check/src/init.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use emmylua_code_analysis::{
2-
EmmyLuaAnalysis, WorkspaceFolder, collect_workspace_files, load_configs, update_code_style,
2+
EmmyLuaAnalysis, WorkspaceFolder, build_workspace_folders, collect_workspace_files,
3+
load_configs, update_code_style,
34
};
45
use fern::Dispatch;
56
use log::LevelFilter;
@@ -90,26 +91,21 @@ pub async fn load_workspace(
9091
);
9192
emmyrc.pre_process_emmyrc(&config_root);
9293

93-
let mut workspace_folders = cmd_workspace_folders
94+
let workspace_folders = cmd_workspace_folders
9495
.iter()
9596
.map(|path| WorkspaceFolder::new(path.clone(), false))
9697
.collect::<Vec<WorkspaceFolder>>();
9798
let mut analysis = EmmyLuaAnalysis::new();
9899
analysis.update_config(emmyrc.clone().into());
99100
analysis.init_std_lib(None);
100101

101-
for lib in &emmyrc.workspace.library {
102-
let path = PathBuf::from(lib.get_path().clone());
103-
analysis.add_library_workspace(path.clone());
104-
workspace_folders.push(WorkspaceFolder::new(path.clone(), true));
105-
}
106-
107-
for path in &workspace_folders {
108-
analysis.add_main_workspace(path.root.clone());
109-
}
110-
111-
for root in &emmyrc.workspace.workspace_roots {
112-
analysis.add_main_workspace(PathBuf::from(root));
102+
let workspace_folders = build_workspace_folders(&workspace_folders, &emmyrc);
103+
for workspace in &workspace_folders {
104+
if workspace.is_library {
105+
analysis.add_library_workspace(workspace);
106+
} else {
107+
analysis.add_main_workspace(workspace.root.clone());
108+
}
113109
}
114110

115111
let file_infos = collect_workspace_files(&workspace_folders, &analysis.emmyrc, None, ignore);

crates/emmylua_code_analysis/locales/lint.yml

Lines changed: 9 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
@@ -273,3 +273,8 @@ Cannot use `...` outside a vararg function.:
273273
en: "Value '%{value}' does not match any enum value. Expected one of: %{enum_values}"
274274
zh_CN: "值 '%{value}' 与任何枚举值都不匹配。应为以下之一: %{enum_values}"
275275
zh_HK: "值 '%{value}' 與任何枚舉值都不匹配。應為以下之一: %{enum_values}"
276+
277+
"Type '%{name}' has inconsistent access modifiers: %{modifiers}.":
278+
en: "Type '%{name}' has inconsistent access modifiers: %{modifiers}."
279+
zh_CN: "类型 '%{name}' 具有不一致的访问修饰符: %{modifiers}."
280+
zh_HK: "類型 '%{name}' 具有不一致的訪問修飾符: %{modifiers}."

crates/emmylua_code_analysis/resources/schema.json

Lines changed: 50 additions & 51 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
}
@@ -152,7 +151,7 @@
152151
"ignoreGlobs": [],
153152
"library": [],
154153
"moduleMap": [],
155-
"packageDirs": [],
154+
"packages": [],
156155
"preloadFileSize": 0,
157156
"reindexDuration": 5000,
158157
"workspaceRoots": []
@@ -442,6 +441,11 @@
442441
"description": "Call to a non-callable value",
443442
"type": "string",
444443
"const": "call-non-callable"
444+
},
445+
{
446+
"description": "inconsistent-type-access-modifier",
447+
"type": "string",
448+
"const": "inconsistent-type-access-modifier"
445449
}
446450
]
447451
},
@@ -478,46 +482,6 @@
478482
"rst"
479483
]
480484
},
481-
"EmmyLibraryConfig": {
482-
"type": "object",
483-
"properties": {
484-
"ignoreDir": {
485-
"description": "Ignore directories within this library",
486-
"type": "array",
487-
"default": [],
488-
"items": {
489-
"type": "string"
490-
}
491-
},
492-
"ignoreGlobs": {
493-
"description": "Ignore globs within this library. eg: [\"**/*.lua\"]",
494-
"type": "array",
495-
"default": [],
496-
"items": {
497-
"type": "string"
498-
}
499-
},
500-
"path": {
501-
"description": "Library path",
502-
"type": "string"
503-
}
504-
},
505-
"required": [
506-
"path"
507-
]
508-
},
509-
"EmmyLibraryItem": {
510-
"anyOf": [
511-
{
512-
"description": "Simple library path string",
513-
"type": "string"
514-
},
515-
{
516-
"description": "Library configuration with path and ignore rules",
517-
"$ref": "#/$defs/EmmyLibraryConfig"
518-
}
519-
]
520-
},
521485
"EmmyrcCodeAction": {
522486
"type": "object",
523487
"properties": {
@@ -1100,11 +1064,6 @@
11001064
"type": "boolean",
11011065
"default": true
11021066
},
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-
},
11081067
"requirePath": {
11091068
"description": "Whether to enable strict mode require path.",
11101069
"type": "boolean",
@@ -1151,7 +1110,7 @@
11511110
"type": "array",
11521111
"default": [],
11531112
"items": {
1154-
"$ref": "#/$defs/EmmyLibraryItem"
1113+
"$ref": "#/$defs/EmmyrcWorkspacePathItem"
11551114
}
11561115
},
11571116
"moduleMap": {
@@ -1162,12 +1121,12 @@
11621121
"$ref": "#/$defs/EmmyrcWorkspaceModuleMap"
11631122
}
11641123
},
1165-
"packageDirs": {
1166-
"description": "Package directories. Treat the parent directory as a `library`, but only add files from the specified directory.\neg: `/usr/local/share/lua/5.1/module`",
1124+
"packages": {
1125+
"description": "Package directories. Can be a string path or an object with path and ignore rules.\nTreat the parent directory as a `library`, but only add files from the specified directory.\neg: [\"/usr/local/share/lua/5.1/module\"] or [{\"path\": \"/usr/local/share/lua/5.1/module\", \"ignoreDir\": [\"test\"], \"ignoreGlobs\": [\"**/*.spec.lua\"]}]",
11671126
"type": "array",
11681127
"default": [],
11691128
"items": {
1170-
"type": "string"
1129+
"$ref": "#/$defs/EmmyrcWorkspacePathItem"
11711130
}
11721131
},
11731132
"preloadFileSize": {
@@ -1207,6 +1166,46 @@
12071166
"pattern",
12081167
"replace"
12091168
]
1169+
},
1170+
"EmmyrcWorkspacePathConfig": {
1171+
"type": "object",
1172+
"properties": {
1173+
"ignoreDir": {
1174+
"description": "Ignore directories within this entry",
1175+
"type": "array",
1176+
"default": [],
1177+
"items": {
1178+
"type": "string"
1179+
}
1180+
},
1181+
"ignoreGlobs": {
1182+
"description": "Ignore globs within this entry. eg: [\"**/*.lua\"]",
1183+
"type": "array",
1184+
"default": [],
1185+
"items": {
1186+
"type": "string"
1187+
}
1188+
},
1189+
"path": {
1190+
"description": "Workspace entry path",
1191+
"type": "string"
1192+
}
1193+
},
1194+
"required": [
1195+
"path"
1196+
]
1197+
},
1198+
"EmmyrcWorkspacePathItem": {
1199+
"anyOf": [
1200+
{
1201+
"description": "Simple workspace entry path string",
1202+
"type": "string"
1203+
},
1204+
{
1205+
"description": "Workspace entry configuration with path and ignore rules",
1206+
"$ref": "#/$defs/EmmyrcWorkspacePathConfig"
1207+
}
1208+
]
12101209
}
12111210
}
12121211
}

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

Lines changed: 19 additions & 5 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;
@@ -192,6 +204,8 @@ fn add_type_decl(
192204
.unwrap_or(basic_name.to_string());
193205
let id = if flag.contains(LuaTypeFlag::Private) {
194206
LuaTypeDeclId::local(file_id, &full_name)
207+
} else if flag.contains(LuaTypeFlag::Internal) {
208+
LuaTypeDeclId::internal(workspace_id, &full_name)
195209
} else {
196210
LuaTypeDeclId::global(&full_name)
197211
};

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@ fn infer_buildin_or_ref_type(
174174
}
175175

176176
let mut founded = false;
177-
let type_id = if let Some(name_type_decl) = analyzer
178-
.db
179-
.get_type_index_mut()
180-
.find_type_decl(analyzer.file_id, name)
181-
{
177+
let type_id = if let Some(name_type_decl) = analyzer.db.get_type_index().find_type_decl(
178+
analyzer.file_id,
179+
name,
180+
Some(analyzer.workspace_id),
181+
) {
182182
founded = true;
183183
name_type_decl.get_id()
184184
} else {
@@ -233,11 +233,11 @@ fn infer_generic_type(analyzer: &mut DocAnalyzer, generic_type: &LuaDocGenericTy
233233
return typ;
234234
}
235235

236-
let id = if let Some(name_type_decl) = analyzer
237-
.db
238-
.get_type_index_mut()
239-
.find_type_decl(analyzer.file_id, &name)
240-
{
236+
let id = if let Some(name_type_decl) = analyzer.db.get_type_index().find_type_decl(
237+
analyzer.file_id,
238+
&name,
239+
Some(analyzer.workspace_id),
240+
) {
241241
name_type_decl.get_id()
242242
} else {
243243
analyzer.db.get_diagnostic_index_mut().add_diagnostic(

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};

0 commit comments

Comments
 (0)