Skip to content

Commit

Permalink
Merge pull request #2116 from mhnaeem/json-invalid-wrap-with-line-len…
Browse files Browse the repository at this point in the history
…gth-and-signed-number

fix - invalid line wrap with signed numbers in json property
  • Loading branch information
bitwiseman authored Aug 30, 2023
2 parents 44b7131 + 87483fc commit 4925cbc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
10 changes: 9 additions & 1 deletion js/src/javascript/beautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,9 @@ Beautifier.prototype.handle_word = function(current_token) {
}

if (this._flags.last_token.type === TOKEN.COMMA || this._flags.last_token.type === TOKEN.START_EXPR || this._flags.last_token.type === TOKEN.EQUALS || this._flags.last_token.type === TOKEN.OPERATOR) {
if (!this.start_of_object_property()) {
if (!this.start_of_object_property() && !(
// start of object property is different for numeric values with +/- prefix operators
in_array(this._flags.last_token.text, ['+', '-']) && this._last_last_text === ':' && this._flags.parent.mode === MODE.ObjectLiteral)) {
this.allow_wrap_or_preserved_newline(current_token);
}
}
Expand Down Expand Up @@ -1172,6 +1174,12 @@ Beautifier.prototype.handle_operator = function(current_token) {
return;
}

if (in_array(current_token.text, ['-', '+']) && this.start_of_object_property()) {
// numeric value with +/- symbol in front as a property
this.print_token(current_token);
return;
}

// Allow line wrapping between operators when operator_position is
// set to before or preserve
if (this._flags.last_token.type === TOKEN.OPERATOR && in_array(this._options.operator_position, OPERATOR_POSITION_BEFORE_OR_PRESERVE)) {
Expand Down
12 changes: 11 additions & 1 deletion python/jsbeautifier/javascript/beautifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,12 @@ def handle_word(self, current_token):
TOKEN.EQUALS,
TOKEN.OPERATOR,
]:
if not self.start_of_object_property():
if not self.start_of_object_property() and not (
# start of object property is different for numeric values with +/- prefix operators
self._flags.last_token.text in ["+", "-"]
and self._last_last_text == ":"
and self._flags.parent.mode == MODE.ObjectLiteral
):
self.allow_wrap_or_preserved_newline(current_token)

if reserved_word(current_token, "function"):
Expand Down Expand Up @@ -1324,6 +1329,11 @@ def handle_operator(self, current_token):
self.print_token(current_token)
return

if current_token.text in ["-", "+"] and self.start_of_object_property():
# numeric value with +/- symbol in front as a property
self.print_token(current_token)
return

# Allow line wrapping between operators when operator_position is
# set to before or preserve
if (
Expand Down
15 changes: 15 additions & 0 deletions test/data/javascript/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,21 @@ exports.test_data = {
'}'
]
},
{
comment: "Issue #1932 - Javascript object property with -/+ symbol wraps issue",
input: [
'{',
' "1234567891234567891234567891234": -433,',
' "abcdefghijklmnopqrstuvwxyz12345": +11',
'}'
],
output: [
'{',
' "1234567891234567891234567891234": -433,',
' "abcdefghijklmnopqrstuvwxyz12345": +11',
'}'
]
},
{
fragment: true,
input: '\' + wrap_input_2 + \'',
Expand Down

0 comments on commit 4925cbc

Please sign in to comment.