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 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() 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, +} 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, +}