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 line wrap with signed numbers in json property #2116

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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