Repository: pappasam/toml-sort

Issue:
IndexError exception on an empty array
TOML file:
```toml
# foo.toml

[tool.foo]
bar = []
```

Exception:
```
$ python -V
Python 3.11.0

$ toml-sort --version
0.22.0

$ toml-sort foo.toml
Traceback (most recent call last):
  File "/private/tmp/foo/.venv/bin/toml-sort", line 8, in <module>
    sys.exit(cli())
             ^^^^^
  File "/private/tmp/foo/.venv/lib/python3.11/site-packages/toml_sort/cli.py", line 356, in cli
    ).sorted()
      ^^^^^^^^
  File "/private/tmp/foo/.venv/lib/python3.11/site-packages/toml_sort/tomlsort.py", line 634, in sorted
    sorted_toml = self.toml_doc_sorted(toml_doc)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/foo/.venv/lib/python3.11/site-packages/toml_sort/tomlsort.py", line 620, in toml_doc_sorted
    item.key, self.toml_elements_sorted(item, sorted_document)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/foo/.venv/lib/python3.11/site-packages/toml_sort/tomlsort.py", line 439, in toml_elements_sorted
    item.key, self.toml_elements_sorted(item, previous_item)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/foo/.venv/lib/python3.11/site-packages/toml_sort/tomlsort.py", line 435, in toml_elements_sorted
    for item in self.sorted_children_table(original.children):
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/foo/.venv/lib/python3.11/site-packages/toml_sort/tomlsort.py", line 387, in sorted_children_table
    non_tables = self.sort_items(
                 ^^^^^^^^^^^^^^^^
  File "/private/tmp/foo/.venv/lib/python3.11/site-packages/toml_sort/tomlsort.py", line 359, in sort_items
    item.value = self.sort_item(item.value)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/foo/.venv/lib/python3.11/site-packages/toml_sort/tomlsort.py", line 306, in sort_item
    return self.sort_array(item, indent_depth=indent_depth)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/foo/.venv/lib/python3.11/site-packages/toml_sort/tomlsort.py", line 284, in sort_array
    new_array_value[-1].comma = Whitespace("")
    ~~~~~~~~~~~~~~~^^^^
IndexError: list index out of range

```

Comments:

Patch:
diff --git a/toml_sort/tomlsort.py b/toml_sort/tomlsort.py
index f17d54e..e43d848 100644
--- a/toml_sort/tomlsort.py
+++ b/toml_sort/tomlsort.py
@@ -280,7 +280,9 @@ class TomlSort:
                 new_array_value.extend(comments)
             new_array_value.append(array_item)
 
-        if not (multiline and self.format_config.trailing_comma_inline_array):
+        if len(new_array_value) != 0 and not (
+            multiline and self.format_config.trailing_comma_inline_array
+        ):
             new_array_value[-1].comma = Whitespace("")
 
         if multiline:


Test patch:
diff --git a/tests/examples/inline.toml b/tests/examples/inline.toml
index 49a1718..f54ef3d 100644
--- a/tests/examples/inline.toml
+++ b/tests/examples/inline.toml
@@ -1,4 +1,6 @@
 [b]
+z = []
+q = {}
 blist=[    'c',     'a', 'b'] # Comment
 alist = [
   'g',
diff --git a/tests/examples/sorted/inline-default.toml b/tests/examples/sorted/inline-default.toml
index b47b062..71e586d 100644
--- a/tests/examples/sorted/inline-default.toml
+++ b/tests/examples/sorted/inline-default.toml
@@ -13,6 +13,8 @@ test = [
 ]
 
 [b]
+z = []
+q = {}
 blist = ['c', 'a', 'b'] # Comment
 alist = [
   'g',
diff --git a/tests/examples/sorted/inline-no-comments-no-table-sort.toml b/tests/examples/sorted/inline-no-comments-no-table-sort.toml
index ad7230f..bdfc385 100644
--- a/tests/examples/sorted/inline-no-comments-no-table-sort.toml
+++ b/tests/examples/sorted/inline-no-comments-no-table-sort.toml
@@ -17,6 +17,8 @@ alist = [
   'w',
 ]
 blist = ['a', 'b', 'c']
+q = {}
+z = []
 
 [a]
 test = [
diff --git a/tests/examples/sorted/inline.toml b/tests/examples/sorted/inline.toml
index e876ac7..b0985d4 100644
--- a/tests/examples/sorted/inline.toml
+++ b/tests/examples/sorted/inline.toml
@@ -37,3 +37,5 @@ alist = [
   'w'  # Comment inline
 ]
 blist = ['a', 'b', 'c']  # Comment
+q = {}
+z = []