From 2bcb0417778c673af5c7ac0e030e189e9dd7a81c Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 13 Sep 2025 14:34:05 +0200 Subject: [PATCH 1/4] Add test case --- tests/inputs-luau/assignment-comments-1.lua | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 tests/inputs-luau/assignment-comments-1.lua diff --git a/tests/inputs-luau/assignment-comments-1.lua b/tests/inputs-luau/assignment-comments-1.lua new file mode 100644 index 00000000..39cbc2b1 --- /dev/null +++ b/tests/inputs-luau/assignment-comments-1.lua @@ -0,0 +1,6 @@ +local Dictionary: {[string]: number} -- comment + = { + ["A"] = 1, + ["B"] = 2, + ["C"] = c, +} From 45217908bf156426f6f9477d9c17d26aad93f5b6 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 13 Sep 2025 14:34:16 +0200 Subject: [PATCH 2/4] Handle comments after a type specifier in a local assignment --- src/formatters/assignment.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/formatters/assignment.rs b/src/formatters/assignment.rs index d0596cdd..8a68b5d6 100644 --- a/src/formatters/assignment.rs +++ b/src/formatters/assignment.rs @@ -507,8 +507,19 @@ pub fn format_local_assignment_no_trivia( }); } + #[cfg(feature = "luau")] + let var_list_ends_with_comments = match type_specifiers.last() { + Some(Some(specifier)) => { + specifier.has_trailing_comments(trivia_util::CommentSearch::Single) + } + _ => name_list.has_trailing_comments(trivia_util::CommentSearch::Single), + }; + #[cfg(not(feature = "luau"))] + let var_list_ends_with_comments = + name_list.has_trailing_comments(trivia_util::CommentSearch::Single); + // If the var list ended with a comment, we need to hang the equals token - if name_list.has_trailing_comments(trivia_util::CommentSearch::Single) { + if var_list_ends_with_comments { const EQUAL_TOKEN_LEN: usize = "= ".len(); shape = shape .reset() From 73a3a90937d5a3f6a6f190a903248e944781aa23 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 13 Sep 2025 14:34:20 +0200 Subject: [PATCH 3/4] Update snapshots --- .../tests__luau@assignment-comments-1.lua.snap | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 tests/snapshots/tests__luau@assignment-comments-1.lua.snap diff --git a/tests/snapshots/tests__luau@assignment-comments-1.lua.snap b/tests/snapshots/tests__luau@assignment-comments-1.lua.snap new file mode 100644 index 00000000..740b0bec --- /dev/null +++ b/tests/snapshots/tests__luau@assignment-comments-1.lua.snap @@ -0,0 +1,11 @@ +--- +source: tests/tests.rs +expression: "format(&contents, LuaVersion::Luau)" +input_file: tests/inputs-luau/assignment-comments-1.lua +--- +local Dictionary: { [string]: number } -- comment + = { + ["A"] = 1, + ["B"] = 2, + ["C"] = c, +} From 3acd6517a9d3c99cd7c76a700944b0279957cc2d Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 13 Sep 2025 14:34:59 +0200 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cb06046..126be3d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed formatting of index containing brackets string in parentheses ([#992](https://github.com/JohnnyMorganz/StyLua/pull/992)) - Fixed goto not being recognised for LuaJIT ([#986](https://github.com/JohnnyMorganz/StyLua/issues/986)) - Fixed semicolon removed after a statement ending with an if-expression leading to ambiguous syntax when the next line begins with parentheses ([#1010](https://github.com/JohnnyMorganz/StyLua/issues/1010)) +- Luau: Fixed malformed formatting when there is a comment after a type specifier in a local assignment ([#995](https://github.com/JohnnyMorganz/StyLua/issues/995)) ## [2.1.0] - 2025-04-21