Skip to content

Commit

Permalink
Merge pull request beautifier#554 from c32hedge/c32hedge/brace-style-…
Browse files Browse the repository at this point in the history
…none

Add brace_style "none" option + tests for javascript, then cleanup all brace_style tests
  • Loading branch information
bitwiseman committed Oct 6, 2014
2 parents 7e19aaa + 670f44c commit 54ca21b
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 331 deletions.
16 changes: 11 additions & 5 deletions js/lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
space_after_anon_function (default false) - should the space before an anonymous function's parens be added, "function()" vs "function ()",
NOTE: This option is overriden by jslint_happy (i.e. if jslint_happy is true, space_after_anon_function is true by design)
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.
brace_style (default "collapse") - "collapse" | "expand" | "end-expand" | "none"
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, or attempt to keep them where they are.
space_before_conditional (default true) - should the space before conditional statement be added, "if(true)" vs "if (true)",
Expand Down Expand Up @@ -705,7 +705,8 @@
var empty_anonymous_function = empty_braces && flags.last_word === 'function' &&
last_type === 'TK_END_EXPR';

if (opt.brace_style === "expand") {
if (opt.brace_style === "expand" ||
(opt.brace_style === "none" && current_token.wanted_newline)) {
if (last_type !== 'TK_OPERATOR' &&
(empty_anonymous_function ||
last_type === 'TK_EQUALS' ||
Expand Down Expand Up @@ -878,7 +879,9 @@
if (!(current_token.type === 'TK_RESERVED' && in_array(current_token.text, ['else', 'catch', 'finally']))) {
prefix = 'NEWLINE';
} else {
if (opt.brace_style === "expand" || opt.brace_style === "end-expand") {
if (opt.brace_style === "expand" ||
opt.brace_style === "end-expand" ||
(opt.brace_style === "none" && current_token.wanted_newline)) {
prefix = 'NEWLINE';
} else {
prefix = 'SPACE';
Expand Down Expand Up @@ -912,7 +915,10 @@
}

if (current_token.type === 'TK_RESERVED' && in_array(current_token.text, ['else', 'catch', 'finally'])) {
if (last_type !== 'TK_END_BLOCK' || opt.brace_style === "expand" || opt.brace_style === "end-expand") {
if (last_type !== 'TK_END_BLOCK' ||
opt.brace_style === "expand" ||
opt.brace_style === "end-expand" ||
(opt.brace_style === "none" && current_token.wanted_newline)) {
print_newline();
} else {
output.trim(true);
Expand Down
4 changes: 2 additions & 2 deletions js/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var fs = require('fs'),
"jslint_happy": Boolean,
"space_after_anon_function": Boolean,
// TODO: expand-strict is obsolete, now identical to expand. Remove in future version
"brace_style": ["collapse", "expand", "end-expand", "expand-strict"],
"brace_style": ["collapse", "expand", "end-expand", "expand-strict", "none"],
"break_chained_methods": Boolean,
"keep_array_indentation": Boolean,
"unescape_strings": Boolean,
Expand Down Expand Up @@ -217,7 +217,7 @@ function usage(err) {
msg.push(' -E, --space-in-empty-paren Add a single space inside empty paren, ie. f( )');
msg.push(' -j, --jslint-happy Enable jslint-stricter mode');
msg.push(' -a, --space_after_anon_function Add a space before an anonymous function\'s parens, ie. function ()');
msg.push(' -b, --brace-style [collapse|expand|end-expand] ["collapse"]');
msg.push(' -b, --brace-style [collapse|expand|end-expand|none] ["collapse"]');
msg.push(' -B, --break-chained-methods Break chained method calls across subsequent lines');
msg.push(' -k, --keep-array-indentation Preserve array indentation');
msg.push(' -x, --unescape-strings Decode printable characters encoded in xNN notation');
Expand Down
Loading

0 comments on commit 54ca21b

Please sign in to comment.