Skip to content

Commit

Permalink
1283 - Javascript ++ Operator Gets Wrong Indentation
Browse files Browse the repository at this point in the history
Resolves beautifier#1283

**What was done:**
 - Adapted function print_newline to restore mode if last_test was operator ++ or --
 - Added corresponding test.

**Example of behavior:**
== Input ==     |== Expexted Output ==
{this.foo++     |{
bar}            |    this.foo++
                |    bar
                |}
                |
  • Loading branch information
Elrendio committed May 4, 2018
1 parent 36c3934 commit 1a31818
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion js/lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ function Beautifier(js_source_text, options) {

function print_newline(force_newline, preserve_statement_flags) {
if (!preserve_statement_flags) {
if (flags.last_text !== ';' && flags.last_text !== ',' && flags.last_text !== '=' && last_type !== 'TK_OPERATOR') {
if (flags.last_text !== ';' && flags.last_text !== ',' && flags.last_text !== '=' && (last_type !== 'TK_OPERATOR' || flags.last_text === '--' || flags.last_text === '++')) {
var next_token = get_token(1);
while (flags.mode === MODE.Statement &&
!(flags.if_block && next_token && next_token.type === 'TK_RESERVED' && next_token.text === 'else') &&
Expand Down
2 changes: 1 addition & 1 deletion js/src/javascript/beautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ function Beautifier(js_source_text, options) {

function print_newline(force_newline, preserve_statement_flags) {
if (!preserve_statement_flags) {
if (flags.last_text !== ';' && flags.last_text !== ',' && flags.last_text !== '=' && last_type !== 'TK_OPERATOR') {
if (flags.last_text !== ';' && flags.last_text !== ',' && flags.last_text !== '=' && (last_type !== 'TK_OPERATOR' || flags.last_text === '--' || flags.last_text === '++')) {
var next_token = get_token(1);
while (flags.mode === MODE.Statement &&
!(flags.if_block && next_token && next_token.type === 'TK_RESERVED' && next_token.text === 'else') &&
Expand Down
10 changes: 10 additions & 0 deletions js/test/generated/beautify-javascript-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3744,6 +3744,16 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
'if (someCondition) {\n' +
' return something;\n' +
'}');

// Issue #1283 - Javascript ++ Operator get wrong indent
bt(
'{this.foo++\n' +
'bar}',
// -- output --
'{\n' +
' this.foo++\n' +
' bar\n' +
'}');


//============================================================
Expand Down
2 changes: 1 addition & 1 deletion python/jsbeautifier/javascript/beautifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def allow_wrap_or_preserved_newline(self, current_token, force_linewrap = False)

def print_newline(self, force_newline = False, preserve_statement_flags = False):
if not preserve_statement_flags:
if self.flags.last_text != ';' and self.flags.last_text != ',' and self.flags.last_text != '=' and self.last_type != 'TK_OPERATOR':
if self.flags.last_text != ';' and self.flags.last_text != ',' and self.flags.last_text != '=' and (self.last_type != 'TK_OPERATOR' or self.flags.last_text == '--' or self.flags.last_text == '++'):
next_token = self.get_token(1)
while (self.flags.mode == MODE.Statement and
not (self.flags.if_block and next_token and next_token.type == 'TK_RESERVED' and next_token.text == 'else') and
Expand Down
10 changes: 10 additions & 0 deletions python/jsbeautifier/tests/generated/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3572,6 +3572,16 @@ def unicode_char(value):
'if (someCondition) {\n' +
' return something;\n' +
'}')

# Issue #1283 - Javascript ++ Operator get wrong indent
bt(
'{this.foo++\n' +
'bar}',
# -- output --
'{\n' +
' this.foo++\n' +
' bar\n' +
'}')


#============================================================
Expand Down
13 changes: 13 additions & 0 deletions test/data/javascript/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2627,6 +2627,19 @@ exports.test_data = {
' return something;',
'}'
]
},
{
comment: "Issue #1283 - Javascript ++ Operator get wrong indent ",
input: [
'{this.foo++',
'bar}'
],
output: [
'{',
' this.foo++',
' bar',
'}'
]
}
]
}, {
Expand Down

0 comments on commit 1a31818

Please sign in to comment.