Repository: sdispater/tomlkit

Issue:
Commas are sometimes appended to comments
Here's a simple repro:
```python
import tomlkit

doc_str = """
[A]
B = [
    # Here's a comment.
    "C",
]
"""

doc = tomlkit.parse(doc_str)
deps = doc['A']['B'].multiline(True)
deps.append('D')
print(doc.as_string())
```
This is the output:
```toml

[A]
B = [
    # Here's a comment.,
    "C",
    "D",
]

```
The "Here's a comment" comment has a comma appended to it. (Expected behavior: exactly the same output, but without the comma appended to the comment.)

Discussion:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a576eaf..6de02aa 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,7 @@
 ### Fixed
 
 - Preserve the newlines before super tables when rendering. ([#178](https://github.com/sdispater/tomlkit/issues/178))
+- Fix the bug that comments are appended with comma when rendering a multiline array. ([#181](https://github.com/sdispater/tomlkit/issues/181))
 
 ## [0.10.0] - 2022-02-18
 
diff --git a/tomlkit/items.py b/tomlkit/items.py
index ba7f848..0da4fbe 100644
--- a/tomlkit/items.py
+++ b/tomlkit/items.py
@@ -988,11 +988,15 @@ class Array(Item, _CustomList):
         if not self._multiline or not self._value:
             return "[{}]".format("".join(v.as_string() for v in self._value))
 
-        s = "[\n" + self.trivia.indent + " " * 4
-        s += (",\n" + self.trivia.indent + " " * 4).join(
-            v.as_string() for v in self._value if not isinstance(v, Whitespace)
+        s = "[\n"
+        s += "".join(
+            self.trivia.indent
+            + " " * 4
+            + v.as_string()
+            + ("\n" if isinstance(v, Comment) else ",\n")
+            for v in self._value
+            if not isinstance(v, Whitespace)
         )
-        s += ",\n"
         s += "]"
 
         return s


Test patch:
diff --git a/tests/test_items.py b/tests/test_items.py
index 9c73a94..f81971e 100644
--- a/tests/test_items.py
+++ b/tests/test_items.py
@@ -275,6 +275,30 @@ x = [
     )
 
 
+def test_append_to_multiline_array_with_comment():
+    doc = parse(
+        """\
+x = [
+    # Here is a comment
+    1,
+    2
+]
+"""
+    )
+    doc["x"].multiline(True).append(3)
+    assert (
+        doc.as_string()
+        == """\
+x = [
+    # Here is a comment
+    1,
+    2,
+    3,
+]
+"""
+    )
+
+
 def test_append_dict_to_array():
     doc = parse("x = [ ]")
     doc["x"].append({"name": "John Doe", "email": "john@doe.com"})