Repository: JohnnyMorganz/StyLua (Rust)

Issue:
comments lost with call_parentheses = "None" 
With all defaults, no toml file, it works fine

``` shell
/tmp> stylua --version
stylua 0.12.0
/tmp> rm .stylua.toml
/tmp> echo 'some_func [[hello]] -- a comment' > a.lua
/tmp> stylua a.lua
/tmp> cat a.lua
some_func([[hello]]) -- a comment
/tmp> stylua a.lua
/tmp> cat a.lua
some_func([[hello]]) -- a comment
```

However with the new setting

```
/tmp> echo 'call_parentheses = "None"' > .stylua.toml
/tmp> echo 'some_func([[hello]]) -- a comment' > a.lua
/tmp> stylua a.lua
/tmp> cat a.lua
some_func [[hello]]
/tmp>
```

The comment gets lost



Discussion:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 895d0ad4..ab034a50 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - Fixed incorrect handling of comments at the end of a callback type's arguments under the `luau` feature flag. ([#352](https://github.com/JohnnyMorganz/StyLua/issues/352))
 - Fixed mistransformation of type declaration when the type info is a union which must be multiline due to comments under the `luau` feature flag. ([#351](https://github.com/JohnnyMorganz/StyLua/issues/351))
 - Fixed leading comments on a `|` symbol in a type info being lost when hanging the type under the `luau` feature flag.
+- Fixed trailing comments of a function call being lost as parentheses are removed around a single argument when `call_parentheses` is set to not `Always`. ([#356](https://github.com/JohnnyMorganz/StyLua/issues/356))
 
 ## [0.12.0] - 2022-01-31
 ### Added
diff --git a/src/formatters/functions.rs b/src/formatters/functions.rs
index 454a0a1e..d254a3ea 100644
--- a/src/formatters/functions.rs
+++ b/src/formatters/functions.rs
@@ -133,13 +133,24 @@ pub fn format_function_args(
                 && !matches!(call_next_node, FunctionCallNextNode::ObscureWithoutParens)
             {
                 let argument = arguments.iter().next().unwrap();
+
+                // Take any trailing trivia from the end parentheses, in case we need to keep it
+                let trailing_comments = parentheses
+                    .tokens()
+                    .1
+                    .trailing_trivia()
+                    .map(|x| x.to_owned())
+                    .collect();
+
                 if let Expression::Value { value, .. } = argument {
                     match &**value {
                         Value::String(token_reference) => {
                             if ctx.should_omit_string_parens() {
                                 return format_function_args(
                                     ctx,
-                                    &FunctionArgs::String(token_reference.to_owned()),
+                                    &FunctionArgs::String(token_reference.update_trailing_trivia(
+                                        FormatTriviaType::Append(trailing_comments),
+                                    )),
                                     shape,
                                     call_next_node,
                                 );
@@ -149,7 +160,11 @@ pub fn format_function_args(
                             if ctx.should_omit_table_parens() {
                                 return format_function_args(
                                     ctx,
-                                    &FunctionArgs::TableConstructor(table_constructor.to_owned()),
+                                    &FunctionArgs::TableConstructor(
+                                        table_constructor.update_trailing_trivia(
+                                            FormatTriviaType::Append(trailing_comments),
+                                        ),
+                                    ),
                                     shape,
                                     call_next_node,
                                 );


Test patch:
diff --git a/tests/test_call_parentheses.rs b/tests/test_call_parentheses.rs
index 15623c4a..d9058da7 100644
--- a/tests/test_call_parentheses.rs
+++ b/tests/test_call_parentheses.rs
@@ -296,3 +296,17 @@ local opt = my_function({
     "###
     );
 }
+
+#[test]
+#[cfg_attr(feature = "luau", ignore)]
+fn test_call_parens_comments() {
+    insta::assert_snapshot!(
+        format(CallParenType::None,
+            r###"
+foo("hello") -- comment
+"###
+        ),
+        @r###"foo "hello" -- comment
+    "###
+    );
+}