Skip to content

Commit fa747d5

Browse files
committed
Re-enable @export check
1 parent 9d67d46 commit fa747d5

File tree

3 files changed

+75
-239
lines changed

3 files changed

+75
-239
lines changed

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use emmylua_parser::{
88

99
use crate::{
1010
DiagnosticCode, InferFailReason, LuaMemberKey, LuaSemanticDeclId, LuaType, ModuleInfo,
11-
SemanticModel, enum_variable_is_param, parse_require_module_info,
11+
SemanticDeclLevel, SemanticModel, enum_variable_is_param, parse_require_module_info,
1212
};
1313

1414
use super::{Checker, DiagnosticContext, humanize_lint_type};
@@ -64,19 +64,18 @@ fn check_index_expr(
6464
let prefix_typ = semantic_model
6565
.infer_expr(index_expr.get_prefix_expr()?)
6666
.unwrap_or(LuaType::Unknown);
67-
let module_info = None;
67+
let mut module_info = None;
6868

6969
if is_invalid_prefix_type(&prefix_typ) {
70-
// if matches!(prefix_typ, LuaType::TableConst(_)) {
71-
// // 如果导入了被 @export 标记的表常量, 那么不应该跳过检查
72-
// module_info = check_require_table_const_with_export(semantic_model, index_expr);
73-
// if module_info.is_none() {
74-
// return Some(());
75-
// }
76-
// } else {
77-
// return Some(());
78-
// }
79-
return Some(());
70+
if matches!(prefix_typ, LuaType::TableConst(_)) {
71+
// 如果导入了被 @export 标记的表常量, 那么不应该跳过检查
72+
module_info = check_require_table_const_with_export(semantic_model, index_expr);
73+
if module_info.is_none() {
74+
return Some(());
75+
}
76+
} else {
77+
return Some(());
78+
}
8079
}
8180

8281
let index_key = index_expr.get_index_key()?;
@@ -480,7 +479,6 @@ fn check_enum_is_param(
480479
)
481480
}
482481

483-
#[allow(unused)]
484482
/// 检查导入的表常量
485483
fn check_require_table_const_with_export<'a>(
486484
semantic_model: &'a SemanticModel,
@@ -495,10 +493,12 @@ fn check_require_table_const_with_export<'a>(
495493
}
496494
}
497495

498-
let semantic_info = semantic_model.get_semantic_info(prefix_expr.syntax().clone().into())?;
499-
496+
let semantic_decl_id = semantic_model.find_decl(
497+
prefix_expr.syntax().clone().into(),
498+
SemanticDeclLevel::NoTrace,
499+
)?;
500500
// 检查是否是声明引用
501-
let decl_id = match semantic_info.semantic_decl? {
501+
let decl_id = match semantic_decl_id {
502502
LuaSemanticDeclId::LuaDecl(decl_id) => decl_id,
503503
_ => return None,
504504
};
@@ -509,14 +509,13 @@ fn check_require_table_const_with_export<'a>(
509509
.get_decl_index()
510510
.get_decl(&decl_id)?;
511511

512-
let module_info = parse_require_module_info(semantic_model, decl)?;
512+
let module_info = parse_require_module_info(semantic_model, &decl)?;
513513
if module_info.is_export(semantic_model.get_db()) {
514514
return Some(module_info);
515515
}
516516
None
517517
}
518518

519-
#[allow(unused)]
520519
pub fn parse_require_expr_module_info<'a>(
521520
semantic_model: &'a SemanticModel,
522521
call_expr: &LuaCallExpr,

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

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -151,63 +151,63 @@ mod test {
151151
// ));
152152
}
153153

154-
// #[test]
155-
// fn test_export() {
156-
// let mut ws = VirtualWorkspace::new();
157-
// ws.def_file(
158-
// "a.lua",
159-
// r#"
160-
// ---@export
161-
// local export = {}
162-
163-
// export.a = 1
164-
165-
// return export
166-
// "#,
167-
// );
168-
// assert!(!ws.check_code_for(
169-
// DiagnosticCode::InjectField,
170-
// r#"
171-
// local a = require("a")
172-
// a.newField = 1
173-
// "#,
174-
// ));
175-
// assert!(ws.check_code_for(
176-
// DiagnosticCode::InjectField,
177-
// r#"
178-
// local a = require("a")
179-
// a.a = 2
180-
// "#,
181-
// ));
182-
// }
183-
184-
// #[test]
185-
// fn test_export_2() {
186-
// let mut ws = VirtualWorkspace::new();
187-
// ws.def_file(
188-
// "a.lua",
189-
// r#"
190-
// ---@export
191-
// return {
192-
// a = 1
193-
// }
194-
// "#,
195-
// );
196-
// assert!(!ws.check_code_for(
197-
// DiagnosticCode::InjectField,
198-
// r#"
199-
// local a = require("a")
200-
// a.newField = 1
201-
// "#,
202-
// ));
203-
// assert!(ws.check_code_for(
204-
// DiagnosticCode::InjectField,
205-
// r#"
206-
// local a = require("a")
207-
// a.a = 2
208-
// "#,
209-
// ));
210-
// }
154+
#[test]
155+
fn test_export() {
156+
let mut ws = VirtualWorkspace::new();
157+
ws.def_file(
158+
"a.lua",
159+
r#"
160+
---@export
161+
local export = {}
162+
163+
export.a = 1
164+
165+
return export
166+
"#,
167+
);
168+
assert!(!ws.check_code_for(
169+
DiagnosticCode::InjectField,
170+
r#"
171+
local a = require("a")
172+
a.newField = 1
173+
"#,
174+
));
175+
assert!(ws.check_code_for(
176+
DiagnosticCode::InjectField,
177+
r#"
178+
local a = require("a")
179+
a.a = 2
180+
"#,
181+
));
182+
}
183+
184+
#[test]
185+
fn test_export_2() {
186+
let mut ws = VirtualWorkspace::new();
187+
ws.def_file(
188+
"a.lua",
189+
r#"
190+
---@export
191+
return {
192+
a = 1
193+
}
194+
"#,
195+
);
196+
assert!(!ws.check_code_for(
197+
DiagnosticCode::InjectField,
198+
r#"
199+
local a = require("a")
200+
a.newField = 1
201+
"#,
202+
));
203+
assert!(ws.check_code_for(
204+
DiagnosticCode::InjectField,
205+
r#"
206+
local a = require("a")
207+
a.a = 2
208+
"#,
209+
));
210+
}
211211

212212
#[test]
213213
fn test_issue_660() {
Lines changed: 1 addition & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
#[cfg(test)]
22
mod tests {
3-
use crate::handlers::{
4-
semantic_token::CustomSemanticTokenType,
5-
test_lib::{ProviderVirtualWorkspace, VirtualSemanticToken, check},
6-
};
3+
use crate::handlers::test_lib::ProviderVirtualWorkspace;
74
use googletest::prelude::*;
8-
use lsp_types::{SemanticTokenModifier, SemanticTokenType};
9-
use std::collections::HashSet;
105

116
#[gtest]
127
fn test_1() -> Result<()> {
@@ -17,164 +12,6 @@ mod tests {
1712
---@field get fun(self: self, a: number): Cast1?
1813
"#,
1914
);
20-
21-
check!(ws.check_semantic_token(
22-
r#"
23-
---@type Cast1
24-
local A
25-
26-
local _a = A:get(1) --[[@cast -?]]:get(2)
27-
"#,
28-
vec![
29-
VirtualSemanticToken {
30-
line: 1,
31-
start: 16,
32-
length: 3,
33-
token_type: SemanticTokenType::COMMENT,
34-
token_modifier: HashSet::new(),
35-
},
36-
VirtualSemanticToken {
37-
line: 1,
38-
start: 19,
39-
length: 1,
40-
token_type: SemanticTokenType::KEYWORD,
41-
token_modifier: HashSet::from([SemanticTokenModifier::DOCUMENTATION]),
42-
},
43-
VirtualSemanticToken {
44-
line: 1,
45-
start: 20,
46-
length: 4,
47-
token_type: SemanticTokenType::KEYWORD,
48-
token_modifier: HashSet::from([SemanticTokenModifier::DOCUMENTATION]),
49-
},
50-
VirtualSemanticToken {
51-
line: 1,
52-
start: 25,
53-
length: 5,
54-
token_type: SemanticTokenType::TYPE,
55-
token_modifier: HashSet::new(),
56-
},
57-
VirtualSemanticToken {
58-
line: 2,
59-
start: 22,
60-
length: 1,
61-
token_type: SemanticTokenType::VARIABLE,
62-
token_modifier: HashSet::from([SemanticTokenModifier::READONLY]),
63-
},
64-
VirtualSemanticToken {
65-
line: 4,
66-
start: 22,
67-
length: 2,
68-
token_type: SemanticTokenType::VARIABLE,
69-
token_modifier: HashSet::new(),
70-
},
71-
VirtualSemanticToken {
72-
line: 4,
73-
start: 25,
74-
length: 1,
75-
token_type: SemanticTokenType::OPERATOR,
76-
token_modifier: HashSet::new(),
77-
},
78-
VirtualSemanticToken {
79-
line: 4,
80-
start: 27,
81-
length: 1,
82-
token_type: SemanticTokenType::VARIABLE,
83-
token_modifier: HashSet::from([SemanticTokenModifier::READONLY]),
84-
},
85-
VirtualSemanticToken {
86-
line: 4,
87-
start: 29,
88-
length: 3,
89-
token_type: SemanticTokenType::METHOD,
90-
token_modifier: HashSet::new(),
91-
},
92-
VirtualSemanticToken {
93-
line: 4,
94-
start: 32,
95-
length: 1,
96-
token_type: CustomSemanticTokenType::DELIMITER,
97-
token_modifier: HashSet::new(),
98-
},
99-
VirtualSemanticToken {
100-
line: 4,
101-
start: 33,
102-
length: 1,
103-
token_type: SemanticTokenType::NUMBER,
104-
token_modifier: HashSet::new(),
105-
},
106-
VirtualSemanticToken {
107-
line: 4,
108-
start: 34,
109-
length: 1,
110-
token_type: CustomSemanticTokenType::DELIMITER,
111-
token_modifier: HashSet::new(),
112-
},
113-
VirtualSemanticToken {
114-
line: 4,
115-
start: 36,
116-
length: 4,
117-
token_type: SemanticTokenType::COMMENT,
118-
token_modifier: HashSet::new(),
119-
},
120-
VirtualSemanticToken {
121-
line: 4,
122-
start: 40,
123-
length: 1,
124-
token_type: SemanticTokenType::KEYWORD,
125-
token_modifier: HashSet::from([SemanticTokenModifier::DOCUMENTATION]),
126-
},
127-
VirtualSemanticToken {
128-
line: 4,
129-
start: 41,
130-
length: 4,
131-
token_type: SemanticTokenType::KEYWORD,
132-
token_modifier: HashSet::from([SemanticTokenModifier::DOCUMENTATION]),
133-
},
134-
VirtualSemanticToken {
135-
line: 4,
136-
start: 46,
137-
length: 1,
138-
token_type: SemanticTokenType::OPERATOR,
139-
token_modifier: HashSet::new(),
140-
},
141-
VirtualSemanticToken {
142-
line: 4,
143-
start: 47,
144-
length: 1,
145-
token_type: SemanticTokenType::OPERATOR,
146-
token_modifier: HashSet::from([SemanticTokenModifier::DOCUMENTATION]),
147-
},
148-
VirtualSemanticToken {
149-
line: 4,
150-
start: 51,
151-
length: 3,
152-
token_type: SemanticTokenType::METHOD,
153-
token_modifier: HashSet::new(),
154-
},
155-
VirtualSemanticToken {
156-
line: 4,
157-
start: 54,
158-
length: 1,
159-
token_type: CustomSemanticTokenType::DELIMITER,
160-
token_modifier: HashSet::new(),
161-
},
162-
VirtualSemanticToken {
163-
line: 4,
164-
start: 55,
165-
length: 1,
166-
token_type: SemanticTokenType::NUMBER,
167-
token_modifier: HashSet::new(),
168-
},
169-
VirtualSemanticToken {
170-
line: 4,
171-
start: 56,
172-
length: 1,
173-
token_type: CustomSemanticTokenType::DELIMITER,
174-
token_modifier: HashSet::new(),
175-
},
176-
],
177-
));
17815
Ok(())
17916
}
18017
}

0 commit comments

Comments
 (0)