Skip to content

Commit bed7ca5

Browse files
authored
pytree_utils.ParseCodeToTree now adds a trailing EOL if needed (#962)
This was previously done by ``yapf_api.FormatCode``, but I think it makes more sense to have it in ``pytree_utils.ParseCodeToTree``, because the grammar requires a trailing EOL.
1 parent 6471e0a commit bed7ca5

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

yapf/yapflib/pytree_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"""
2626

2727
import ast
28+
import os
2829

2930
from lib2to3 import pygram
3031
from lib2to3 import pytree
@@ -108,6 +109,9 @@ def ParseCodeToTree(code):
108109
"""
109110
# This function is tiny, but the incantation for invoking the parser correctly
110111
# is sufficiently magical to be worth abstracting away.
112+
if not code.endswith(os.linesep):
113+
code += os.linesep
114+
111115
try:
112116
# Try to parse using a Python 3 grammar, which is more permissive (print and
113117
# exec are not keywords).

yapf/yapflib/yapf_api.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,6 @@ def FormatCode(unformatted_source,
177177
Tuple of (reformatted_source, changed). reformatted_source conforms to the
178178
desired formatting style. changed is True if the source changed.
179179
"""
180-
if not unformatted_source.endswith('\n'):
181-
unformatted_source += '\n'
182-
183180
try:
184181
tree = pytree_utils.ParseCodeToTree(unformatted_source)
185182
except parse.ParseError as e:

0 commit comments

Comments
 (0)