Split semicolon separated statements
Separating statements with a semicolon is discouraged.
diff --git a/yapf/yapflib/pytree_unwrapper.py b/yapf/yapflib/pytree_unwrapper.py
index 0addce5..510f37b 100644
--- a/yapf/yapflib/pytree_unwrapper.py
+++ b/yapf/yapflib/pytree_unwrapper.py
@@ -213,8 +213,12 @@
"""
if (leaf.type not in _WHITESPACE_TOKENS and
(leaf.type != grammar_token.COMMENT or leaf.value.strip())):
- # Add non-whitespace tokens and comments that aren't empty.
- self._cur_unwrapped_line.AppendNode(leaf)
+ if leaf.value == ';':
+ # Split up multiple statements on one line.
+ self._StartNewLine()
+ else:
+ # Add non-whitespace tokens and comments that aren't empty.
+ self._cur_unwrapped_line.AppendNode(leaf)
_BRACKET_MATCH = {')': '(', '}': '{', ']': '['}
diff --git a/yapftests/reformatter_test.py b/yapftests/reformatter_test.py
index ab5ee16..48b0881 100644
--- a/yapftests/reformatter_test.py
+++ b/yapftests/reformatter_test.py
@@ -1408,12 +1408,20 @@
uwlines = _ParseAndUnwrap(unformatted_code)
self.assertEqual(expected_formatted_code, reformatter.Reformat(uwlines))
- def testSpaceBeforeSemicolon(self):
+ def testSplittingSemicolonStatements(self):
unformatted_code = textwrap.dedent("""\
- x = y + 42 ; z = n * 42
+ def f():
+ x = y + 42 ; z = n * 42
+ if True: a += 1 ; b += 1; c += 1
""")
expected_formatted_code = textwrap.dedent("""\
- x = y + 42; z = n * 42
+ def f():
+ x = y + 42
+ z = n * 42
+ if True:
+ a += 1
+ b += 1
+ c += 1
""")
uwlines = _ParseAndUnwrap(unformatted_code)
self.assertEqual(expected_formatted_code, reformatter.Reformat(uwlines))