diff --git a/CHANGELOG.md b/CHANGELOG.md index 126be3d6..fe1d053b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - 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)) +- 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)) ## [2.1.0] - 2025-04-21 diff --git a/src/formatters/functions.rs b/src/formatters/functions.rs index cb5393a2..3459b319 100644 --- a/src/formatters/functions.rs +++ b/src/formatters/functions.rs @@ -147,11 +147,13 @@ fn function_args_contains_comments( true } else { arguments.pairs().any(|argument| { - // Leading / Trailing trivia of expression (ignore inline comments) + // Leading / Trailing trivia of expression argument.value().leading_trivia() .iter() .chain(argument.value().trailing_trivia().iter()) .any(function_trivia_contains_comments) + // Inline comments that will force hanging + || argument.value().has_inline_comments() // Punctuation contains comments || argument .punctuation() diff --git a/tests/inputs/hang-binop-comments-3.lua b/tests/inputs/hang-binop-comments-3.lua new file mode 100644 index 00000000..b91dce49 --- /dev/null +++ b/tests/inputs/hang-binop-comments-3.lua @@ -0,0 +1,10 @@ +-- https://github.com/JohnnyMorganz/StyLua/issues/996 + +local function f() + local a = "ab" + a = a:gsub("a".. -- " + 'b', function() return "c" end) + print(a) +end + +f() diff --git a/tests/snapshots/tests__standard@hang-binop-comments-3.lua.snap b/tests/snapshots/tests__standard@hang-binop-comments-3.lua.snap new file mode 100644 index 00000000..274ebb5e --- /dev/null +++ b/tests/snapshots/tests__standard@hang-binop-comments-3.lua.snap @@ -0,0 +1,20 @@ +--- +source: tests/tests.rs +expression: "format(&contents, LuaVersion::Lua51)" +input_file: tests/inputs/hang-binop-comments-3.lua +--- +-- https://github.com/JohnnyMorganz/StyLua/issues/996 + +local function f() + local a = "ab" + a = a:gsub( + "a" -- " + .. "b", + function() + return "c" + end + ) + print(a) +end + +f()