Skip to content

Commit

Permalink
Do not remove indent for condtional blocks
Browse files Browse the repository at this point in the history
Fixes #298
  • Loading branch information
bitwiseman committed Oct 2, 2014
1 parent c13efe3 commit baf2a34
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
29 changes: 16 additions & 13 deletions js/lib/beautify.js
Expand Up @@ -163,6 +163,16 @@
return beautifier.beautify();
}

var MODE = {
BlockStatement: 'BlockStatement', // 'BLOCK'
Statement: 'Statement', // 'STATEMENT'
ObjectLiteral: 'ObjectLiteral', // 'OBJECT',
ArrayLiteral: 'ArrayLiteral', //'[EXPRESSION]',
ForInitializer: 'ForInitializer', //'(FOR-EXPRESSION)',
Conditional: 'Conditional', //'(COND-EXPRESSION)',
Expression: 'Expression' //'(EXPRESSION)'
};

function Beautifier(js_source_text, options) {
"use strict";
var output
Expand All @@ -173,20 +183,9 @@
var flags, previous_flags, flag_store;
var prefix;

var handlers, MODE, opt;
var handlers, opt;
var baseIndentString = '';


MODE = {
BlockStatement: 'BlockStatement', // 'BLOCK'
Statement: 'Statement', // 'STATEMENT'
ObjectLiteral: 'ObjectLiteral', // 'OBJECT',
ArrayLiteral: 'ArrayLiteral', //'[EXPRESSION]',
ForInitializer: 'ForInitializer', //'(FOR-EXPRESSION)',
Conditional: 'Conditional', //'(COND-EXPRESSION)',
Expression: 'Expression' //'(EXPRESSION)'
};

handlers = {
'TK_START_EXPR': handle_start_expr,
'TK_END_EXPR': handle_end_expr,
Expand Down Expand Up @@ -1345,7 +1344,11 @@
// after wrap points are calculated
// These issues are minor compared to ugly indentation.

if (frame.multiline_frame) return;
if (frame.multiline_frame ||
frame.mode === MODE.ForInitializer ||
frame.mode === MODE.Conditional) {
return;
}

// remove one indent from each line inside this section
var index = frame.start_line_index;
Expand Down
9 changes: 9 additions & 0 deletions js/test/beautify-tests.js
Expand Up @@ -1817,6 +1817,15 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
'};');
// END tests for issue 508

// START tests for issue 298
bt("'use strict';\n" +
"if ([].some(function() {\n" +
" return false;\n" +
" })) {\n" +
" console.log('hello');\n" +
"}");
// END tests for issue 298

bt('var a=1,b={bang:2},c=3;',
'var a = 1,\n b = {\n bang: 2\n },\n c = 3;');
bt('var a={bing:1},b=2,c=3;',
Expand Down
2 changes: 1 addition & 1 deletion python/jsbeautifier/__init__.py
Expand Up @@ -1244,7 +1244,7 @@ def remove_redundant_indentation(self, frame):
# after wrap points are calculated
# These issues are minor compared to ugly indentation.

if frame.multiline_frame:
if frame.multiline_frame or frame.mode == MODE.ForInitializer or frame.mode == MODE.Conditional:
return

# remove one indent from each line inside this section
Expand Down
9 changes: 9 additions & 0 deletions python/jsbeautifier/tests/testjsbeautifier.py
Expand Up @@ -1683,6 +1683,15 @@ def test_beautifier(self):
' d: function() {}\n' +
'};');
# END tests for issue 508

# START tests for issue 298
bt("'use strict';\n" +
"if ([].some(function() {\n" +
" return false;\n" +
" })) {\n" +
" console.log('hello');\n" +
"}");
# END tests for issue 298

bt('var a=1,b={bang:2},c=3;',
'var a = 1,\n b = {\n bang: 2\n },\n c = 3;');
Expand Down

0 comments on commit baf2a34

Please sign in to comment.