Merge pull request #2116 from mhnaeem/json-invalid-wrap-with-line-length-and-signed-number

fix - invalid line wrap with signed numbers in json property
This commit is contained in:
Liam Newman 2023-08-30 15:45:05 -07:00 committed by GitHub
commit 4925cbc664
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 2 deletions

View File

@ -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);
}
}
@ -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)) {

View File

@ -974,7 +974,12 @@ class Beautifier:
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"):
@ -1324,6 +1329,11 @@ class Beautifier:
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 (

View File

@ -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 + \'',