diff --git a/python/jsbeautifier/javascript/beautifier.py b/python/jsbeautifier/javascript/beautifier.py index ef06bc9c3..55f63014a 100644 --- a/python/jsbeautifier/javascript/beautifier.py +++ b/python/jsbeautifier/javascript/beautifier.py @@ -312,7 +312,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 diff --git a/python/jsbeautifier/tests/core/test_wrap_line_length.py b/python/jsbeautifier/tests/core/test_wrap_line_length.py new file mode 100644 index 000000000..90aec1b97 --- /dev/null +++ b/python/jsbeautifier/tests/core/test_wrap_line_length.py @@ -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))