Skip to content

Commit 2cf31d7

Browse files
Handle comments after type specifiers in local assignments (#1024)
* Add test case * Handle comments after a type specifier in a local assignment * Update snapshots * Update changelog
1 parent 766339a commit 2cf31d7

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- Fixed formatting of index containing brackets string in parentheses ([#992](https://github.com/JohnnyMorganz/StyLua/pull/992))
1717
- Fixed goto not being recognised for LuaJIT ([#986](https://github.com/JohnnyMorganz/StyLua/issues/986))
1818
- 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))
19+
- 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))
1920

2021
## [2.1.0] - 2025-04-21
2122

src/formatters/assignment.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,19 @@ pub fn format_local_assignment_no_trivia(
507507
});
508508
}
509509

510+
#[cfg(feature = "luau")]
511+
let var_list_ends_with_comments = match type_specifiers.last() {
512+
Some(Some(specifier)) => {
513+
specifier.has_trailing_comments(trivia_util::CommentSearch::Single)
514+
}
515+
_ => name_list.has_trailing_comments(trivia_util::CommentSearch::Single),
516+
};
517+
#[cfg(not(feature = "luau"))]
518+
let var_list_ends_with_comments =
519+
name_list.has_trailing_comments(trivia_util::CommentSearch::Single);
520+
510521
// If the var list ended with a comment, we need to hang the equals token
511-
if name_list.has_trailing_comments(trivia_util::CommentSearch::Single) {
522+
if var_list_ends_with_comments {
512523
const EQUAL_TOKEN_LEN: usize = "= ".len();
513524
shape = shape
514525
.reset()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
local Dictionary: {[string]: number} -- comment
2+
= {
3+
["A"] = 1,
4+
["B"] = 2,
5+
["C"] = c,
6+
}

tests/snapshots/[email protected]

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
source: tests/tests.rs
3+
expression: "format(&contents, LuaVersion::Luau)"
4+
input_file: tests/inputs-luau/assignment-comments-1.lua
5+
---
6+
local Dictionary: { [string]: number } -- comment
7+
= {
8+
["A"] = 1,
9+
["B"] = 2,
10+
["C"] = c,
11+
}

0 commit comments

Comments
 (0)