diff --git a/js/src/javascript/beautifier.js b/js/src/javascript/beautifier.js index dfec38351..37350c225 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 217507c42..fff41a05f 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 009361949..dd0f31690 100644 --- a/test/data/javascript/tests.js +++ b/test/data/javascript/tests.js @@ -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 + \'',