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

Fix collapsed if stmt formatting when block begins with empty line #753

Merged
merged 4 commits into from
Sep 2, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fixed LuaJIT suffixes `LL`/`ULL` causing a panic when running in `--verify` mode ([#750](https://github.com/JohnnyMorganz/StyLua/issues/750))
- Fixed incorrect formatting of conditionals when `collapse_simple_statement` is enabled and the block begins with an empty line ([#744](https://github.com/JohnnyMorganz/StyLua/issues/744))

## [0.18.1] - 2023-07-15

Expand Down
8 changes: 6 additions & 2 deletions src/formatters/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,12 @@ pub fn format_if(ctx: &Context, if_node: &If, shape: Shape) -> If {
// Rather than deferring to `format_block()`, since we know that there is only a single Stmt or LastStmt in the block, we can format it immediately
// We need to modify the formatted LastStmt, since it will have automatically added leading/trailing trivia we don't want
// We assume that there is only a laststmt present in the block - the callee of this function should have already checked for this
let stmt_leading_trivia = FormatTriviaType::Append(vec![Token::new(TokenType::spaces(1))]);
let stmt_trailing_trivia = FormatTriviaType::Append(vec![Token::new(TokenType::spaces(1))]);
// INVARIANT: this stmt has no leading/trailing comments, as this is checked in `is_if_guard`
// This means we can replace trivia completely
debug_assert!(!trivia_util::contains_comments(if_node.block()));
let stmt_leading_trivia = FormatTriviaType::Replace(vec![Token::new(TokenType::spaces(1))]);
let stmt_trailing_trivia =
FormatTriviaType::Replace(vec![Token::new(TokenType::spaces(1))]);

let block = if let Some(stmt) = if_node.block().stmts().next() {
let stmt = format_stmt_no_trivia(ctx, stmt, singleline_shape)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- https://github.com/JohnnyMorganz/StyLua/issues/744

if tabnr ~= finaltab then

stack:push('%T')
end
8 changes: 8 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: tests/tests.rs
expression: "format_code(&contents,\n Config::default().with_collapse_simple_statement(CollapseSimpleStatement::Always),\n None, OutputVerification::None).unwrap()"
---
-- https://github.com/JohnnyMorganz/StyLua/issues/744

if tabnr ~= finaltab then stack:push("%T") end