Fix #241, check if dot after newline, and change indentation

This commit is contained in:
John K. Paul 2013-05-02 17:36:27 -04:00
parent 55e111dd18
commit cb4ae314aa
4 changed files with 68 additions and 1 deletions

View File

@ -86,7 +86,7 @@
var input, output, token_text, token_type, last_type, last_last_text, indent_string;
var flags, previous_flags, flag_store;
var whitespace, wordchar, punct, parser_pos, line_starters, digits;
var prefix;
var prefix, dot_after_newline;
var input_wanted_newline;
var output_wrapped, output_space_before_token;
var input_length, n_newlines, whitespace_before_token;
@ -969,6 +969,10 @@
set_mode(MODE.ArrayLiteral);
indent();
}
if(dot_after_newline) {
dot_after_newline = false;
indent();
}
}
function handle_end_expr() {
@ -1091,6 +1095,10 @@
}
}
if(dot_after_newline && is_special_word(token_text)) {
dot_after_newline = false;
}
// if may be followed by else, or not
// Bare/inline ifs are tricky
// Need to unwind the modes correctly: if (a) if (b) c(); else d(); else e();
@ -1480,7 +1488,12 @@
allow_wrap_or_preserved_newline (flags.last_text === ')' && opt.break_chained_methods);
}
if (just_added_newline()) {
dot_after_newline = true;
}
print_token();
}
function handle_unknown() {

View File

@ -959,6 +959,28 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify)
test_fragment('xml=<a></b>\nc<b;', 'xml = <a></b>\nc<b;');
opts.e4x = false;
// START tests for issue 241
bt('obj\n' +
' .last({\n' +
' foo: 1,\n' +
' bar: 2\n' +
' });\n' +
'var test = 1;');
bt('obj\n' +
' .last(function() {\n' +
' var test;\n' +
' });\n' +
'var test = 1;');
bt('obj.first()\n' +
' .second()\n' +
' .last(function(err, response) {\n' +
' console.log(err);\n' +
' });');
// END tests for issue 241
Urlencoded.run_tests(sanitytest);

View File

@ -230,6 +230,7 @@ class Beautifier:
self.last_last_text = '' # pre-last token text
self.input = None
self.dot_after_newline = False
self.output = [] # formatted javascript gets built here
self.output_wrapped = False
self.output_space_before_token = False
@ -825,6 +826,9 @@ class Beautifier:
if self.token_text == '[':
self.set_mode(MODE.ArrayLiteral)
self.indent()
if self.dot_after_newline:
self.dot_after_newline = False
self.indent()
@ -930,6 +934,9 @@ class Beautifier:
self.append_newline()
self.flags.do_block = False
if self.dot_after_newline and self.is_special_word(token_text):
self.dot_after_newline = False
# if may be followed by else, or not
# Bare/inline ifs are tricky
# Need to unwind the modes correctly: if (a) if (b) c(); else d(); else e();
@ -1280,6 +1287,9 @@ class Beautifier:
self.allow_wrap_or_preserved_newline(token_text,
self.flags.last_text == ')' and self.opts.break_chained_methods)
if self.just_added_newline():
self.dot_after_newline = True
self.append_token(token_text)
def handle_unknown(self, token_text):

View File

@ -895,6 +895,28 @@ class TestJSBeautifier(unittest.TestCase):
test_fragment('xml=<a></b>\nc<b;', 'xml = <a></b>\nc<b;');
self.options.e4x = False
# START tests for issue 241
bt('obj\n' +
' .last({\n' +
' foo: 1,\n' +
' bar: 2\n' +
' });\n' +
'var test = 1;');
bt('obj\n' +
' .last(function() {\n' +
' var test;\n' +
' });\n' +
'var test = 1;');
bt('obj.first()\n' +
' .second()\n' +
' .last(function(err, response) {\n' +
' console.log(err);\n' +
' });');
# END tests for issue 241