Skip to content

Commit 76faea7

Browse files
committed
fix format error for vararg param
1 parent adb851b commit 76faea7

3 files changed

Lines changed: 34 additions & 14 deletions

File tree

crates/emmylua_formatter/src/formatter/render/comments.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ pub(crate) fn extract_trailing_comment_rendered(
2222
LuaSyntaxKind::LocalStat | LuaSyntaxKind::AssignStat
2323
) && trailing_gap_requests_alignment(
2424
node,
25-
comment.syntax().text_range(),
25+
comment.get_range(),
2626
ctx.config.comments.line_comment_min_spaces_before.max(1),
2727
);
28-
Some((docs, comment.syntax().text_range(), align_hint))
28+
Some((docs, comment.get_range(), align_hint))
2929
}
3030

3131
pub(crate) fn append_trailing_comment_suffix(

crates/emmylua_formatter/src/formatter/render/comments_ast.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use crate::formatter::model::{FormatPlan, TokenSpacingExpected};
33
use crate::ir;
44
use crate::ir::DocIR;
55
use emmylua_parser::{
6-
LuaAstNode, LuaAstToken, LuaComment, LuaDocDescriptionOwner, LuaDocTag, LuaSyntaxId,
7-
LuaSyntaxToken, LuaTokenKind,
6+
LuaAstNode, LuaAstToken, LuaComment, LuaDocDescription, LuaDocDescriptionOwner, LuaDocFieldKey,
7+
LuaDocTag, LuaSyntaxId, LuaSyntaxToken, LuaTokenKind,
88
};
99
use std::collections::HashMap;
1010

@@ -477,10 +477,14 @@ fn extract_columns(
477477
let desc = tag_description(tag);
478478
match tag {
479479
LuaDocTag::Param(t) => {
480-
let name = t
481-
.get_name_token()
482-
.map(|n| n.get_name_text().to_string())
483-
.unwrap_or_default();
480+
let name = if t.is_vararg() {
481+
"...".to_string()
482+
} else if let Some(name_token) = t.get_name_token() {
483+
name_token.get_name_text().to_string()
484+
} else {
485+
"".to_string()
486+
};
487+
484488
let nullable = if t.is_nullable() { "?" } else { "" };
485489
let ty = t
486490
.get_type()
@@ -544,12 +548,12 @@ fn extract_columns(
544548
let key = f
545549
.get_field_key()
546550
.map(|k| match k {
547-
emmylua_parser::LuaDocFieldKey::Name(n) => n.get_name_text().to_string(),
548-
emmylua_parser::LuaDocFieldKey::String(s) => format!("[\"{}\"]", s.get_value()),
549-
emmylua_parser::LuaDocFieldKey::Integer(i) => {
551+
LuaDocFieldKey::Name(n) => n.get_name_text().to_string(),
552+
LuaDocFieldKey::String(s) => format!("[\"{}\"]", s.get_value()),
553+
LuaDocFieldKey::Integer(i) => {
550554
format!("[{}]", i.syntax().text())
551555
}
552-
emmylua_parser::LuaDocFieldKey::Type(ty) => {
556+
LuaDocFieldKey::Type(ty) => {
553557
format!("[{}]", format_node_tokens(plan, ty.syntax()))
554558
}
555559
})
@@ -649,7 +653,7 @@ fn extract_columns(
649653
}
650654

651655
fn tag_description(tag: &LuaDocTag) -> String {
652-
let d: Option<emmylua_parser::LuaDocDescription> = match tag {
656+
let d: Option<LuaDocDescription> = match tag {
653657
LuaDocTag::Class(t) => t.get_description(),
654658
LuaDocTag::Param(t) => t.get_description(),
655659
LuaDocTag::Return(t) => t.get_description(),
@@ -674,7 +678,7 @@ fn comment_desc_for_tag(c: &LuaComment, tag: &LuaDocTag) -> String {
674678
found_tag = true;
675679
continue;
676680
}
677-
if found_tag && let Some(desc) = emmylua_parser::LuaDocDescription::cast(n) {
681+
if found_tag && let Some(desc) = LuaDocDescription::cast(n) {
678682
return desc.syntax().text().to_string().trim().to_string();
679683
}
680684
}

crates/emmylua_formatter/src/test/comment_tests.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2370,6 +2370,22 @@ local a = 1
23702370
r#"---@cast assert [function]
23712371
"#,
23722372
r#"---@cast assert [function]
2373+
"#
2374+
);
2375+
}
2376+
2377+
#[test]
2378+
fn test_param_vararg() {
2379+
assert_format!(
2380+
r#"
2381+
---@param a string
2382+
---@param ... any
2383+
function foo(a, ...) end
2384+
"#,
2385+
r#"
2386+
---@param a string
2387+
---@param ... any
2388+
function foo(a, ...) end
23732389
"#
23742390
);
23752391
}

0 commit comments

Comments
 (0)