Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix invalid json being generated with wrap_line_length #1931

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion python/jsbeautifier/javascript/beautifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,10 @@ def allow_wrap_or_preserved_newline(self, current_token, force_linewrap=False):
if shouldPreserveOrForce:
self.print_newline(preserve_statement_flags=True)
elif self._options.wrap_line_length > 0:
if reserved_array(self._flags.last_token, self._newline_restricted_tokens):
if (
reserved_array(self._flags.last_token, self._newline_restricted_tokens)
or self._flags.last_token.type == TOKEN.OPERATOR
):
# These tokens should never have a newline inserted between
# them and the following expression.
return
Expand Down
25 changes: 25 additions & 0 deletions python/jsbeautifier/tests/core/test_wrap_line_length.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import jsbeautifier
import json
import unittest


class TestWrapLineLength(unittest.TestCase):
@classmethod
def setUpClass(cls):
pass

def test_wrap_line_does_not_create_invalid_json(self):
options = jsbeautifier.default_options()
options.indent_size = 4
options.brace_style = "expand"
options.wrap_line_length = 40
obj = {
"1234567891234567891234567891234": -4
}
# make sure exception is not raised due to bad json (line break after -):
# {
# "1234567891234567891234567891234": -
# 4
# }
# json.decoder.JSONDecodeError: Expecting value: line 2 column 40 (char 41)
json.loads(jsbeautifier.beautify(json.dumps(obj), options))