Skip to content

Commit

Permalink
Drop Crockford's switch indentation (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
einars committed Oct 11, 2012
1 parent 595d812 commit b20be94
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 34 deletions.
28 changes: 8 additions & 20 deletions beautify.js
Expand Up @@ -90,7 +90,6 @@ function js_beautify(js_source_text, options) {
opt_jslint_happy = options.jslint_happy === 'undefined' ? false : options.jslint_happy,
opt_keep_array_indentation = typeof options.keep_array_indentation === 'undefined' ? false : options.keep_array_indentation,
opt_space_before_conditional = typeof options.space_before_conditional === 'undefined' ? true : options.space_before_conditional,
opt_indent_case = typeof options.indent_case === 'undefined' ? false : options.indent_case,
opt_unescape_strings = typeof options.unescape_strings === 'undefined' ? false : options.unescape_strings;

just_added_newline = false;
Expand Down Expand Up @@ -172,9 +171,6 @@ function js_beautify(js_source_text, options) {
if (flags.var_line && flags.var_line_reindented) {
output.push(indent_string); // skip space-stuffing, if indenting with a tab
}
if (flags.case_body) {
output.push(indent_string);
}
}


Expand Down Expand Up @@ -233,7 +229,7 @@ function js_beautify(js_source_text, options) {
case_body: false, // the indented case-action block
eat_next_space: false,
indentation_baseline: -1,
indentation_level: (flags ? flags.indentation_level + (flags.case_body ? 1 : 0) + ((flags.var_line && flags.var_line_reindented) ? 1 : 0) : 0),
indentation_level: (flags ? flags.indentation_level + ((flags.var_line && flags.var_line_reindented) ? 1 : 0) : 0),
ternary_depth: 0
};
}
Expand Down Expand Up @@ -979,23 +975,16 @@ function js_beautify(js_source_text, options) {
}

if (token_text === 'case' || (token_text === 'default' && flags.in_case_statement)) {
if (last_text === ':' || flags.case_body) {
print_newline();
if (flags.case_body) {
// switch cases following one another
flags.indentation_level--;
flags.case_body = false;
remove_indent();
} else {
// case statement starts in the same line where switch
if (!opt_indent_case) {
flags.indentation_level--;
}
print_newline();
if (!opt_indent_case) {
flags.indentation_level++;
}
}
print_token();
flags.in_case = true;
flags.in_case_statement = true;
flags.case_body = false;
break;
}

Expand Down Expand Up @@ -1190,10 +1179,9 @@ function js_beautify(js_source_text, options) {
}

if (token_text === ':' && flags.in_case) {
if (opt_indent_case) {
flags.case_body = true;
}
print_token(); // colon really asks for separate treatment
flags.case_body = true;
indent();
print_token();
print_newline();
flags.in_case = false;
break;
Expand Down
14 changes: 8 additions & 6 deletions python/jsbeautifier/__init__.py
Expand Up @@ -84,6 +84,7 @@ def __init__(self, mode):
self.chain_extra_indentation = 0
self.in_case = False
self.in_case_statement = False
self.case_body = False
self.eat_next_space = False
self.indentation_baseline = -1
self.indentation_level = 0
Expand Down Expand Up @@ -853,12 +854,11 @@ def handle_word(self, token_text):
return

if token_text == 'case' or (token_text == 'default' and self.flags.in_case_statement):
if self.last_text == ':':
self.remove_indent()
else:
self.flags.indentation_level -= 1
self.append_newline()
self.flags.indentation_level += 1
self.append_newline()
if self.flags.case_body:
self.remove_indent();
self.flags.case_body = False
self.flags.indentation_level -= 1;
self.append(token_text)
self.flags.in_case = True
self.flags.in_case_statement = True
Expand Down Expand Up @@ -1036,6 +1036,8 @@ def handle_operator(self, token_text):


if token_text == ':' and self.flags.in_case:
self.flags.case_body = True
self.indent();
self.append(token_text)
self.append_newline()
self.flags.in_case = False
Expand Down
8 changes: 4 additions & 4 deletions python/jsbeautifier/tests/testjsbeautifier.py
Expand Up @@ -99,8 +99,8 @@ def test_beautifier(self):
bt('(xx)()'); # magic function call
bt('a[1]()'); # another magic function call
bt('if(a){b();}else if(c) foo();', "if (a) {\n b();\n} else if (c) foo();");
bt('switch(x) {case 0: case 1: a(); break; default: break}', "switch (x) {\ncase 0:\ncase 1:\n a();\n break;\ndefault:\n break\n}");
bt('switch(x){case -1:break;case !y:break;}', 'switch (x) {\ncase -1:\n break;\ncase !y:\n break;\n}');
bt('switch(x) {case 0: case 1: a(); break; default: break}', "switch (x) {\n case 0:\n case 1:\n a();\n break;\n default:\n break\n}");
bt('switch(x){case -1:break;case !y:break;}', 'switch (x) {\n case -1:\n break;\n case !y:\n break;\n}');
bt('a !== b');
bt('if (a) b(); else c();', "if (a) b();\nelse c();");
bt("// comment\n(function something() {})"); # typical greasemonkey start
Expand Down Expand Up @@ -436,9 +436,9 @@ def test_beautifier(self):
bt('<!-- dont crash')
bt('for () /abc/.test()')
bt('if (k) /aaa/m.test(v) && l();')
bt('switch (true) {\ncase /swf/i.test(foo):\n bar();\n}')
bt('switch (true) {\n case /swf/i.test(foo):\n bar();\n}')
bt('createdAt = {\n type: Date,\n default: Date.now\n}')
bt('switch (createdAt) {\ncase a:\n Date,\ndefault:\n Date.now\n}')
bt('switch (createdAt) {\n case a:\n Date,\n default:\n Date.now\n}')

bt('foo = {\n x: y, // #44\n w: z // #44\n}');
bt('return function();')
Expand Down
8 changes: 4 additions & 4 deletions tests/beautify-tests.js
Expand Up @@ -143,8 +143,8 @@ function run_beautifier_tests(test_obj)
bt('(xx)()'); // magic function call
bt('a[1]()'); // another magic function call
bt('if(a){b();}else if(c) foo();', "if (a) {\n b();\n} else if (c) foo();");
bt('switch(x) {case 0: case 1: a(); break; default: break}', "switch (x) {\ncase 0:\ncase 1:\n a();\n break;\ndefault:\n break\n}");
bt('switch(x){case -1:break;case !y:break;}', 'switch (x) {\ncase -1:\n break;\ncase !y:\n break;\n}');
bt('switch(x) {case 0: case 1: a(); break; default: break}', "switch (x) {\n case 0:\n case 1:\n a();\n break;\n default:\n break\n}");
bt('switch(x){case -1:break;case !y:break;}', 'switch (x) {\n case -1:\n break;\n case !y:\n break;\n}');
bt('a !== b');
bt('if (a) b(); else c();', "if (a) b();\nelse c();");
bt("// comment\n(function something() {})"); // typical greasemonkey start
Expand Down Expand Up @@ -482,9 +482,9 @@ function run_beautifier_tests(test_obj)
bt('<!-- dont crash');
bt('for () /abc/.test()');
bt('if (k) /aaa/m.test(v) && l();');
bt('switch (true) {\ncase /swf/i.test(foo):\n bar();\n}');
bt('switch (true) {\n case /swf/i.test(foo):\n bar();\n}');
bt('createdAt = {\n type: Date,\n default: Date.now\n}');
bt('switch (createdAt) {\ncase a:\n Date,\ndefault:\n Date.now\n}');
bt('switch (createdAt) {\n case a:\n Date,\n default:\n Date.now\n}');
opts.space_before_conditional = false;
bt('if(a) b()');

Expand Down

0 comments on commit b20be94

Please sign in to comment.