diff --git a/CHANGELOG.md b/CHANGELOG.md index 98386381..e8ea0da2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added flag `--no-ignore-vcs` to continue formatting files listed in a `.gitignore` file, instead of skipping over them ([#895](https://github.com/JohnnyMorganz/StyLua/issues/895)) +### Fixed + +- Fixed syntax error in output when a single-line comment appears between an index suffix and a table call argument, e.g. `foo.bar -- comment { }` ([#873](https://github.com/JohnnyMorganz/StyLua/issues/873)) + ## [2.3.1] - 2025-11-01 ### Fixed diff --git a/src/formatters/functions.rs b/src/formatters/functions.rs index cb5393a2..3954fe55 100644 --- a/src/formatters/functions.rs +++ b/src/formatters/functions.rs @@ -1141,10 +1141,18 @@ pub fn format_function_call( let mut suffix = format_suffix(ctx, suffix, current_shape, ambiguous_next_suffix); + // Check if the previous suffix has trailing single-line comments. + // If it does, we MUST hang the AnonymousCall to avoid the opening parenthesis being commented out + let previous_suffix_has_trailing_singleline_comment = formatted_suffixes + .last() + .map(|s: &Suffix| s.has_trailing_comments(CommentSearch::Single)) + .unwrap_or(false); + // Hang the call, but don't hang if the previous suffix was an index and this is an anonymous call, i.e. `.foo()` if will_hang && !(previous_suffix_was_index - && matches!(suffix, Suffix::Call(Call::AnonymousCall(_)))) + && matches!(suffix, Suffix::Call(Call::AnonymousCall(_))) + && !previous_suffix_has_trailing_singleline_comment) { suffix = trivia_util::prepend_newline_indent(ctx, &suffix, current_shape); } diff --git a/tests/inputs/hang-call-chain-comments-4.lua b/tests/inputs/hang-call-chain-comments-4.lua new file mode 100644 index 00000000..5542accf --- /dev/null +++ b/tests/inputs/hang-call-chain-comments-4.lua @@ -0,0 +1,15 @@ +-- https://github.com/JohnnyMorganz/StyLua/issues/873 + +x = a.b -- comment +{ + y +} + +console.dialog = iup.dialog +{ + iup.hbox -- use it to inherit margins + { + console.prompt, + }, + title = "Command:", +} diff --git a/tests/snapshots/tests__standard@hang-call-chain-comments-4.lua.snap b/tests/snapshots/tests__standard@hang-call-chain-comments-4.lua.snap new file mode 100644 index 00000000..4e97b650 --- /dev/null +++ b/tests/snapshots/tests__standard@hang-call-chain-comments-4.lua.snap @@ -0,0 +1,21 @@ +--- +source: tests/tests.rs +expression: "format(&contents, LuaVersion::Lua51)" +input_file: tests/inputs/hang-call-chain-comments-4.lua +--- +-- https://github.com/JohnnyMorganz/StyLua/issues/873 + +x = a + .b -- comment + ({ + y, + }) + +console.dialog = iup.dialog({ + iup + .hbox -- use it to inherit margins + ({ + console.prompt, + }), + title = "Command:", +})