From 4a2995e8add984297450a5d65eac8bf1e379b37e Mon Sep 17 00:00:00 2001 From: Muhammad Hammad Date: Thu, 17 Nov 2022 16:59:13 -0500 Subject: [PATCH] fix - invalid line wrap with signed numbers in json property This fix makes sure we can identify signed numbers correctly when they are used as an object property to avoid having issues with line wrapping fixes #1932 --- js/src/javascript/beautifier.js | 10 +++++++++- python/jsbeautifier/javascript/beautifier.py | 12 +++++++++++- test/data/javascript/tests.js | 15 +++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/js/src/javascript/beautifier.js b/js/src/javascript/beautifier.js index 51cd10d8e..880be6bde 100644 --- a/js/src/javascript/beautifier.js +++ b/js/src/javascript/beautifier.js @@ -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)) { diff --git a/python/jsbeautifier/javascript/beautifier.py b/python/jsbeautifier/javascript/beautifier.py index efeb584b4..b0588902d 100644 --- a/python/jsbeautifier/javascript/beautifier.py +++ b/python/jsbeautifier/javascript/beautifier.py @@ -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"): @@ -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 ( diff --git a/test/data/javascript/tests.js b/test/data/javascript/tests.js index 367c3e231..aeed67e5d 100644 --- a/test/data/javascript/tests.js +++ b/test/data/javascript/tests.js @@ -1216,6 +1216,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 + \'',