Skip to content

Commit 8314c09

Browse files
Check for inline comments that will force hanging in function arguments (#1025)
* Add test case * Check for inline comments that will force hanging in function arguments * Update snapshots * Update changelog * Fix has_inline_comments to not recurse into nested function bodies The previous implementation used contains_comments(lhs) which checked all tokens in the entire LHS tree, incorrectly detecting comments inside nested function bodies as "inline" comments of the outer binary expression. Now we only check for comments that are actually inline within the binary expression: trailing comments on LHS, leading comments on RHS, and recursively check nested compound expressions. * Only match singleline comments
1 parent 4c2d066 commit 8314c09

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616

1717
- Fixed syntax error in output when a single-line comment appears between an index suffix and a table call argument, e.g. `foo.bar -- comment { }` ([#873](https://github.com/JohnnyMorganz/StyLua/issues/873))
1818

19+
### Fixed
20+
21+
- Fixed malformed formatting when a binary expression inside of a function call with comments around the operators is incorrectly collapsed onto one line ([#996](https://github.com/JohnnyMorganz/StyLua/issues/996))
22+
1923
## [2.3.1] - 2025-11-01
2024

2125
### Fixed

src/formatters/functions.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,13 @@ fn function_args_contains_comments(
147147
true
148148
} else {
149149
arguments.pairs().any(|argument| {
150-
// Leading / Trailing trivia of expression (ignore inline comments)
150+
// Leading / Trailing trivia of expression
151151
argument.value().leading_trivia()
152152
.iter()
153153
.chain(argument.value().trailing_trivia().iter())
154154
.any(function_trivia_contains_comments)
155+
// Inline comments that will force hanging
156+
|| argument.value().has_inline_comments()
155157
// Punctuation contains comments
156158
|| argument
157159
.punctuation()

src/formatters/trivia_util.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,11 @@ impl HasInlineComments for Expression {
11721172
fn has_inline_comments(&self) -> bool {
11731173
match self {
11741174
Expression::BinaryOperator { lhs, binop, rhs } => {
1175-
contains_comments(binop) || contains_comments(lhs) || rhs.has_inline_comments()
1175+
contains_comments(binop)
1176+
|| lhs.has_trailing_comments(CommentSearch::Single)
1177+
|| rhs.has_leading_comments(CommentSearch::Single)
1178+
|| lhs.has_inline_comments()
1179+
|| rhs.has_inline_comments()
11761180
}
11771181
Expression::UnaryOperator { unop, expression } => {
11781182
let op_contains_comments = match unop {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- https://github.com/JohnnyMorganz/StyLua/issues/996
2+
3+
local function f()
4+
local a = "ab"
5+
a = a:gsub("a".. -- "
6+
'b', function() return "c" end)
7+
print(a)
8+
end
9+
10+
f()

tests/snapshots/[email protected]

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
source: tests/tests.rs
3+
expression: "format(&contents, LuaVersion::Lua51)"
4+
input_file: tests/inputs/hang-binop-comments-3.lua
5+
---
6+
-- https://github.com/JohnnyMorganz/StyLua/issues/996
7+
8+
local function f()
9+
local a = "ab"
10+
a = a:gsub(
11+
"a" -- "
12+
.. "b",
13+
function()
14+
return "c"
15+
end
16+
)
17+
print(a)
18+
end
19+
20+
f()

0 commit comments

Comments
 (0)