Skip to content

Commit

Permalink
fix: fix a regression of adding comment only line to an array
Browse files Browse the repository at this point in the history
Fix #223
  • Loading branch information
frostming committed Aug 10, 2022
1 parent a157e59 commit 97487d2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 6 additions & 2 deletions tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ def test_array_add_line():
t.add_line(1, 2, 3, comment="Line 1")
t.add_line(4, 5, 6, comment="Line 2")
t.add_line(7, api.ws(","), api.ws(" "), 8, add_comma=False)
t.add_line(comment="Line 4")
t.add_line(indent="")
assert len(t) == 8
assert list(t) == [1, 2, 3, 4, 5, 6, 7, 8]
Expand All @@ -484,6 +485,7 @@ def test_array_add_line():
1, 2, 3, # Line 1
4, 5, 6, # Line 2
7, 8,
# Line 4
]"""
)

Expand Down Expand Up @@ -864,14 +866,16 @@ def test_table_copy():


def test_copy_copy():
result = parse("""
result = parse(
"""
[tool.poetry]
classifiers = [
# comment
"a",
"b",
]
""")
"""
)
classifiers = result["tool"]["poetry"]["classifiers"]
new = copy.copy(classifiers)
assert new == classifiers
9 changes: 8 additions & 1 deletion tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,14 @@ def add_line(
list.extend(self, data_values)
if len(self._value) > 0:
last_item = self._value[-1]
last_value_item = next((v for v in self._value[::-1] if v.value), None)
last_value_item = next(
(
v
for v in self._value[::-1]
if v.value is not None and not isinstance(v.value, Null)
),
None,
)
if last_value_item is not None:
last_value_item.comma = Whitespace(",")
if last_item.is_whitespace():
Expand Down

0 comments on commit 97487d2

Please sign in to comment.