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 ==
{this.foo++
bar}

== Expexted Output ==
{
    this.foo++
    bar
}

== Unchanged ==
axios.interceptors.request.use(
    config => {
        // loading
        window.store.loading++
        let extraParams = {}
    }
)
  • Loading branch information
Elrendio committed May 25, 2018
1 parent 36c3934 commit 268064b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 4 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
20 changes: 20 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,26 @@ 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' +
'}');

// Issue #1283 - Javascript ++ Operator get wrong indent (2)
bt(
'axios.interceptors.request.use(\n' +
' config => {\n' +
' // loading\n' +
' window.store.loading++\n' +
' let extraParams = {}\n' +
' }\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
20 changes: 20 additions & 0 deletions python/jsbeautifier/tests/generated/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3572,6 +3572,26 @@ 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' +
'}')

# Issue #1283 - Javascript ++ Operator get wrong indent (2)
bt(
'axios.interceptors.request.use(\n' +
' config => {\n' +
' // loading\n' +
' window.store.loading++\n' +
' let extraParams = {}\n' +
' }\n' +
')')


#============================================================
Expand Down
27 changes: 26 additions & 1 deletion test/data/javascript/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2627,6 +2627,31 @@ exports.test_data = {
' return something;',
'}'
]
},
{
comment: "Issue #1283 - Javascript ++ Operator get wrong indent ",
input: [
'{this.foo++',
'bar}'
],
output: [
'{',
' this.foo++',
' bar',
'}'
]
},
{
comment: "Issue #1283 - Javascript ++ Operator get wrong indent (2)",
unchanged: [
'axios.interceptors.request.use(',
' config => {',
' // loading',
' window.store.loading++',
' let extraParams = {}',
' }',
')'
]
}
]
}, {
Expand Down Expand Up @@ -3157,4 +3182,4 @@ exports.test_data = {
output: "" //string or array of lines
}]
}]
};
};

0 comments on commit 268064b

Please sign in to comment.