From 5229711cb3d26a1e6b1064011967a7626db959ed Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 20 Aug 2022 16:18:33 +0100 Subject: [PATCH 1/4] Add test case --- tests/inputs-luau/if-expression-comments-3.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 tests/inputs-luau/if-expression-comments-3.lua diff --git a/tests/inputs-luau/if-expression-comments-3.lua b/tests/inputs-luau/if-expression-comments-3.lua new file mode 100644 index 00000000..3962220d --- /dev/null +++ b/tests/inputs-luau/if-expression-comments-3.lua @@ -0,0 +1,12 @@ +-- https://github.com/JohnnyMorganz/StyLua/issues/520 +do + return if #timings <= workers + then max + else math.max(Array.reduce(timings, function( + -- food + sum, + time_ + ) + return sum + time_ + end) / workers, max) +end From 3c1f8695937beb1729fd9249cb86922933ff42d8 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 20 Aug 2022 16:18:45 +0100 Subject: [PATCH 2/4] Don't break on else if it is not necessary --- src/formatters/expression.rs | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/formatters/expression.rs b/src/formatters/expression.rs index 5d99d232..75e5437a 100644 --- a/src/formatters/expression.rs +++ b/src/formatters/expression.rs @@ -454,7 +454,15 @@ fn format_token_expression_sequence( expression.update_trailing_trivia(FormatTriviaType::Replace(vec![])), ); // Remove trailing trivia (comments) before checking, as they shouldn't have an impact - let token = match requires_multiline_expression { + let newline_after_token = trivia_util::trivia_contains_comments( + token.trailing_trivia(), + trivia_util::CommentSearch::Single, + ) || trivia_util::trivia_contains_comments( + trivia_util::get_expression_leading_trivia(expression).iter(), + trivia_util::CommentSearch::Single, + ); + + let token = match newline_after_token { // `\n` true => format_token_reference(ctx, token, shape) .update_trailing_trivia(FormatTriviaType::Append(vec![create_newline_trivia(ctx)])), @@ -465,12 +473,15 @@ fn format_token_expression_sequence( }; let expression = match requires_multiline_expression { - true => { - let shape = shape.reset().increment_additional_indent(); - hang_expression(ctx, expression, shape, None).update_leading_trivia( - FormatTriviaType::Append(vec![create_indent_trivia(ctx, shape)]), - ) - } + true => match newline_after_token { + true => { + let shape = shape.reset().increment_additional_indent(); + hang_expression(ctx, expression, shape, None).update_leading_trivia( + FormatTriviaType::Append(vec![create_indent_trivia(ctx, shape)]), + ) + } + false => hang_expression(ctx, expression, shape, None), + }, false => formatted_expression, }; From 17dd44fc8feebd7e36e8c6ef2110d730a390ea6f Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 20 Aug 2022 16:18:51 +0100 Subject: [PATCH 3/4] Snapshot --- ...ests__luau@if-expression-comments-3.lua.snap | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/snapshots/tests__luau@if-expression-comments-3.lua.snap diff --git a/tests/snapshots/tests__luau@if-expression-comments-3.lua.snap b/tests/snapshots/tests__luau@if-expression-comments-3.lua.snap new file mode 100644 index 00000000..a59fd36b --- /dev/null +++ b/tests/snapshots/tests__luau@if-expression-comments-3.lua.snap @@ -0,0 +1,17 @@ +--- +source: tests/tests.rs +expression: format(&contents) +--- +-- https://github.com/JohnnyMorganz/StyLua/issues/520 +do + return if #timings <= workers + then max + else math.max(Array.reduce(timings, function( + -- food + sum, + time_ + ) + return sum + time_ + end) / workers, max) +end + From a64a4b9309522ec4a8fd9beb49089e578ea6a89b Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 20 Aug 2022 16:19:22 +0100 Subject: [PATCH 4/4] Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ca0256f..c8dac96b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Long union/interesection types inside of a parentheses will now cause the parentheses to expand multiline ([#531](https://github.com/JohnnyMorganz/StyLua/issues/531)) - Fixed leading comments lost from an expression when excessive parentheses are removed from it ([#530](https://github.com/JohnnyMorganz/StyLua/issues/530)) - Fixed comments present in a complex expression not forcing multiline hanging leading to a syntax error ([#524](https://github.com/JohnnyMorganz/StyLua/issues/524)) +- Fixed unnecessary break on `else` in an if-expression when the expression contains a comment ([#520](https://github.com/JohnnyMorganz/StyLua/issues/520)) ## [0.14.2] - 2022-07-27