Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseGoncalves committed Sep 11, 2014
2 parents ca6f764 + 56fac25 commit 597a68a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ python:
- "2.7"
- "3.2"
- "3.3"
- "3.4"

node_js:
- "0.8"
Expand Down
6 changes: 3 additions & 3 deletions js/lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@
}

if (token_type === 'TK_RESERVED' && token_text === 'function') {
if (in_array(flags.last_text, ['}', ';']) || (just_added_newline() && ! in_array(flags.last_text, ['{', ':', '=', ',']))) {
if (in_array(flags.last_text, ['}', ';']) || (just_added_newline() && ! in_array(flags.last_text, ['[', '{', ':', '=', ',']))) {
// make sure there is a nice clean space of at least one blank line
// before a new function definition
if ( ! just_added_blankline() && ! flags.had_comment) {
Expand Down Expand Up @@ -1585,7 +1585,7 @@
// http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1
// if there is a newline between -- or ++ and anything else we should preserve it.
if (input_wanted_newline && (token_text === '--' || token_text === '++')) {
print_newline();
print_newline(false, true);
}

// Allow line wrapping between operators
Expand All @@ -1605,7 +1605,7 @@
space_before = true;
}

if (last_type === 'TK_RESERVED') {
if (last_type === 'TK_RESERVED' || last_type === 'TK_END_EXPR') {
space_before = true;
}

Expand Down
6 changes: 6 additions & 0 deletions js/test/beautify-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
bt('{foo();++bar;}', '{\n foo();\n ++bar;\n}');
bt('{--bar;}', '{\n --bar;\n}');
bt('{++bar;}', '{\n ++bar;\n}');
bt('if(true)++a;','if (true) ++a;');
bt('if(true)\n++a;','if (true)\n ++a;');
bt('if(true)--a;','if (true) --a;');
bt('if(true)\n--a;','if (true)\n --a;');

// Handling of newlines around unary ++ and -- operators
bt('{foo\n++bar;}', '{\n foo\n ++bar;\n}');
Expand Down Expand Up @@ -1460,6 +1464,8 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
'function foo() {}\n\nfunction foo() {}'
);

bt('[\n function() {}\n]');



bt("if\n(a)\nb();", "if (a)\n b();");
Expand Down
6 changes: 3 additions & 3 deletions python/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,7 @@ def handle_word(self, token_text):
return

if self.token_type == 'TK_RESERVED' and token_text == 'function':
if self.flags.last_text in ['}', ';'] or (self.just_added_newline() and not self.flags.last_text in ['{', ':', '=', ',']):
if self.flags.last_text in ['}', ';'] or (self.just_added_newline() and not self.flags.last_text in ['[', '{', ':', '=', ',']):
# make sure there is a nice clean space of at least one blank line
# before a new function definition, except in arrays
if not self.just_added_blankline() and not self.flags.had_comment:
Expand Down Expand Up @@ -1361,7 +1361,7 @@ def handle_operator(self, token_text):
# http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1
# if there is a newline between -- or ++ and anything else we should preserve it.
if self.input_wanted_newline and (token_text == '--' or token_text == '++'):
self.append_newline()
self.append_newline(preserve_statement_flags = True)

# Allow line wrapping between operators in an expression
if self.last_type == 'TK_OPERATOR':
Expand All @@ -1380,7 +1380,7 @@ def handle_operator(self, token_text):
# ^^
space_before = True

if self.last_type == 'TK_RESERVED':
if self.last_type == 'TK_RESERVED' or self.last_type == 'TK_END_EXPR':
space_before = True

if self.flags.mode == MODE.BlockStatement and self.flags.last_text in ['{', ';']:
Expand Down
5 changes: 5 additions & 0 deletions python/jsbeautifier/tests/testjsbeautifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ def test_beautifier(self):
bt('{foo();++bar;}', '{\n foo();\n ++bar;\n}');
bt('{--bar;}', '{\n --bar;\n}');
bt('{++bar;}', '{\n ++bar;\n}');
bt('if(true)++a;','if (true) ++a;');
bt('if(true)\n++a;','if (true)\n ++a;');
bt('if(true)--a;','if (true) --a;');
bt('if(true)\n--a;','if (true)\n --a;');

# Handling of newlines around unary ++ and -- operators
bt('{foo\n++bar;}', '{\n foo\n ++bar;\n}');
Expand Down Expand Up @@ -1252,6 +1256,7 @@ def test_beautifier(self):
'function foo() {}\n\nfunction foo() {}'
);

bt('[\n function() {}\n]');


bt("if\n(a)\nb();", "if (a) b();");
Expand Down

0 comments on commit 597a68a

Please sign in to comment.