mirror of
https://github.com/beautifier/js-beautify.git
synced 2025-03-04 11:08:11 +00:00
Add experimental brace-on-own-line support. Yes.
This commit is contained in:
parent
6ed98f8c85
commit
7426ff6c51
@ -25,7 +25,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("-a Indent arrays");
|
||||
print("-b\tPut braces on own line (Allman / ANSI style)");
|
||||
print("-a\tIndent arrays");
|
||||
print("-n\tPreserve newlines");
|
||||
print("-p\tJSLint-pedantic mode, currently only adds space between \"function ()\"");
|
||||
print("-h\tPrint this help\n");
|
||||
@ -44,6 +45,9 @@ function parse_opts(args) {
|
||||
case "-i":
|
||||
options.indent = args.shift();
|
||||
break;
|
||||
case "-b":
|
||||
options.braces_on_own_line = true;
|
||||
break;
|
||||
case "-a":
|
||||
options.keep_array_indentation = false;
|
||||
break;
|
||||
@ -89,6 +93,11 @@ function do_js_beautify() {
|
||||
lines.push(stdin.readLine());
|
||||
}
|
||||
if (lines.length) js_source = lines.join("\n");
|
||||
|
||||
if ( ! lines.length) {
|
||||
print_usage();
|
||||
quit();
|
||||
}
|
||||
}
|
||||
js_source = js_source.replace(/^\s+/, '');
|
||||
var indent_size = options.indent ? options.indent : 2;
|
||||
@ -106,7 +115,8 @@ function do_js_beautify() {
|
||||
indent_char: indent_char,
|
||||
preserve_newlines: preserve_newlines,
|
||||
space_after_anon_function: options.jslint_pedantic,
|
||||
keep_array_indentation: options.keep_array_indentation
|
||||
keep_array_indentation: options.keep_array_indentation,
|
||||
braces_on_own_line: options.braces_on_own_line
|
||||
});
|
||||
}
|
||||
return result;
|
||||
|
57
beautify.js
57
beautify.js
@ -44,6 +44,7 @@ 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_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;
|
||||
@ -671,31 +672,45 @@ function js_beautify(js_source_text, options) {
|
||||
} else {
|
||||
set_mode('BLOCK');
|
||||
}
|
||||
if (last_type !== 'TK_OPERATOR' && last_type !== 'TK_START_EXPR') {
|
||||
if (last_type === 'TK_START_BLOCK') {
|
||||
print_newline();
|
||||
} else {
|
||||
print_single_space();
|
||||
if (opt_braces_on_own_line) {
|
||||
if (last_type !== 'TK_OPERATOR') {
|
||||
print_newline(true);
|
||||
}
|
||||
print_token();
|
||||
indent();
|
||||
} else {
|
||||
if (last_type !== 'TK_OPERATOR' && last_type !== 'TK_START_EXPR') {
|
||||
if (last_type === 'TK_START_BLOCK') {
|
||||
print_newline();
|
||||
} else {
|
||||
print_single_space();
|
||||
}
|
||||
}
|
||||
indent();
|
||||
print_token();
|
||||
}
|
||||
indent();
|
||||
print_token();
|
||||
|
||||
break;
|
||||
|
||||
case 'TK_END_BLOCK':
|
||||
restore_mode();
|
||||
if (last_type === 'TK_START_BLOCK') {
|
||||
// nothing
|
||||
if (just_added_newline) {
|
||||
remove_indent();
|
||||
} else {
|
||||
// {}
|
||||
trim_output();
|
||||
}
|
||||
} else {
|
||||
if (opt_braces_on_own_line) {
|
||||
print_newline();
|
||||
print_token();
|
||||
} else {
|
||||
if (last_type === 'TK_START_BLOCK') {
|
||||
// nothing
|
||||
if (just_added_newline) {
|
||||
remove_indent();
|
||||
} else {
|
||||
// {}
|
||||
trim_output();
|
||||
}
|
||||
} else {
|
||||
print_newline();
|
||||
}
|
||||
print_token();
|
||||
}
|
||||
print_token();
|
||||
break;
|
||||
|
||||
case 'TK_WORD':
|
||||
@ -744,8 +759,12 @@ function js_beautify(js_source_text, options) {
|
||||
if (!in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) {
|
||||
prefix = 'NEWLINE';
|
||||
} else {
|
||||
prefix = 'SPACE';
|
||||
print_single_space();
|
||||
if (opt_braces_on_own_line) {
|
||||
prefix = 'NEWLINE';
|
||||
} else {
|
||||
prefix = 'SPACE';
|
||||
print_single_space();
|
||||
}
|
||||
}
|
||||
} else if (last_type === 'TK_SEMICOLON' && (flags.mode === 'BLOCK' || flags.mode === 'DO_BLOCK')) {
|
||||
prefix = 'NEWLINE';
|
||||
|
@ -18,10 +18,14 @@ function add_onload_function(fn)
|
||||
add_onload_function(function() {
|
||||
|
||||
var tabsize = get_var('tabsize');
|
||||
var braces_on_own_line = get_var('braces');
|
||||
var c;
|
||||
if (tabsize) {
|
||||
document.getElementById('tabsize').value = tabsize;
|
||||
}
|
||||
if (braces_on_own_line) {
|
||||
document.getElementById('braces-on-own-line').checked = 'checked';
|
||||
}
|
||||
|
||||
if (get_var('test')) {
|
||||
run_tests();
|
||||
@ -82,6 +86,7 @@ function do_js_beautify()
|
||||
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;
|
||||
|
||||
if (indent_size == 1) {
|
||||
indent_char = '\t';
|
||||
@ -96,6 +101,7 @@ function do_js_beautify()
|
||||
indent_size: indent_size,
|
||||
indent_char: indent_char,
|
||||
preserve_newlines:preserve_newlines,
|
||||
braces_on_own_line: braces_on_own_line,
|
||||
keep_array_indentation:keep_array_indentation,
|
||||
space_after_anon_function:true});
|
||||
}
|
||||
@ -258,6 +264,7 @@ pre {
|
||||
<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><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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user