Skip to content

Commit

Permalink
Rewrite declaration var, let, const indenting
Browse files Browse the repository at this point in the history
Fixes  #256, #430
  • Loading branch information
bitwiseman committed Apr 4, 2014
1 parent d9ee1cb commit 035363b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 100 deletions.
83 changes: 37 additions & 46 deletions js/lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@
var next_indent_level = 0;
if (flags_base) {
next_indent_level = flags_base.indentation_level;
next_indent_level += (flags_base.var_line && flags_base.var_line_reindented) ? 1 : 0;
if (!just_added_newline() &&
flags_base.line_indent_level > next_indent_level) {
next_indent_level = flags_base.line_indent_level;
Expand All @@ -212,9 +211,8 @@
parent: flags_base,
last_text: flags_base ? flags_base.last_text : '', // last token text
last_word: flags_base ? flags_base.last_word : '', // last 'TK_WORD' passed
var_line: false,
var_line_tainted: false,
var_line_reindented: false,
declaration_statement: false,
declaration_assignment: false,
in_html_comment: false,
multiline_frame: false,
if_block: false,
Expand Down Expand Up @@ -325,6 +323,10 @@
token_type = t[1];

if (token_type === 'TK_EOF') {
// Unwind any open statements
while (flags.mode === MODE.Statement) {
restore_mode();
}
break;
}

Expand Down Expand Up @@ -456,7 +458,7 @@
print_newline(false, true);

// Expressions and array literals already indent their contents.
if (!(is_array(flags.mode) || is_expression(flags.mode))) {
if (!(is_array(flags.mode) || is_expression(flags.mode) || flags.mode === MODE.Statement)) {
output_wrapped = true;
}
}
Expand All @@ -467,7 +469,7 @@
output_space_before_token = false;

if (!preserve_statement_flags) {
if (flags.last_text !== ';') {
if (flags.last_text !== ';' && flags.last_text !== ',' && flags.last_text !== '=') {
while (flags.mode === MODE.Statement && !flags.if_block && !flags.do_block) {
restore_mode();
}
Expand Down Expand Up @@ -499,7 +501,6 @@
}

print_indent_string(flags.indentation_level +
(flags.var_line && flags.var_line_reindented ? 1 : 0) +
(output_wrapped ? 1 : 0));
}
}
Expand Down Expand Up @@ -606,6 +607,9 @@
if (flag_store.length > 0) {
previous_flags = flags;
flags = flag_store.pop();
if (previous_flags.mode === MODE.Statement) {
remove_redundant_indentation(previous_flags);
}
}
}

Expand All @@ -616,23 +620,26 @@

function start_of_statement() {
if (
((last_type === 'TK_RESERVED' && flags.last_text === 'do') ||
(last_type === 'TK_RESERVED' && in_array(flags.last_text, ['var', 'let', 'const']) && token_type === 'TK_WORD') ||
((last_type === 'TK_RESERVED' && flags.last_text === 'do') ||
(last_type === 'TK_RESERVED' && flags.last_text === 'else' && !(token_type === 'TK_RESERVED' && token_text === 'if')) ||
(last_type === 'TK_END_EXPR' && (previous_flags.mode === MODE.ForInitializer || previous_flags.mode === MODE.Conditional)))) {

set_mode(MODE.Statement);
indent();

if (last_type === 'TK_RESERVED' && in_array(flags.last_text, ['var', 'let', 'const']) && token_type === 'TK_WORD') {
flags.declaration_statement = true;
}

// Issue #276:
// If starting a new statement with [if, for, while, do], push to a new line.
// if (a) if (b) if(c) d(); else e(); else f();
allow_wrap_or_preserved_newline(
token_type === 'TK_RESERVED' && in_array(token_text, ['do', 'for', 'if', 'while']));

set_mode(MODE.Statement);
// Issue #275:
// If starting on a newline, all of a statement should be indented.
// if not, use line wrapping logic for indent.
if (just_added_newline()) {
indent();
output_wrapped = false;
}
output_wrapped = false;

return true;
}
return false;
Expand Down Expand Up @@ -798,7 +805,9 @@
return [c, 'TK_WORD'];
}

if (last_type !== 'TK_DOT' && in_array(c, reserved_words)) {
if (!(last_type === 'TK_DOT' ||
(last_type === 'TK_RESERVED' && in_array(flags.last_text, ['set', 'get'])))
&& in_array(c, reserved_words)) {
if (c === 'in') { // hack for 'in' operator
return [c, 'TK_OPERATOR'];
}
Expand Down Expand Up @@ -1207,7 +1216,7 @@
(last_type === 'TK_RESERVED' && is_special_word(flags.last_text) && flags.last_text !== 'else'))) {
output_space_before_token = true;
} else {
print_newline();
print_newline(false, true);
}
} else { // collapse
if (last_type !== 'TK_OPERATOR' && last_type !== 'TK_START_EXPR') {
Expand Down Expand Up @@ -1267,7 +1276,7 @@
} else if (input_wanted_newline && !is_expression(flags.mode) &&
(last_type !== 'TK_OPERATOR' || (flags.last_text === '--' || flags.last_text === '++')) &&
last_type !== 'TK_EQUALS' &&
(opt.preserve_newlines || !(last_type === 'TK_RESERVED' && in_array(flags.last_text, ['var', 'let', 'const'])))) {
(opt.preserve_newlines || !(last_type === 'TK_RESERVED' && in_array(flags.last_text, ['var', 'let', 'const', 'set', 'get'])))) {

print_newline();
}
Expand Down Expand Up @@ -1314,9 +1323,6 @@
}

if (token_type === 'TK_RESERVED' && token_text === 'function') {
if (flags.var_line && last_type !== 'TK_EQUALS') {
flags.var_line_reindented = true;
}
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
Expand Down Expand Up @@ -1415,14 +1421,10 @@
// no newline for } else if {
output_space_before_token = true;
} else {
flags.var_line = false;
flags.var_line_reindented = false;
print_newline();
}
}
} else if (token_type === 'TK_RESERVED' && in_array(token_text, line_starters) && flags.last_text !== ')') {
flags.var_line = false;
flags.var_line_reindented = false;
print_newline();
}
} else if (is_array(flags.mode) && flags.last_text === ',' && last_last_text === '}') {
Expand All @@ -1433,12 +1435,6 @@
print_token();
flags.last_word = token_text;

if (token_type === 'TK_RESERVED' && in_array(token_text, ['var', 'let', 'const'])) {
flags.var_line = true;
flags.var_line_reindented = false;
flags.var_line_tainted = false;
}

if (token_type === 'TK_RESERVED' && token_text === 'do') {
flags.do_block = true;
}
Expand All @@ -1458,8 +1454,6 @@
restore_mode();
}
print_token();
flags.var_line = false;
flags.var_line_reindented = false;
if (flags.mode === MODE.ObjectLiteral) {
// if we're in OBJECT mode and see a semicolon, its invalid syntax
// recover back to treating this as a BLOCK
Expand All @@ -1485,31 +1479,28 @@
}

function handle_equals() {
if (flags.var_line) {
if (flags.declaration_statement) {
// just got an '=' in a var-line, different formatting/line-breaking, etc will now be done
flags.var_line_tainted = true;
flags.declaration_assignment = true;
}
output_space_before_token = true;
print_token();
output_space_before_token = true;
}

function handle_comma() {
if (flags.var_line) {
if (is_expression(flags.mode) || last_type === 'TK_END_BLOCK') {
if (flags.declaration_statement) {
if (is_expression(flags.parent.mode) ||
(last_type === 'TK_END_BLOCK' && previous_flags.mode === MODE.ObjectLiteral)) {
// do not break on comma, for(var a = 1, b = 2)
flags.var_line_tainted = false;
}

if (flags.var_line) {
flags.var_line_reindented = true;
flags.declaration_assignment = false;
}

print_token();

if (flags.var_line_tainted) {
flags.var_line_tainted = false;
print_newline();
if (flags.declaration_assignment) {
flags.declaration_assignment = false;
print_newline(false, true);
} else {
output_space_before_token = true;
}
Expand Down
11 changes: 7 additions & 4 deletions js/test/beautify-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
bt('if (a) a()\nnewline()');
bt('a=typeof(x)', 'a = typeof(x)');

bt('var a = function() {\n return null;\n},\n b = false;');
bt('var a = function() {\n return null;\n },\n b = false;');

bt('var a = function() {\n func1()\n}');
bt('var a = function() {\n func1()\n}\nvar b = function() {\n func2()\n}');
Expand All @@ -450,7 +450,7 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
bt('switch(x){case -1:break;case !y:break;}',
'switch (x) {\n case -1:\n break;\n case !y:\n break;\n}');
test_fragment("// comment 2\n(function()", "// comment 2\n(function()"); // typical greasemonkey start
bt("var a2, b2, c2, d2 = 0, c = function() {}, d = '';", "var a2, b2, c2, d2 = 0,\n c = function() {}, d = '';");
bt("var a2, b2, c2, d2 = 0, c = function() {}, d = '';", "var a2, b2, c2, d2 = 0,\n c = function() {},\n d = '';");
bt("var a2, b2, c2, d2 = 0, c = function() {},\nd = '';", "var a2, b2, c2, d2 = 0,\n c = function() {},\n d = '';");
bt('var o2=$.extend(a);function(){alert(x);}', 'var o2 = $.extend(a);\n\nfunction() {\n alert(x);\n}');

Expand Down Expand Up @@ -509,7 +509,7 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
);

opts.preserve_newlines = true;
bt('var\na=do_preserve_newlines;', 'var\na = do_preserve_newlines;');
bt('var\na=do_preserve_newlines;', 'var\n a = do_preserve_newlines;');
bt('// a\n// b\n\n// c\n// d');
bt('if (foo) // comment\n{\n bar();\n}');

Expand Down Expand Up @@ -599,7 +599,9 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
bt('a: do {} while (); xxx', 'a: do {} while ();\nxxx');
bt('var a = new function();');
bt('var a = new function() {};');
bt('var a = new function a()\n {};');
bt('var a = new function()\n{};', 'var a = new function() {};');
bt('var a = new function a()\n{};');
bt('var a = new function a()\n {},\n b = new function b()\n {};');
test_fragment('new function');
bt("foo({\n 'a': 1\n},\n10);",
"foo(\n {\n 'a': 1\n },\n 10);");
Expand Down Expand Up @@ -869,6 +871,7 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
bt("{\n var a = set\n foo();\n}");
bt("var x = {\n get function()\n}");
bt("var x = {\n set function()\n}");
bt("var x = set\n\na() {}", "var x = set\n\n a() {}");
bt("var x = set\n\nfunction() {}", "var x = set\n\n function() {}");

bt('<!-- foo\nbar();\n-->');
Expand Down
Loading

0 comments on commit 035363b

Please sign in to comment.