Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 12 additions & 1 deletion src/formatters/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions tests/inputs-luau/assignment-comments-1.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local Dictionary: {[string]: number} -- comment
= {
["A"] = 1,
["B"] = 2,
["C"] = c,
}
11 changes: 11 additions & 0 deletions tests/snapshots/tests__luau@assignment-comments-1.lua.snap
Original file line number Diff line number Diff line change
@@ -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,
}
Loading