Skip to content
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fixed a single-line comment before `do` in a numeric `for` loop, or right after the opening parenthesis of a hanging condition, commenting out the code that followed it ([#1176](https://github.com/JohnnyMorganz/StyLua/issues/1176))

## [2.6.0] - 2026-10-10

### Added
Expand Down
5 changes: 5 additions & 0 deletions src/formatters/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,12 @@ fn format_hanging_expression_(
let formatted_expression = format_expression(ctx, expression, lhs_shape + 1); // 1 = opening parentheses

let expression_str = formatted_expression.to_string();
// A single-line comment after the opening parenthesis needs the expression on the next line
if !contains_comments(expression)
&& !contained
.tokens()
.0
.has_trailing_comments(CommentSearch::Single)
&& !lhs_shape.add_width(2 + expression_str.len()).over_budget()
{
// The expression inside the parentheses is small, we do not need to break it down further
Expand Down
37 changes: 35 additions & 2 deletions src/formatters/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,8 +785,41 @@ pub fn format_numeric_for(ctx: &Context, numeric_for: &NumericFor, shape: Shape)
_ => unreachable!("Got numeric for end step comma with no step or vice versa"),
};

let do_token = fmt_symbol!(ctx, numeric_for.do_token(), " do", shape)
.update_trailing_trivia(FormatTriviaType::Append(trailing_trivia.to_owned()));
// A single-line comment before `do` would comment it out, so put `do` on its own line,
// as for `while` and generic `for`
let do_on_own_line = numeric_for
.step()
.unwrap_or(numeric_for.end())
.has_trailing_comments(CommentSearch::Single)
|| numeric_for
.do_token()
.has_leading_comments(CommentSearch::Single);

let (end, step) = match (do_on_own_line, step) {
(true, Some(step)) => (
end,
Some(step.update_trailing_trivia(FormatTriviaType::Append(vec![
create_newline_trivia(ctx),
]))),
),
(true, None) => (
end.update_trailing_trivia(FormatTriviaType::Append(vec![create_newline_trivia(ctx)])),
None,
),
(false, step) => (end, step),
};

let do_token = match do_on_own_line {
true => format_end_token(
ctx,
numeric_for.do_token(),
EndTokenType::IndentComments,
shape,
)
.update_leading_trivia(FormatTriviaType::Append(leading_trivia.to_owned())),
false => fmt_symbol!(ctx, numeric_for.do_token(), " do", shape),
}
.update_trailing_trivia(FormatTriviaType::Append(trailing_trivia.to_owned()));
let block_shape = shape.reset().increment_block_indent();
let block = format_block(ctx, numeric_for.block(), block_shape);
let end_token = format_end_token(
Expand Down
19 changes: 19 additions & 0 deletions tests/inputs/numeric-for-comment-before-do.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
for i = 1, 2 -- comment
do
print(i)
end

for i = 1, 10, 2 -- comment
do
print(i)
end

for i = 1, 2
-- comment
do
print(i)
end

for i = 1, 2 --[[ comment ]] do
print(i)
end
12 changes: 12 additions & 0 deletions tests/inputs/parentheses-comment-after-open.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
local a, b, c = true, false, true
if a
and ( -- comment
b or c
)
then
print("yes")
end

local x = ( -- comment
1 + 2
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: tests/tests.rs
expression: "format(&contents, LuaVersion::Lua51)"
input_file: tests/inputs/numeric-for-comment-before-do.lua
---
for i = 1, 2 -- comment
do
print(i)
end

for i = 1, 10, 2 -- comment
do
print(i)
end

for i = 1, 2
-- comment
do
print(i)
end

for i = 1, 2 --[[ comment ]] do
print(i)
end

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: tests/tests.rs
expression: "format(&contents, LuaVersion::Lua51)"
input_file: tests/inputs/parentheses-comment-after-open.lua
---
local a, b, c = true, false, true
if
a and ( -- comment
b or c
)
then
print("yes")
end

local x = ( -- comment
1 + 2
)