Skip to content

Commit

Permalink
Add support for opting out of switch-case alignment when using jslint…
Browse files Browse the repository at this point in the history
…_happy. Closes beautifier#372
  • Loading branch information
gabrielmaldi committed Jun 10, 2014
1 parent 219a44e commit 4831333
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 61 deletions.
38 changes: 23 additions & 15 deletions js/lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,16 @@
jslint_happy (default false) - if true, then jslint-stricter mode is enforced.
jslint_happy !jslint_happy
jslint_happy !jslint_happy
---------------------------------
function () function()
function () function()
switch () { switch () {
case 1: case 1:
break; break;
} }
jslint_happy_align_switch_case (default true) - when jslint_happy is true, should case be aligned to switch,
brace_style (default "collapse") - "collapse" | "expand" | "end-expand"
put braces on the same line as control statements (default), or put braces on own line (Allman / ANSI style), or just put end braces on own line.
Expand Down Expand Up @@ -266,6 +273,7 @@
opt.space_in_paren = (options.space_in_paren === undefined) ? false : options.space_in_paren;
opt.space_in_empty_paren = (options.space_in_empty_paren === undefined) ? false : options.space_in_empty_paren;
opt.jslint_happy = (options.jslint_happy === undefined) ? false : options.jslint_happy;
opt.jslint_happy_align_switch_case = (options.jslint_happy_align_switch_case === undefined) ? true : options.jslint_happy_align_switch_case;
opt.keep_array_indentation = (options.keep_array_indentation === undefined) ? false : options.keep_array_indentation;
opt.space_before_conditional = (options.space_before_conditional === undefined) ? true : options.space_before_conditional;
opt.unescape_strings = (options.unescape_strings === undefined) ? false : options.unescape_strings;
Expand Down Expand Up @@ -618,12 +626,12 @@
(last_type === 'TK_RESERVED' && flags.last_text === 'return' && !input_wanted_newline) ||
(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)) ||
(last_type === 'TK_WORD' && flags.mode === MODE.BlockStatement
&& !flags.in_case
&& !(token_text === '--' || token_text === '++')
(last_type === 'TK_WORD' && flags.mode === MODE.BlockStatement
&& !flags.in_case
&& !(token_text === '--' || token_text === '++')
&& token_type !== 'TK_WORD' && token_type !== 'TK_RESERVED') ||
(flags.mode === MODE.ObjectLiteral && flags.last_text === ':' && flags.ternary_depth === 0)
(flags.mode === MODE.ObjectLiteral && flags.last_text === ':' && flags.ternary_depth === 0)

) {

set_mode(MODE.Statement);
Expand Down Expand Up @@ -1312,7 +1320,7 @@

if (token_type === 'TK_RESERVED' && (token_text === 'case' || (token_text === 'default' && flags.in_case_statement))) {
print_newline();
if (flags.case_body || opt.jslint_happy) {
if (flags.case_body || (opt.jslint_happy && opt.jslint_happy_align_switch_case)) {
// switch cases following one another
deindent();
flags.case_body = false;
Expand Down Expand Up @@ -1380,7 +1388,7 @@
prefix = 'SPACE';
} else if (last_type === 'TK_STRING') {
prefix = 'NEWLINE';
} else if (last_type === 'TK_RESERVED' || last_type === 'TK_WORD' ||
} else if (last_type === 'TK_RESERVED' || last_type === 'TK_WORD' ||
(flags.last_text === '*' && last_last_text === 'function')) {
prefix = 'SPACE';
} else if (last_type === 'TK_START_BLOCK') {
Expand Down Expand Up @@ -1484,7 +1492,7 @@
if (start_of_statement()) {
// The conditional starts the statement if appropriate.
}

if (flags.declaration_statement) {
// just got an '=' in a var-line, different formatting/line-breaking, etc will now be done
flags.declaration_assignment = true;
Expand Down Expand Up @@ -1513,7 +1521,7 @@
}

print_token();
if (flags.mode === MODE.ObjectLiteral ||
if (flags.mode === MODE.ObjectLiteral ||
(flags.mode === MODE.Statement && flags.parent.mode === MODE.ObjectLiteral)) {
if (flags.mode === MODE.Statement) {
restore_mode();
Expand All @@ -1523,7 +1531,7 @@
// EXPR or DO_BLOCK
output_space_before_token = true;
}

}

function handle_operator() {
Expand All @@ -1533,11 +1541,11 @@
(last_type === 'TK_WORD' || last_type === 'TK_RESERVED')){
flags.mode = MODE.ObjectLiteral;
}

if (start_of_statement()) {
// The conditional starts the statement if appropriate.
}

var space_before = true;
var space_after = true;
if (last_type === 'TK_RESERVED' && is_special_word(flags.last_text)) {
Expand Down Expand Up @@ -1672,7 +1680,7 @@
if (start_of_statement()) {
// The conditional starts the statement if appropriate.
}

if (last_type === 'TK_RESERVED' && is_special_word(flags.last_text)) {
output_space_before_token = true;
} else {
Expand Down
105 changes: 59 additions & 46 deletions js/test/beautify-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,20 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
test_fragment("// comment 1\n(function()", "// comment 1\n(function ()"); // typical greasemonkey start
bt('var o1=$.extend(a);function(){alert(x);}', 'var o1 = $.extend(a);\n\nfunction () {\n alert(x);\n}');
bt('function* () {\n yield 1;\n}');


opts.jslint_happy_align_switch_case = false;

bt('a=typeof(x)', 'a = typeof (x)');
bt('x();\n\nfunction(){}', 'x();\n\nfunction () {}');
bt('function () {\n var a, b, c, d, e = [],\n f;\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}');
test_fragment("// comment 1\n(function()", "// comment 1\n(function ()"); // typical greasemonkey start
bt('var o1=$.extend(a);function(){alert(x);}', 'var o1 = $.extend(a);\n\nfunction () {\n alert(x);\n}');
bt('function* () {\n yield 1;\n}');

opts.jslint_happy = false;

bt('switch(x) {case 0: case 1: a(); break; default: break}',
Expand All @@ -466,7 +479,7 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
bt('function*() {\n yield 1;\n}');

bt('function* x() {\n yield 1;\n}');

bt('{"x":[{"a":1,"b":3},7,8,8,8,8,{"b":99},{"a":11}]}', '{\n "x": [{\n "a": 1,\n "b": 3\n },\n 7, 8, 8, 8, 8, {\n "b": 99\n }, {\n "a": 11\n }\n ]\n}');

bt('{"1":{"1a":"1b"},"2"}', '{\n "1": {\n "1a": "1b"\n },\n "2"\n}');
Expand Down Expand Up @@ -1314,10 +1327,10 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
'this.oa = new OAuth(_requestToken, _accessToken, consumer_key);');
bt('foo = {\n x: y, // #44\n w: z // #44\n}');
bt('switch (x) {\n case "a":\n // comment on newline\n break;\n case "b": // comment on same line\n break;\n}');
bt('this.type =\n this.options =\n // comment\n this.enabled null;',
bt('this.type =\n this.options =\n // comment\n this.enabled null;',
'this.type = this.options =\n // comment\n this.enabled null;');
bt('someObj\n .someFunc1()\n // This comment should not break the indent\n .someFunc2();',
'someObj.someFunc1()\n // This comment should not break the indent\n .someFunc2();');
'someObj.someFunc1()\n // This comment should not break the indent\n .someFunc2();');

bt('if (true ||\n!true) return;', 'if (true || !true) return;');

Expand Down Expand Up @@ -1392,7 +1405,7 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
bt('switch (x) {\n case "a":\n // comment on newline\n break;\n case "b": // comment on same line\n break;\n}');
bt('this.type =\n this.options =\n // comment\n this.enabled null;');
bt('someObj\n .someFunc1()\n // This comment should not break the indent\n .someFunc2();');

bt('if (true ||\n!true) return;', 'if (true ||\n !true) return;');

// these aren't ready yet.
Expand Down Expand Up @@ -1701,24 +1714,24 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
'<li>\n' +
' content\n' +
'</li>');

// START tests for issue 453
bth('<script type="text/unknown"><div></div></script>',
'<script type="text/unknown">\n' +
' <div></div>\n' +
bth('<script type="text/unknown"><div></div></script>',
'<script type="text/unknown">\n' +
' <div></div>\n' +
'</script>');
bth('<script type="text/javascript"><div></div></script>',
'<script type="text/javascript">\n' +
' < div > < /div>\n' +
bth('<script type="text/javascript"><div></div></script>',
'<script type="text/javascript">\n' +
' < div > < /div>\n' +
'</script>');
bth('<script><div></div></script>',
'<script>\n' +
' < div > < /div>\n' +
bth('<script><div></div></script>',
'<script>\n' +
' < div > < /div>\n' +
'</script>');
bth('<script type="text/javascript">var foo = "bar";</script>',
'<script type="text/javascript">\n' +
' var foo = "bar";\n' +
'</script>');
bth('<script type="text/javascript">var foo = "bar";</script>',
'<script type="text/javascript">\n' +
' var foo = "bar";\n' +
'</script>');
bth('<script type="application/javascript">var foo = "bar";</script>',
'<script type="application/javascript">\n' +
' var foo = "bar";\n' +
Expand All @@ -1739,37 +1752,37 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
'<script type="text/javascript1.5">\n' +
' var foo = "bar";\n' +
'</script>');
bth('<script>var foo = "bar";</script>',
'<script>\n' +
' var foo = "bar";\n' +
'</script>');
bth('<style type="text/unknown"><tag></tag></style>',
'<style type="text/unknown">\n' +
' <tag></tag>\n' +
'</style>');
bth('<style type="text/css"><tag></tag></style>',
'<style type="text/css">\n' +
' <tag></tag>\n' +
'</style>');
bth('<style><tag></tag></style>',
'<style>\n' +
' <tag></tag>\n' +
'</style>');
bth('<style type="text/css">.selector {font-size:12px;}</style>',
'<style type="text/css">\n' +
bth('<script>var foo = "bar";</script>',
'<script>\n' +
' var foo = "bar";\n' +
'</script>');

bth('<style type="text/unknown"><tag></tag></style>',
'<style type="text/unknown">\n' +
' <tag></tag>\n' +
'</style>');
bth('<style type="text/css"><tag></tag></style>',
'<style type="text/css">\n' +
' <tag></tag>\n' +
'</style>');
bth('<style><tag></tag></style>',
'<style>\n' +
' <tag></tag>\n' +
'</style>');
bth('<style type="text/css">.selector {font-size:12px;}</style>',
'<style type="text/css">\n' +
' .selector {\n' +
' font-size: 12px;\n' +
' }\n'+
' font-size: 12px;\n' +
' }\n'+
'</style>');
bth('<style>.selector {font-size:12px;}</style>',
'<style>\n' +
bth('<style>.selector {font-size:12px;}</style>',
'<style>\n' +
' .selector {\n' +
' font-size: 12px;\n' +
' }\n'+
'</style>');
' font-size: 12px;\n' +
' }\n'+
'</style>');
// END tests for issue 453

// Tests that don't pass, but probably should.
// bth('<div><span>content</span></div>');

Expand Down

0 comments on commit 4831333

Please sign in to comment.