Skip to content

Commit 4c50012

Browse files
committed
fix(diagnostic): prevent comments from shifting call argument diagnostics
fix #1158
1 parent bb4f4f0 commit 4c50012

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#[cfg(test)]
22
mod tests {
3+
use lsp_types::NumberOrString;
4+
use tokio_util::sync::CancellationToken;
5+
36
use crate::{DiagnosticCode, VirtualWorkspace};
47

58
#[test]
@@ -355,4 +358,50 @@ foo({})
355358
"#
356359
));
357360
}
361+
362+
#[test]
363+
fn test_call_argument_comment_does_not_shift_missing_fields_range() {
364+
let mut ws = VirtualWorkspace::new();
365+
ws.analysis
366+
.diagnostic
367+
.enable_only(DiagnosticCode::MissingFields);
368+
let file_id = ws.def(
369+
r#"---@class A
370+
---@field a 1
371+
---@class B
372+
---@field b 2
373+
---@class C
374+
---@field c 3
375+
376+
---@param a A
377+
---@param b B
378+
---@param c C
379+
local function test(a, b, c) end
380+
381+
test(
382+
-- What
383+
{},
384+
{},
385+
{}
386+
)"#,
387+
);
388+
let code = Some(NumberOrString::String(
389+
DiagnosticCode::MissingFields.get_name().to_string(),
390+
));
391+
let diagnostics = ws
392+
.analysis
393+
.diagnose_file(file_id, CancellationToken::new())
394+
.unwrap_or_default()
395+
.into_iter()
396+
.filter(|diagnostic| diagnostic.code == code)
397+
.collect::<Vec<_>>();
398+
399+
assert_eq!(diagnostics.len(), 3, "{diagnostics:#?}");
400+
assert_eq!(diagnostics[0].range.start.line, 14, "{diagnostics:#?}");
401+
assert!(diagnostics[0].message.contains("`a`"), "{diagnostics:#?}");
402+
assert_eq!(diagnostics[1].range.start.line, 15, "{diagnostics:#?}");
403+
assert!(diagnostics[1].message.contains("`b`"), "{diagnostics:#?}");
404+
assert_eq!(diagnostics[2].range.start.line, 16, "{diagnostics:#?}");
405+
assert!(diagnostics[2].message.contains("`c`"), "{diagnostics:#?}");
406+
}
358407
}

crates/emmylua_code_analysis/src/semantic/infer/infer_table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ fn infer_table_type_by_callee(
224224
)?;
225225
let param_types = func_type.get_params();
226226
let mut call_arg_number = call_arg_list
227-
.children::<LuaAst>()
227+
.get_args()
228228
.enumerate()
229229
.find(|(_, arg)| arg.get_position() == table_expr.get_position())
230230
.ok_or(InferFailReason::None)?

0 commit comments

Comments
 (0)