Repository: asottile/add-trailing-comma

Issue:
Tuple/list split across lines gets rewritten to invalid syntax
```
x = ([a,
     b], None)
```

gets rewritten to this:

```
x = (     
    [     
        a,
        b,
    ],    
, None    
)         
```

which is invalid syntax:
```
  File "test.py", line 6   
    , None                 
    ^                      
SyntaxError: invalid syntax
```

(with version 0.6.0)

Discussion:

Patch:
diff --git a/add_trailing_comma.py b/add_trailing_comma.py
index 2e71af5..006dfe8 100644
--- a/add_trailing_comma.py
+++ b/add_trailing_comma.py
@@ -347,16 +347,18 @@ def _fix_src(contents_text, py35_plus, py36_plus):
         elif key in visitor.literals:
             fixes.append((True, _find_simple(i, tokens)))
 
-        # need to additionally handle literals afterwards as tuples report
-        # their starting index as the first element, which may be one of the
-        # above things.
-        if key in visitor.tuples:
-            fixes.append((True, _find_tuple(i, tokens)))
-
         for add_comma, fix_data in fixes:
             if fix_data is not None:
                 _fix_brace(fix_data, add_comma, tokens)
 
+        # need to handle tuples afterwards as tuples report their starting
+        # starting index as the first element, which may be one of the above
+        # things.
+        if key in visitor.tuples:
+            fix_data = _find_tuple(i, tokens)
+            if fix_data is not None:
+                _fix_brace(fix_data, True, tokens)
+
     return tokens_to_src(tokens)
 
 


Test patch:
diff --git a/tests/add_trailing_comma_test.py b/tests/add_trailing_comma_test.py
index a370743..a663f99 100644
--- a/tests/add_trailing_comma_test.py
+++ b/tests/add_trailing_comma_test.py
@@ -565,6 +565,18 @@ def test_noop_unhugs(src):
             '    "bar",\n'
             ')',
         ),
+        # Regression test for #29
+        (
+            'x = ([a,\n'
+            '      b], None)',
+
+            'x = (\n'
+            '    [\n'
+            '        a,\n'
+            '        b,\n'
+            '    ], None,\n'
+            ')',
+        ),
     ),
 )
 def test_fix_unhugs(src, expected):