Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary else break in if expression comments #539

Merged
merged 4 commits into from
Aug 20, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 18 additions & 7 deletions src/formatters/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
// `<token>\n`
true => format_token_reference(ctx, token, shape)
.update_trailing_trivia(FormatTriviaType::Append(vec![create_newline_trivia(ctx)])),
Expand All @@ -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,
};

Expand Down
12 changes: 12 additions & 0 deletions tests/inputs-luau/if-expression-comments-3.lua
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions tests/snapshots/tests__luau@if-expression-comments-3.lua.snap
Original file line number Diff line number Diff line change
@@ -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