Skip to content

Commit feede61

Browse files
authored
Merge pull request #647 from taminomara/fix-tags
Fix doc tag associations, warn about useless tags
2 parents 389c98a + 1991d24 commit feede61

File tree

4 files changed

+95
-36
lines changed

4 files changed

+95
-36
lines changed

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ use crate::{
33
};
44

55
use super::{
6-
tags::{find_owner_closure, get_owner_id},
6+
tags::{find_owner_closure_or_report, get_owner_id_or_report},
77
DocAnalyzer,
88
};
9+
use crate::compilation::analyzer::doc::tags::report_orphan_tag;
910
use emmylua_parser::{
10-
LuaAst, LuaAstNode, LuaDocDescriptionOwner, LuaDocTagDeprecated, LuaDocTagExport,
11-
LuaDocTagNodiscard, LuaDocTagSource, LuaDocTagVersion, LuaDocTagVisibility, LuaTableExpr,
11+
LuaAst, LuaAstNode, LuaDocDescriptionOwner, LuaDocTagAsync, LuaDocTagDeprecated,
12+
LuaDocTagExport, LuaDocTagNodiscard, LuaDocTagSource, LuaDocTagVersion, LuaDocTagVisibility,
13+
LuaTableExpr,
1214
};
1315

1416
pub fn analyze_visibility(
1517
analyzer: &mut DocAnalyzer,
1618
visibility: LuaDocTagVisibility,
1719
) -> Option<()> {
1820
let visibility_kind = visibility.get_visibility_token()?.get_visibility();
19-
let owner_id = get_owner_id(analyzer)?;
21+
let owner_id = get_owner_id_or_report(analyzer, &visibility)?;
2022

2123
analyzer.db.get_property_index_mut().add_visibility(
2224
analyzer.file_id,
@@ -28,19 +30,19 @@ pub fn analyze_visibility(
2830
}
2931

3032
pub fn analyze_source(analyzer: &mut DocAnalyzer, source: LuaDocTagSource) -> Option<()> {
31-
let source = source.get_path_token()?.get_path().to_string();
32-
let owner_id = get_owner_id(analyzer)?;
33+
let path = source.get_path_token()?.get_path().to_string();
34+
let owner_id = get_owner_id_or_report(analyzer, &source)?;
3335

3436
analyzer
3537
.db
3638
.get_property_index_mut()
37-
.add_source(analyzer.file_id, owner_id, source);
39+
.add_source(analyzer.file_id, owner_id, path);
3840

3941
Some(())
4042
}
4143

4244
pub fn analyze_nodiscard(analyzer: &mut DocAnalyzer, nodiscard: LuaDocTagNodiscard) -> Option<()> {
43-
let closure = find_owner_closure(analyzer)?;
45+
let closure = find_owner_closure_or_report(analyzer, &nodiscard)?;
4446
let signature_id = LuaSignatureId::from_closure(analyzer.file_id, &closure);
4547
let signature = analyzer
4648
.db
@@ -72,7 +74,7 @@ pub fn analyze_deprecated(analyzer: &mut DocAnalyzer, tag: LuaDocTagDeprecated)
7274
} else {
7375
None
7476
};
75-
let owner_id = get_owner_id(analyzer)?;
77+
let owner_id = get_owner_id_or_report(analyzer, &tag)?;
7678

7779
analyzer
7880
.db
@@ -83,7 +85,7 @@ pub fn analyze_deprecated(analyzer: &mut DocAnalyzer, tag: LuaDocTagDeprecated)
8385
}
8486

8587
pub fn analyze_version(analyzer: &mut DocAnalyzer, version: LuaDocTagVersion) -> Option<()> {
86-
let owner_id = get_owner_id(analyzer)?;
88+
let owner_id = get_owner_id_or_report(analyzer, &version)?;
8789

8890
let mut version_set = Vec::new();
8991
for version in version.get_version_list() {
@@ -100,8 +102,8 @@ pub fn analyze_version(analyzer: &mut DocAnalyzer, version: LuaDocTagVersion) ->
100102
Some(())
101103
}
102104

103-
pub fn analyze_async(analyzer: &mut DocAnalyzer) -> Option<()> {
104-
let closure = find_owner_closure(analyzer)?;
105+
pub fn analyze_async(analyzer: &mut DocAnalyzer, tag: LuaDocTagAsync) -> Option<()> {
106+
let closure = find_owner_closure_or_report(analyzer, &tag)?;
105107
let signature_id = LuaSignatureId::from_closure(analyzer.file_id, &closure);
106108
let signature = analyzer
107109
.db
@@ -114,7 +116,10 @@ pub fn analyze_async(analyzer: &mut DocAnalyzer) -> Option<()> {
114116
}
115117

116118
pub fn analyze_export(analyzer: &mut DocAnalyzer, tag: LuaDocTagExport) -> Option<()> {
117-
let owner = analyzer.comment.get_owner()?;
119+
let Some(owner) = analyzer.comment.get_owner() else {
120+
report_orphan_tag(analyzer, &tag);
121+
return None;
122+
};
118123
let owner_id = match owner {
119124
LuaAst::LuaReturnStat(return_stat) => {
120125
let return_table_expr = return_stat.child::<LuaTableExpr>()?;
@@ -123,7 +128,7 @@ pub fn analyze_export(analyzer: &mut DocAnalyzer, tag: LuaDocTagExport) -> Optio
123128
return_table_expr.get_position(),
124129
))
125130
}
126-
_ => get_owner_id(analyzer)?,
131+
_ => get_owner_id_or_report(analyzer, &tag)?,
127132
};
128133

129134
let export_scope = if let Some(scope_text) = tag.get_export_scope() {

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

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use emmylua_parser::{
44

55
use crate::{
66
db_index::{LuaMemberId, LuaSemanticDeclId, LuaSignatureId},
7-
LuaDeclId,
7+
AnalyzeError, DiagnosticCode, LuaDeclId,
88
};
99

1010
use super::{
@@ -74,8 +74,8 @@ pub fn analyze_tag(analyzer: &mut DocAnalyzer, tag: LuaDocTag) -> Option<()> {
7474
LuaDocTag::Version(version) => {
7575
analyze_version(analyzer, version)?;
7676
}
77-
LuaDocTag::Async(_) => {
78-
analyze_async(analyzer)?;
77+
LuaDocTag::Async(tag) => {
78+
analyze_async(analyzer, tag)?;
7979
}
8080

8181
// field or operator
@@ -135,7 +135,23 @@ pub fn find_owner_closure(analyzer: &DocAnalyzer) -> Option<LuaClosureExpr> {
135135
None
136136
}
137137

138+
pub fn find_owner_closure_or_report(
139+
analyzer: &mut DocAnalyzer,
140+
tag: &impl LuaAstNode,
141+
) -> Option<LuaClosureExpr> {
142+
match find_owner_closure(analyzer) {
143+
Some(id) => Some(id),
144+
None => {
145+
report_orphan_tag(analyzer, tag);
146+
None
147+
}
148+
}
149+
}
150+
138151
pub fn get_owner_id(analyzer: &mut DocAnalyzer) -> Option<LuaSemanticDeclId> {
152+
if let Some(current_type_id) = &analyzer.current_type_id {
153+
return Some(LuaSemanticDeclId::TypeDecl(current_type_id.clone()));
154+
}
139155
let owner = analyzer.comment.get_owner()?;
140156
match owner {
141157
LuaAst::LuaAssignStat(assign) => {
@@ -186,3 +202,27 @@ pub fn get_owner_id(analyzer: &mut DocAnalyzer) -> Option<LuaSemanticDeclId> {
186202
}
187203
}
188204
}
205+
206+
pub fn get_owner_id_or_report(
207+
analyzer: &mut DocAnalyzer,
208+
tag: &impl LuaAstNode,
209+
) -> Option<LuaSemanticDeclId> {
210+
match get_owner_id(analyzer) {
211+
Some(id) => Some(id),
212+
None => {
213+
report_orphan_tag(analyzer, tag);
214+
None
215+
}
216+
}
217+
}
218+
219+
pub fn report_orphan_tag(analyzer: &mut DocAnalyzer, tag: &impl LuaAstNode) {
220+
analyzer.db.get_diagnostic_index_mut().add_diagnostic(
221+
analyzer.file_id,
222+
AnalyzeError {
223+
kind: DiagnosticCode::AnnotationUsageError,
224+
message: t!("`@%{name}` can't be used here", name = tag.get_text()).to_string(),
225+
range: tag.get_range(),
226+
},
227+
);
228+
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ use emmylua_parser::{
88
};
99
use rowan::TextRange;
1010

11+
use super::{
12+
infer_type::infer_type, preprocess_description, tags::find_owner_closure, DocAnalyzer,
13+
};
14+
use crate::compilation::analyzer::doc::tags::report_orphan_tag;
1115
use crate::{
1216
compilation::analyzer::bind_type::bind_type,
1317
db_index::{LuaDeclId, LuaMemberId, LuaSemanticDeclId, LuaSignatureId, LuaType},
1418
LuaTypeCache, LuaTypeDeclId,
1519
};
1620

17-
use super::{
18-
infer_type::infer_type, preprocess_description, tags::find_owner_closure, DocAnalyzer,
19-
};
20-
2121
pub fn analyze_class(analyzer: &mut DocAnalyzer, tag: LuaDocTagClass) -> Option<()> {
2222
let file_id = analyzer.file_id;
2323
let name = tag.get_name_token()?.get_name_text().to_string();
@@ -324,7 +324,10 @@ fn get_global_reference_ranges(
324324
}
325325

326326
pub fn analyze_func_generic(analyzer: &mut DocAnalyzer, tag: LuaDocTagGeneric) -> Option<()> {
327-
let comment_owner = analyzer.comment.get_owner()?;
327+
let Some(comment_owner) = analyzer.comment.get_owner() else {
328+
report_orphan_tag(analyzer, &tag);
329+
return None;
330+
};
328331
let mut params_result = HashMap::new();
329332
let mut param_info = Vec::new();
330333
if let Some(params_list) = tag.get_generic_decl_list() {

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

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ use emmylua_parser::{
55
LuaVarExpr,
66
};
77

8+
use super::{
9+
infer_type::infer_type,
10+
preprocess_description,
11+
tags::{find_owner_closure, get_owner_id_or_report},
12+
DocAnalyzer,
13+
};
14+
use crate::compilation::analyzer::doc::tags::{
15+
find_owner_closure_or_report, get_owner_id, report_orphan_tag,
16+
};
817
use crate::{
918
compilation::analyzer::{bind_type::bind_type, unresolve::UnResolveModuleRef},
1019
db_index::{
@@ -15,13 +24,6 @@ use crate::{
1524
SignatureReturnStatus, TypeOps,
1625
};
1726

18-
use super::{
19-
infer_type::infer_type,
20-
preprocess_description,
21-
tags::{find_owner_closure, get_owner_id},
22-
DocAnalyzer,
23-
};
24-
2527
pub fn analyze_type(analyzer: &mut DocAnalyzer, tag: LuaDocTagType) -> Option<()> {
2628
let description = if let Some(des) = tag.get_description() {
2729
Some(preprocess_description(&des.get_description_text(), None))
@@ -36,7 +38,10 @@ pub fn analyze_type(analyzer: &mut DocAnalyzer, tag: LuaDocTagType) -> Option<()
3638
}
3739

3840
// bind ref type
39-
let owner = analyzer.comment.get_owner()?;
41+
let Some(owner) = analyzer.comment.get_owner() else {
42+
report_orphan_tag(analyzer, &tag);
43+
return None;
44+
};
4045
match owner {
4146
LuaAst::LuaAssignStat(assign_stat) => {
4247
let (vars, _) = assign_stat.get_var_and_expr_list();
@@ -137,7 +142,9 @@ pub fn analyze_type(analyzer: &mut DocAnalyzer, tag: LuaDocTagType) -> Option<()
137142
}
138143
}
139144
}
140-
_ => {}
145+
_ => {
146+
report_orphan_tag(analyzer, &tag);
147+
}
141148
}
142149

143150
Some(())
@@ -196,6 +203,8 @@ pub fn analyze_param(analyzer: &mut DocAnalyzer, tag: LuaDocTagParam) -> Option<
196203
break;
197204
}
198205
}
206+
} else {
207+
report_orphan_tag(analyzer, &tag);
199208
}
200209

201210
Some(())
@@ -208,7 +217,7 @@ pub fn analyze_return(analyzer: &mut DocAnalyzer, tag: LuaDocTagReturn) -> Optio
208217
None
209218
};
210219

211-
if let Some(closure) = find_owner_closure(analyzer) {
220+
if let Some(closure) = find_owner_closure_or_report(analyzer, &tag) {
212221
let signature_id = LuaSignatureId::from_closure(analyzer.file_id, &closure);
213222
let returns = tag.get_type_and_name_list();
214223
for (doc_type, name_token) in returns {
@@ -254,6 +263,8 @@ pub fn analyze_return_cast(analyzer: &mut DocAnalyzer, tag: LuaDocTagReturnCast)
254263
name.to_string(),
255264
cast_op_type.to_ptr(),
256265
);
266+
} else {
267+
report_orphan_tag(analyzer, &tag);
257268
}
258269

259270
Some(())
@@ -275,7 +286,7 @@ pub fn analyze_overload(analyzer: &mut DocAnalyzer, tag: LuaDocTagOverload) -> O
275286
}
276287
_ => {}
277288
}
278-
} else if let Some(closure) = find_owner_closure(analyzer) {
289+
} else if let Some(closure) = find_owner_closure_or_report(analyzer, &tag) {
279290
let type_ref = infer_type(analyzer, tag.get_type()?);
280291
match type_ref {
281292
LuaType::DocFunction(func) => {
@@ -294,7 +305,7 @@ pub fn analyze_module(analyzer: &mut DocAnalyzer, tag: LuaDocTagModule) -> Optio
294305
let module_info = analyzer.db.get_module_index().find_module(&module_path)?;
295306
let export_type = module_info.export_type.clone();
296307
let module_file_id = module_info.file_id;
297-
let owner_id = get_owner_id(analyzer)?;
308+
let owner_id = get_owner_id_or_report(analyzer, &tag)?;
298309
if let Some(export_type) = export_type {
299310
match &owner_id {
300311
LuaSemanticDeclId::LuaDecl(decl_id) => {
@@ -372,7 +383,7 @@ pub fn analyze_cast(analyzer: &mut DocAnalyzer, tag: LuaDocTagCast) -> Option<()
372383
}
373384

374385
pub fn analyze_see(analyzer: &mut DocAnalyzer, tag: LuaDocTagSee) -> Option<()> {
375-
let owner = get_owner_id(analyzer)?;
386+
let owner = get_owner_id_or_report(analyzer, &tag)?;
376387
let content = tag.get_see_content()?;
377388
let text = content.get_text();
378389

@@ -385,7 +396,7 @@ pub fn analyze_see(analyzer: &mut DocAnalyzer, tag: LuaDocTagSee) -> Option<()>
385396
}
386397

387398
pub fn analyze_other(analyzer: &mut DocAnalyzer, other: LuaDocTagOther) -> Option<()> {
388-
let owner = get_owner_id(analyzer)?;
399+
let owner = get_owner_id_or_report(analyzer, &other)?;
389400
let tag_name = other.get_tag_name()?;
390401
let description = if let Some(des) = other.get_description() {
391402
let description = preprocess_description(&des.get_description_text(), None);

0 commit comments

Comments
 (0)