Skip to content

Keep code after a single-line comment before do or after ( - #1178

Open
ybai08 wants to merge 1 commit into
JohnnyMorganz:mainfrom
ybai08:fix/1176-comment-before-code
Open

ybai08 wants to merge 1 commit into
JohnnyMorganz:mainfrom
ybai08:fix/1176-comment-before-code

Conversation

@ybai08

@ybai08 ybai08 commented Oct 17, 2026

Copy link
Copy Markdown

Fixes #1176.

Both cases in the issue produced output that no longer parses, because a single-line comment ended up in front of code on the same line.

1. Numeric for with a comment before do. format_numeric_for always appended " do" after the end or step expression, so a trailing -- comment on that expression swallowed it. If the last expression of the header (the step, or else the end) has a trailing single-line comment, or do has a leading one, it now ends that expression with a newline and puts do on its own line, with do's leading comments handled as while does (format_end_token, IndentComments). while and generic for already keep do on its own line in this case. A block comment (--[[ ]]) before do is unchanged, since it can't swallow anything.

2. Comment right after ( in a hanging condition. In format_hanging_expression_, a short parenthesised expression took the single-line shortcut when the expression itself had no comments. A comment attached to the opening parenthesis wasn't checked, so ( -- comment was followed by b or c) on the same line. A trailing single-line comment on ( now sends it down the existing hanging path, which already puts the expression on the next line.

Output for the issue's inputs:

for i = 1, 2 -- comment
do
	print(i)
end
if
	a and ( -- comment
		b or c
	)
then
	print("yes")
end

The second fix also covers local x = ( -- comment followed by 1 + 2 ), which on main gave local x = ( -- comment1 + 2).

Tests

There are two new snapshot inputs.

tests/inputs/numeric-for-comment-before-do.lua covers:

  • the issue's case;
  • the same with a step;
  • a comment on its own line before do;
  • a block comment before do, which stays on one line.

tests/inputs/parentheses-comment-after-open.lua covers the issue's condition and the local case.

Without the two src changes, both snapshots fail. With them, cargo test passes for every feature set in ci.yml on macOS arm64:

Features Passed
default 201
--no-default-features 144
luau 210
lua52 203
lua53 204
lua54 205
luajit 203
cfxlua 206

Formatting the new snapshots' output again gives the same output, and it parses. cargo fmt --check and cargo clippy --all-targets --all-features are clean.

A related case I left alone: print(( -- c / 1 / )) drops the comment on main and still does with this change. The redundant inner parentheses are removed along with the comment attached to them. That's a different code path, so I can open a separate issue if you like.

A comment before `do` in a numeric for loop, or right after the opening
parenthesis of a hanging condition, was followed by the rest of the code on
the same line, commenting it out. Put `do` on its own line, as while and
generic for already do, and hang the parenthesised expression when its
opening parenthesis carries a single-line comment.

Fixes JohnnyMorganz#1176

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code after a -- comment gets joined into the comment: numeric for ... do, and ( -- comment in a condition

1 participant