Skip to content

Commit

Permalink
Added comma separation CLI option for brace_style
Browse files Browse the repository at this point in the history
Removed brace_preserve_inline from everywhere
Correctly validates CLI arguments in both Python and Javascript
  • Loading branch information
Peter "Coburn" Fornari committed Dec 8, 2016
1 parent 510a887 commit 1509a1f
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 105 deletions.
2 changes: 1 addition & 1 deletion ISSUE_TEMPLATE.md
Expand Up @@ -43,7 +43,7 @@ Example:
"max_preserve_newlines": 10,
"jslint_happy": false,
"space_after_anon_function": false,
"brace_style": "collapse-preserve-inline",
"brace_style": "collapse,preserve-inline",
"keep_array_indentation": false,
"keep_function_indentation": false,
"space_before_conditional": true,
Expand Down
4 changes: 1 addition & 3 deletions README.md
Expand Up @@ -106,8 +106,7 @@ Beautifier Options:
-P, --space-in-paren Add padding spaces within paren, ie. f( a, b )
-j, --jslint-happy Enable jslint-stricter mode
-a, --space-after-anon-function Add a space before an anonymous function's parens, ie. function ()
-b, --brace-style [collapse|expand|end-expand|none] ["collapse"]
-i, --brace-preserve-inline Preserve line-breaks of braces that appear on the same line [false]
-b, --brace-style [collapse|expand|end-expand|none][,preserve-inline] ["collapse"]
-B, --break-chained-methods Break chained method calls across subsequent lines
-k, --keep-array-indentation Preserve array indentation
-x, --unescape-strings Decode printable characters encoded in xNN notation
Expand All @@ -133,7 +132,6 @@ These largely correspond to the underscored option keys for both library interfa
"jslint_happy": false,
"space_after_anon_function": false,
"brace_style": "collapse",
"brace_preserve_inline": false,
"keep_array_indentation": false,
"keep_function_indentation": false,
"space_before_conditional": true,
Expand Down
1 change: 0 additions & 1 deletion js/config/defaults.json
Expand Up @@ -8,7 +8,6 @@
"jslint_happy": false,
"space_after_anon_function": false,
"brace_style": "collapse",
"brace_preserve_inline": false,
"keep_array_indentation": false,
"keep_function_indentation": false,
"space_before_conditional": true,
Expand Down
33 changes: 18 additions & 15 deletions js/lib/beautify.js
Expand Up @@ -61,10 +61,9 @@
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" | "none"
brace_style (default "collapse") - "collapse" | "expand" | "end-expand" | "none" | any of the former + ",preserve-inline"
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.
brace_preserve_inline (default false) - if open and closed braces appear on the same line, then don't touch them
preserve-inline will try to preserve inline blocks of curly braces
space_before_conditional (default true) - should the space before conditional statement be added, "if(true)" vs "if (true)",
Expand Down Expand Up @@ -301,22 +300,26 @@ if (!Object.values) {
options = options ? options : {};
opt = {};

// compatibility
if (options.braces_on_own_line !== undefined) { //graceful handling of deprecated option
opt.brace_style = options.braces_on_own_line ? "expand" : "collapse";
// compatibility, re
if (options.brace_style === "expand-strict") { //graceful handling of deprecated option
options.brace_style = "expand";
}
opt.brace_style = options.brace_style ? options.brace_style : (opt.brace_style ? opt.brace_style : "collapse");

// graceful handling of deprecated option
if (opt.brace_style === "expand-strict") {
opt.brace_style = "expand";
else if (options.brace_style === "collapse-preserve-inline") { //graceful handling of deprecated option
options.brace_style = "collapse,preserve-inline";
}
else if (options.braces_on_own_line !== undefined) { //graceful handling of deprecated option
options.brace_style = options.braces_on_own_line ? "expand" : "collapse";
}
if (opt.brace_style === "collapse-preserve-inline") {
opt.brace_style = "collapse";
opt.brace_preserve_inline = true;
else if(!options.brace_style) //Nothing exists to set it
{
options.brace_style = "collapse";
}


var brace_style_split = options.brace_style.split(/[^a-zA-Z0-9_\-]+/);
opt.brace_style = brace_style_split[0];
opt.brace_preserve_inline = brace_style_split[1] ? brace_style_split[1] : false;

opt.brace_preserve_inline = (options.brace_preserve_inline === undefined) ? false : options.brace_preserve_inline;
opt.indent_size = options.indent_size ? parseInt(options.indent_size, 10) : 4;
opt.indent_char = options.indent_char ? options.indent_char : ' ';
opt.eol = options.eol ? options.eol : 'auto';
Expand Down
31 changes: 22 additions & 9 deletions js/lib/cli.js
Expand Up @@ -40,8 +40,26 @@ var fs = require('fs'),
cc = require('config-chain'),
beautify = require('../index'),
mkdirp = require('mkdirp'),
nopt = require('nopt'),
path = require('path'),
nopt = require('nopt');
nopt.typeDefs["brace_style"] = {
type : "brace_style",
validate : function(data, key, val) {
data[key] = val;
// TODO: expand-strict is obsolete, now identical to expand. Remove in future version
// TODO: collapse-preserve-inline is obselete, now identical to collapse,preserve-inline = true. Remove in future version
var validVals = ["collapse", "collapse-preserve-inline", "expand", "end-expand", "expand-strict", "none"];
var valSplit = val.split(/[^a-zA-Z0-9_\-]+/);
for(var i=0; i<validVals.length; i++)
{
if(validVals[i] === val || validVals[i] === valSplit[0] && valSplit[1] === "preserve-inline")
{
return true;
}
}
return false;
}
};
var path = require('path'),
editorconfig = require('editorconfig'),
knownOpts = {
// Beautifier
Expand All @@ -56,10 +74,7 @@ var fs = require('fs'),
"space_in_empty_paren": Boolean,
"jslint_happy": Boolean,
"space_after_anon_function": Boolean,
// TODO: expand-strict is obsolete, now identical to expand. Remove in future version
// TODO: collapse-preserve-inline is obselete, now identical to collapse + brace_preserve_inline = true. Remove in future version
"brace_style": ["collapse", "collapse-preserve-inline", "expand", "end-expand", "expand-strict", "none"],
"brace_preserve_inline": Boolean,
"brace_style": "brace_style", //See above for validation
"break_chained_methods": Boolean,
"keep_array_indentation": Boolean,
"unescape_strings": Boolean,
Expand Down Expand Up @@ -110,7 +125,6 @@ var fs = require('fs'),
"j": ["--jslint_happy"],
"a": ["--space_after_anon_function"],
"b": ["--brace_style"],
"V": ["--brace_preserve_inline"],
"B": ["--break_chained_methods"],
"k": ["--keep_array_indentation"],
"x": ["--unescape_strings"],
Expand Down Expand Up @@ -306,8 +320,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|none] ["collapse"]');
msg.push(' -V, --brace_preserve_inline Preserve line-breaks of braces that appear on the same line [false]');
msg.push(' -b, --brace-style [collapse|expand|end-expand|none][,preserve-inline] [collapse,preserve-inline]');
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
34 changes: 14 additions & 20 deletions js/test/generated/beautify-javascript-tests.js
Expand Up @@ -29,7 +29,6 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
default_opts.jslint_happy = false;
default_opts.keep_array_indentation = false;
default_opts.brace_style = 'collapse';
default_opts.brace_preserve_inline = false;
default_opts.operator_position = 'before-newline';

function reset_options()
Expand Down Expand Up @@ -343,28 +342,28 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
reset_options();
//============================================================
// Brace style permutations - (ibo = "", iao = "", ibc = "", iac = "", obo = " ", oao = " ", obc = " ", oac = " ")
opts.brace_preserve_inline = true;
opts.brace_style = 'collapse,preserve-inline';
bt('var a ={a: 2};\nvar a ={a: 2};', 'var a = { a: 2 };\nvar a = { a: 2 };');
bt('//case 1\nif (a == 1){}\n//case 2\nelse if (a == 2){}', '//case 1\nif (a == 1) {}\n//case 2\nelse if (a == 2) {}');
bt('if(1){2}else{3}', 'if (1) { 2 } else { 3 }');
bt('try{a();}catch(b){c();}catch(d){}finally{e();}', 'try { a(); } catch (b) { c(); } catch (d) {} finally { e(); }');

// Brace style permutations - (ibo = "\n", iao = "\n", ibc = "\n", iac = "\n", obo = " ", oao = "\n ", obc = "\n", oac = " ")
opts.brace_preserve_inline = true;
opts.brace_style = 'collapse,preserve-inline';
bt('var a =\n{\na: 2\n}\n;\nvar a =\n{\na: 2\n}\n;', 'var a = {\n a: 2\n};\nvar a = {\n a: 2\n};');
bt('//case 1\nif (a == 1)\n{}\n//case 2\nelse if (a == 2)\n{}', '//case 1\nif (a == 1) {}\n//case 2\nelse if (a == 2) {}');
bt('if(1)\n{\n2\n}\nelse\n{\n3\n}', 'if (1) {\n 2\n} else {\n 3\n}');
bt('try\n{\na();\n}\ncatch(b)\n{\nc();\n}\ncatch(d)\n{}\nfinally\n{\ne();\n}', 'try {\n a();\n} catch (b) {\n c();\n} catch (d) {} finally {\n e();\n}');

// Brace style permutations - (ibo = "", iao = "", ibc = "", iac = "", obo = " ", oao = "\n ", obc = "\n", oac = " ")
opts.brace_preserve_inline = false;
opts.brace_style = 'collapse';
bt('var a ={a: 2};\nvar a ={a: 2};', 'var a = {\n a: 2\n};\nvar a = {\n a: 2\n};');
bt('//case 1\nif (a == 1){}\n//case 2\nelse if (a == 2){}', '//case 1\nif (a == 1) {}\n//case 2\nelse if (a == 2) {}');
bt('if(1){2}else{3}', 'if (1) {\n 2\n} else {\n 3\n}');
bt('try{a();}catch(b){c();}catch(d){}finally{e();}', 'try {\n a();\n} catch (b) {\n c();\n} catch (d) {} finally {\n e();\n}');

// Brace style permutations - (ibo = "\n", iao = "\n", ibc = "\n", iac = "\n", obo = " ", oao = "\n ", obc = "\n", oac = " ")
opts.brace_preserve_inline = false;
opts.brace_style = 'collapse';
bt('var a =\n{\na: 2\n}\n;\nvar a =\n{\na: 2\n}\n;', 'var a = {\n a: 2\n};\nvar a = {\n a: 2\n};');
bt('//case 1\nif (a == 1)\n{}\n//case 2\nelse if (a == 2)\n{}', '//case 1\nif (a == 1) {}\n//case 2\nelse if (a == 2) {}');
bt('if(1)\n{\n2\n}\nelse\n{\n3\n}', 'if (1) {\n 2\n} else {\n 3\n}');
Expand Down Expand Up @@ -2206,9 +2205,8 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,

reset_options();
//============================================================
// brace_preserve_inline tests - (obo = " ", obot = "", oao = "\n", oaot = " ", obc = "\n", oac = " ", oact = "")
opts.brace_style = 'collapse';
opts.brace_preserve_inline = true;
// brace_style ,preserve-inline tests - (obo = " ", obot = "", oao = "\n", oaot = " ", obc = "\n", oac = " ", oact = "")
opts.brace_style = 'collapse,preserve-inline';
bt('import { asdf } from "asdf";');
bt('function inLine() { console.log("oh em gee"); }');
bt('if (cancer) { console.log("Im sorry but you only have so long to live..."); }');
Expand Down Expand Up @@ -2248,9 +2246,8 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
' var obj = {\n a: function() { console.log("test"); },\n' +
' b() {\n console.log("test2");\n }\n };\n}');

// brace_preserve_inline tests - (obo = "\n", obot = " ", oao = "\n", oaot = " ", obc = "\n", oac = "\n", oact = " ")
opts.brace_style = 'expand';
opts.brace_preserve_inline = true;
// brace_style ,preserve-inline tests - (obo = "\n", obot = " ", oao = "\n", oaot = " ", obc = "\n", oac = "\n", oact = " ")
opts.brace_style = 'expand,preserve-inline';
bt('import { asdf } from "asdf";');
bt('function inLine() { console.log("oh em gee"); }');
bt('if (cancer) { console.log("Im sorry but you only have so long to live..."); }');
Expand Down Expand Up @@ -2290,9 +2287,8 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
' var obj = {\n a: function() { console.log("test"); },\n' +
' b()\n {\n console.log("test2");\n }\n };\n}');

// brace_preserve_inline tests - (obo = " ", obot = "", oao = "\n", oaot = " ", obc = "\n", oac = "\n", oact = " ")
opts.brace_style = 'end-expand';
opts.brace_preserve_inline = true;
// brace_style ,preserve-inline tests - (obo = " ", obot = "", oao = "\n", oaot = " ", obc = "\n", oac = "\n", oact = " ")
opts.brace_style = 'end-expand,preserve-inline';
bt('import { asdf } from "asdf";');
bt('function inLine() { console.log("oh em gee"); }');
bt('if (cancer) { console.log("Im sorry but you only have so long to live..."); }');
Expand Down Expand Up @@ -2332,9 +2328,8 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
' var obj = {\n a: function() { console.log("test"); },\n' +
' b() {\n console.log("test2");\n }\n };\n}');

// brace_preserve_inline tests - (obo = " ", obot = "", oao = "\n", oaot = " ", obc = "\n", oac = " ", oact = "")
opts.brace_style = 'none';
opts.brace_preserve_inline = true;
// brace_style ,preserve-inline tests - (obo = " ", obot = "", oao = "\n", oaot = " ", obc = "\n", oac = " ", oact = "")
opts.brace_style = 'none,preserve-inline';
bt('import { asdf } from "asdf";');
bt('function inLine() { console.log("oh em gee"); }');
bt('if (cancer) { console.log("Im sorry but you only have so long to live..."); }');
Expand Down Expand Up @@ -2374,7 +2369,7 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
' var obj = {\n a: function() { console.log("test"); },\n' +
' b() {\n console.log("test2");\n }\n };\n}');

// brace_preserve_inline tests - (obo = " ", obot = "", oao = "\n", oaot = " ", obc = "\n", oac = " ", oact = "")
// brace_style ,preserve-inline tests - (obo = " ", obot = "", oao = "\n", oaot = " ", obc = "\n", oac = " ", oact = "")
opts.brace_style = 'collapse-preserve-inline';
bt('import { asdf } from "asdf";');
bt('function inLine() { console.log("oh em gee"); }');
Expand Down Expand Up @@ -2419,8 +2414,7 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
reset_options();
//============================================================
// Destructured and related
opts.brace_style = 'collapse';
opts.brace_preserve_inline = true;
opts.brace_style = 'collapse,preserve-inline';

// Issue 382 - import destructured
bt(
Expand Down
3 changes: 1 addition & 2 deletions jsbeautifyrc
Expand Up @@ -7,8 +7,7 @@
"max_preserve_newlines": 10,
"jslint_happy": false,
"space_after_anon_function": false,
"brace_style": "collapse",
"brace_preserve_inline": true,
"brace_style": "collapse,preserve-inline",
"keep_array_indentation": false,
"keep_function_indentation": false,
"space_before_conditional": true,
Expand Down
22 changes: 11 additions & 11 deletions python/jsbeautifier/__init__.py
Expand Up @@ -74,7 +74,6 @@ def __init__(self):
self.jslint_happy = False
self.space_after_anon_function = False
self.brace_style = 'collapse'
self.brace_preserve_inline = False
self.keep_array_indentation = False
self.keep_function_indentation = False
self.eval_code = False
Expand Down Expand Up @@ -102,7 +101,6 @@ def __repr__(self):
space_after_anon_function = %s
indent_with_tabs = %s
brace_style = %s
brace_preserve_inline = %s
keep_array_indentation = %s
eval_code = %s
wrap_line_length = %s
Expand All @@ -116,7 +114,6 @@ def __repr__(self):
self.space_after_anon_function,
self.indent_with_tabs,
self.brace_style,
self.brace_preserve_inline,
self.keep_array_indentation,
self.eval_code,
self.wrap_line_length,
Expand Down Expand Up @@ -338,8 +335,7 @@ def usage(stream=sys.stdout):
-E, --space-in-empty-paren Add a single space inside empty paren, ie. f( )
-j, --jslint-happy More jslint-compatible output
-a, --space_after_anon_function Add a space before an anonymous function's parens, ie. function ()
-b, --brace-style=collapse Brace style (collapse, expand, end-expand)
-V, --brace-preserve-inline Preserve line-breaks of braces that appear on the same line
-b, --brace-style=collapse Brace style (collapse, expand, end-expand, none)(,preserve-inline)
-k, --keep-array-indentation Keep array indentation.
-r, --replace Write output in-place, replacing input
-o, --outfile=FILE Specify a file to output to (default stdout)
Expand Down Expand Up @@ -450,12 +446,18 @@ def beautify(self, s, opts = None ):

#Compat with old form
if self.opts.brace_style == 'collapse-preserve-inline':
self.opts.brace_style = 'collapse'
self.opts.brace_preserve_inline = True

self.opts.brace_style = 'collapse,preserve-inline'

split = re.compile("[^a-zA-Z0-9_\-]+").split(self.opts.brace_style)
self.opts.brace_style = split[0]
self.opts.brace_preserve_inline = (True if bool(split[1] == 'preserve-inline') else None) if len(split) > 1 else False

if self.opts.brace_style not in ['expand', 'collapse', 'end-expand', 'none']:
raise(Exception('opts.brace_style must be "expand", "collapse", "end-expand", or "none".'))

if self.opts.brace_preserve_inline == None:
raise(Exception('opts.brace_style second item must be "preserve-inline"'))

s = self.blank_state(s)

input = self.unpack(s, self.opts.eval_code)
Expand Down Expand Up @@ -2154,7 +2156,7 @@ def main():
opts, args = getopt.getopt(argv, "s:c:e:o:rdEPjabVkil:xhtfvXnCO:w:",
['indent-size=','indent-char=','eol=''outfile=', 'replace', 'disable-preserve-newlines',
'space-in-paren', 'space-in-empty-paren', 'jslint-happy', 'space-after-anon-function',
'brace-style=', 'brace_preserve_inline', 'keep-array-indentation', 'indent-level=', 'unescape-strings',
'brace-style=', 'keep-array-indentation', 'indent-level=', 'unescape-strings',
'help', 'usage', 'stdin', 'eval-code', 'indent-with-tabs', 'keep-function-indentation', 'version',
'e4x', 'end-with-newline','comma-first','operator-position=','wrap-line-length','editorconfig'])
except getopt.GetoptError as ex:
Expand Down Expand Up @@ -2200,8 +2202,6 @@ def main():
js_options.eval_code = True
elif opt in ('--brace-style', '-b'):
js_options.brace_style = arg
elif opt in ('--brace-preserve-inline', '-V'):
js_options.brace_preserve_inline = True
elif opt in ('--unescape-strings', '-x'):
js_options.unescape_strings = True
elif opt in ('--e4x', '-X'):
Expand Down

0 comments on commit 1509a1f

Please sign in to comment.