Skip to content

Commit

Permalink
Alternate brace style patch by Chris J. Shull
Browse files Browse the repository at this point in the history
  • Loading branch information
einars committed Mar 23, 2011
1 parent 084a997 commit 0d22b9f
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 29 deletions.
16 changes: 11 additions & 5 deletions beautify-cl.js
Expand Up @@ -4,6 +4,7 @@
----------------------------------------
Written by Patrick Hof, <patrickhof@web.de>
"End braces on own line" added by Chris J. Shull, <chrisjshull@gmail.com>
This script is to be run with Rhino[1], the JavaScript Engine written in Java,
on the command line.
Expand All @@ -22,7 +23,8 @@ function print_usage() {
print("Reads from standard input if no file or URL is specified.\n");
print("Options:");
print("-i NUM\tIndent size (1 for TAB)");
print("-b\tPut braces on own line (Allman / ANSI style)");
print("-e\tPut end braces on own line");
print("-b [ collapse | expand | end-expand ]\t 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");
print("-a\tIndent arrays");
print("-n\tPreserve newlines");
print("-p\tJSLint-pedantic mode, currently only adds space between \"function ()\"");
Expand All @@ -44,7 +46,10 @@ function parse_opts(args) {
options.indent = args.shift();
break;
case "-b":
options.braces_on_own_line = true;
if (!args[0] || args[0].substr(0, 1) == '-')
options.braces_on_own_line = true;
else
options.brace_style = args.shift();
break;
case "-a":
options.keep_array_indentation = false;
Expand Down Expand Up @@ -76,7 +81,7 @@ function parse_opts(args) {


function do_js_beautify() {
var js_source = '';
var js_source = '';
if (options.source) { // Check if source argument is an URL
if (options.source.substring(0, 4) === 'http') {
js_source = readUrl(options.source);
Expand All @@ -88,7 +93,7 @@ function do_js_beautify() {
importPackage(java.lang);
var stdin = new BufferedReader(new InputStreamReader(System['in']));
var lines = [];

// read stdin buffer until EOF
while (stdin.ready()) {
lines.push(stdin.readLine());
Expand Down Expand Up @@ -117,7 +122,8 @@ function do_js_beautify() {
preserve_newlines: preserve_newlines,
space_after_anon_function: options.jslint_pedantic,
keep_array_indentation: options.keep_array_indentation,
braces_on_own_line: options.braces_on_own_line
braces_on_own_line: options.braces_on_own_line,
brace_style: options.brace_style
});
}
return result;
Expand Down
30 changes: 19 additions & 11 deletions beautify.js
Expand Up @@ -9,6 +9,7 @@
http://jsbeautifier.org/
Originally converted to javascript by Vital, <vital76@gmail.com>
"End braces on own line" added by Chris J. Shull, <chrisjshull@gmail.com>
You are free to use this in any way you want, in case you find this useful or working for you.
Expand All @@ -24,8 +25,9 @@
indent_level (default 0) — initial indentation level, you probably won't need this ever,
space_after_anon_function (default false) — if true, then space is added between "function ()"
(jslint is happy about this); if false, then the common "function()" output is used.
braces_on_own_line (default false) - ANSI / Allman brace style, each opening/closing brace gets its own line.
(jslint is happy about this); if false, then the common "function()" output is used,
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.
e.g
Expand All @@ -46,7 +48,14 @@ function js_beautify(js_source_text, options) {

// Some interpreters have unexpected results with foo = baz || bar;
options = options ? options : {};
var opt_braces_on_own_line = options.braces_on_own_line ? options.braces_on_own_line : false;

var opt_brace_style;
if (options.braces_on_own_line != undefined) { //graceful handling of depricated option
opt_brace_style = options.braces_on_own_line ? "expand" : "collapse";
}
opt_brace_style = options.brace_style ? options.brace_style : (opt_brace_style ? opt_brace_style : "collapse");

//var opt_braces_on_own_line = options.braces_on_own_line ? options.braces_on_own_line : false;
var opt_indent_size = options.indent_size ? options.indent_size : 4;
var opt_indent_char = options.indent_char ? options.indent_char : ' ';
var opt_preserve_newlines = typeof options.preserve_newlines === 'undefined' ? true : options.preserve_newlines;
Expand Down Expand Up @@ -719,9 +728,9 @@ function js_beautify(js_source_text, options) {
} else {
set_mode('BLOCK');
}
if (opt_braces_on_own_line) {
if (opt_brace_style=="expand") {
if (last_type !== 'TK_OPERATOR') {
if (last_text === 'return' || last_text === '=') {
if (last_text == 'return') {
print_single_space();
} else {
print_newline(true);
Expand Down Expand Up @@ -755,10 +764,8 @@ function js_beautify(js_source_text, options) {

case 'TK_END_BLOCK':
restore_mode();
if (opt_braces_on_own_line) {
if (last_text !== '{') {
print_newline();
}
if (opt_brace_style=="expand") {
print_newline();
print_token();
} else {
if (last_type === 'TK_START_BLOCK') {
Expand Down Expand Up @@ -830,10 +837,11 @@ function js_beautify(js_source_text, options) {
prefix = 'NONE';

if (last_type === 'TK_END_BLOCK') {

if (!in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) {
prefix = 'NEWLINE';
} else {
if (opt_braces_on_own_line) {
if (opt_brace_style=="expand" || opt_brace_style=="end-expand") {
prefix = 'NEWLINE';
} else {
prefix = 'SPACE';
Expand All @@ -859,7 +867,7 @@ function js_beautify(js_source_text, options) {
flags.if_line = false;
}
if (in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) {
if (last_type !== 'TK_END_BLOCK' || opt_braces_on_own_line) {
if (last_type !== 'TK_END_BLOCK' || opt_brace_style=="expand" || opt_brace_style=="end-expand") {
print_newline();
} else {
trim_output(true);
Expand Down
22 changes: 15 additions & 7 deletions index.html
Expand Up @@ -2,6 +2,7 @@
<!--
(c) 2006-2010: Einar Lielmanis, einars@jsbeautifier.org
"End braces on own line" added by Chris J. Shull, chrisjshull@gmail.com
-->
<html>
Expand All @@ -17,13 +18,16 @@
add_onload_function(function() {

var tabsize = get_var('tabsize');
var braces_on_own_line = get_var('braces');
var brace_style = get_var('braces');
var end_braces_on_own_line = get_var('endbraces');
var c;
if (tabsize) {
document.getElementById('tabsize').value = tabsize;
}
if (braces_on_own_line) {
document.getElementById('braces-on-own-line').checked = 'checked';
if (brace_style) {
if (brace_style===true)
brace_style = "expand";
document.getElementById('brace-style').value = brace_style;
}

if (get_var('test')) {
Expand Down Expand Up @@ -85,7 +89,7 @@
var indent_char = ' ';
var preserve_newlines = document.getElementById('preserve-newlines').checked;
var keep_array_indentation = document.getElementById('keep-array-indentation').checked;
var braces_on_own_line = document.getElementById('braces-on-own-line').checked;
var brace_style = document.getElementById('brace-style').value;

if (indent_size == 1) {
indent_char = '\t';
Expand All @@ -100,7 +104,7 @@
indent_size: indent_size,
indent_char: indent_char,
preserve_newlines:preserve_newlines,
braces_on_own_line: braces_on_own_line,
brace_style: brace_style,
keep_array_indentation:keep_array_indentation,
space_after_anon_function:true});
}
Expand Down Expand Up @@ -293,7 +297,11 @@ <h2>Options</h2>
<option value="4" selected="selected">indent with 4 spaces</option>
<option value="8">indent with 8 spaces</option>
</select></li>
<li><input type="checkbox" id="braces-on-own-line" /><label for="braces-on-own-line"> Braces on own line</label><br /></li>
<li><select id="brace-style">
<option value="collapse">Braces with control statement</option>
<option value="expand">Braces on own line</option>
<option value="end-expand">End braces on own line</option>
</select></li>
<li><input type="checkbox" id="preserve-newlines" checked="checked" /><label for="preserve-newlines"> Preserve empty lines?</label><br /></li>
<li><input type="checkbox" id="detect-packers" checked="checked" /><label for="detect-packers"> Detect packers?</label><br /></li>
<li><input type="checkbox" id="keep-array-indentation" /><label for="keep-array-indentation"> Keep array indentation?</label></li>
Expand Down Expand Up @@ -349,7 +357,7 @@ <h3>gEdit</h3>
<a href="#" style="border-bottom: 1px dashed #36d; text-decoration: none;" onclick="run_tests(); return false;">Run the tests</a>
<br />
<pre id="testresults"></pre>
Written by <a href="mailto:einar@jsbeautifier.org">Einar Lielmanis</a>, with the help of <a href="http://jason.diamond.name/weblog/">Jason Diamond</a>, Patrick Hof, Nochum Sossonko, Andreas Schneider, Dave Vasilevsky, <a href="http://my.opera.com/Vital/blog/">Vital Batmanov,</a> Ron Baldwin, Gabriel Harrison and others.
Written by <a href="mailto:einar@jsbeautifier.org">Einar Lielmanis</a>, with the help of <a href="http://jason.diamond.name/weblog/">Jason Diamond</a>, Patrick Hof, Nochum Sossonko, Andreas Schneider, Dave Vasilevsky, <a href="http://my.opera.com/Vital/blog/">Vital Batmanov,</a> Ron Baldwin, Gabriel Harrison, <a href="http://shullian.com">Chris J. Shull</a> and others.

</div>
</div>
Expand Down
50 changes: 44 additions & 6 deletions tests/beautify-tests.js
Expand Up @@ -7,7 +7,7 @@ flags = {
preserve_newlines: true,
space_after_anon_function: true,
keep_array_indentation: false,
braces_on_own_line: false
brace_style: 'collapse'
}

function test_beautifier(input)
Expand Down Expand Up @@ -72,7 +72,7 @@ function run_beautifier_tests(test_obj)
flags.preserve_newlines = true;
flags.space_after_anon_function = true;
flags.keep_array_indentation = false;
flags.braces_on_own_line = false;
flags.brace_style = "collapse";

bt('');
bt('return .5');
Expand Down Expand Up @@ -381,15 +381,53 @@ function run_beautifier_tests(test_obj)

bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}', 'if (a) {\n b;\n} else {\n c;\n}');

flags.braces_on_own_line = true;

bt_braces('if (a)\n{\nb;\n}\nelse\n{\nc;\n}', 'if (a)\n{\n b;\n}\nelse\n{\n c;\n}');
bt_braces('var foo = {}');
flags.brace_style = 'expand';

bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}', 'if (a)\n{\n b;\n}\nelse\n{\n c;\n}');
test_fragment('if (foo) {', 'if (foo)\n{');
test_fragment('foo {', 'foo\n{');
test_fragment('return {', 'return {'); // return needs the brace. maybe something else as well: feel free to report.
// test_fragment('return\n{', 'return\n{'); // can't support this?, but that's an improbable and extreme case anyway.
test_fragment('return;\n{', 'return;\n{');


flags.brace_style = 'collapse';

bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}', 'if (a) {\n b;\n} else {\n c;\n}');
test_fragment('if (foo) {', 'if (foo) {');
test_fragment('foo {', 'foo {');
test_fragment('return {', 'return {'); // return needs the brace. maybe something else as well: feel free to report.
// test_fragment('return\n{', 'return\n{'); // can't support this?, but that's an improbable and extreme case anyway.
test_fragment('return;\n{', 'return; {');


flags.brace_style = "collapse";

bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}', 'if (a) {\n b;\n} else {\n c;\n}');
test_fragment('if (foo) {', 'if (foo) {');
test_fragment('foo {', 'foo {');
test_fragment('return {', 'return {'); // return needs the brace. maybe something else as well: feel free to report.
// test_fragment('return\n{', 'return\n{'); // can't support this?, but that's an improbable and extreme case anyway.
test_fragment('return;\n{', 'return; {');

flags.brace_style = "expand";

bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}', 'if (a)\n{\n b;\n}\nelse\n{\n c;\n}');
test_fragment('if (foo) {', 'if (foo)\n{');
test_fragment('foo {', 'foo\n{');
test_fragment('return {', 'return {'); // return needs the brace. maybe something else as well: feel free to report.
// test_fragment('return\n{', 'return\n{'); // can't support this, but that's an improbable and extreme case anyway.
// test_fragment('return\n{', 'return\n{'); // can't support this?, but that's an improbable and extreme case anyway.
test_fragment('return;\n{', 'return;\n{');

flags.brace_style = "end-expand";

bt('if(1){2}else{3}', "if (1) {\n 2\n}\nelse {\n 3\n}");
bt('try{a();}catch(b){c();}finally{d();}', "try {\n a();\n}\ncatch (b) {\n c();\n}\nfinally {\n d();\n}");
bt('if(a){b();}else if(c) foo();', "if (a) {\n b();\n}\nelse if (c) foo();");
bt("if (a) {\n// comment\n}else{\n// comment\n}", "if (a) {\n // comment\n}\nelse {\n // comment\n}"); // if/else statement with empty body
bt('if (x) {y} else { if (x) {y}}', 'if (x) {\n y\n}\nelse {\n if (x) {\n y\n }\n}');
bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}', 'if (a) {\n b;\n}\nelse {\n c;\n}');

return sanitytest;
}

0 comments on commit 0d22b9f

Please sign in to comment.