Add --space-in-empty-paren (-E) option, fix #409

This commit is contained in:
Maël Nison 2014-02-17 11:14:37 +01:00
parent 181b6a17f3
commit 9e326ac78b
3 changed files with 20 additions and 2 deletions

View File

@ -202,6 +202,7 @@
opt.break_chained_methods = (options.break_chained_methods === undefined) ? false : options.break_chained_methods;
opt.max_preserve_newlines = (options.max_preserve_newlines === undefined) ? 0 : parseInt(options.max_preserve_newlines, 10);
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.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;
@ -1098,7 +1099,7 @@
allow_wrap_or_preserved_newline();
}
if (opt.space_in_paren) {
if (last_type === 'TK_START_EXPR') {
if (last_type === 'TK_START_EXPR' && ! opt.space_in_empty_paren) {
// () [] no inner space in empty parens like these, ever, ref #320
trim_output();
output_space_before_token = false;

View File

@ -51,6 +51,7 @@ var fs = require('fs'),
"preserve_newlines": Boolean,
"max_preserve_newlines": Number,
"space_in_paren": Boolean,
"space_in_empty_paren": Boolean,
"jslint_happy": Boolean,
// TODO: expand-strict is obsolete, now identical to expand. Remove in future version
"brace_style": ["collapse", "expand", "end-expand", "expand-strict"],
@ -85,6 +86,7 @@ var fs = require('fs'),
"p": ["--preserve_newlines"],
"m": ["--max_preserve_newlines"],
"P": ["--space_in_paren"],
"E": ["--space_in_empty_paren"],
"j": ["--jslint_happy"],
"b": ["--brace_style"],
"B": ["--break_chained_methods"],
@ -208,6 +210,7 @@ function usage(err) {
msg.push(' -p, --preserve-newlines Preserve line-breaks (--no-preserve-newlines disables)');
msg.push(' -m, --max-preserve-newlines Number of line-breaks to be preserved in one chunk [10]');
msg.push(' -P, --space-in-paren Add padding spaces within paren, ie. f( a, b )');
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(' -b, --brace-style [collapse|expand|end-expand] ["collapse"]');
msg.push(' -B, --break-chained-methods Break chained method calls across subsequent lines');

View File

@ -1356,7 +1356,7 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
'a = [b, c, d];');
bt('a= f[b];',
'a = f[b];');
opts.space_in_paren = true
opts.space_in_paren = true;
bt('if(p) foo(a,b)', 'if ( p ) foo( a, b )');
bt('try{while(true){willThrow()}}catch(result)switch(result){case 1:++result }',
'try {\n while ( true ) {\n willThrow()\n }\n} catch ( result ) switch ( result ) {\n case 1:\n ++result\n}');
@ -1369,6 +1369,20 @@ function run_beautifier_tests(test_obj, Urlencoded, js_beautify, html_beautify,
'a = [ b, c, d ];');
bt('a= f[b];',
'a = f[ b ];');
opts.space_in_empty_paren = true;
bt('if(p) foo(a,b)', 'if ( p ) foo( a, b )');
bt('try{while(true){willThrow()}}catch(result)switch(result){case 1:++result }',
'try {\n while ( true ) {\n willThrow( )\n }\n} catch ( result ) switch ( result ) {\n case 1:\n ++result\n}');
bt('((e/((a+(b)*c)-d))^2)*5;', '( ( e / ( ( a + ( b ) * c ) - d ) ) ^ 2 ) * 5;');
bt('function f(a,b) {if(a) b()}function g(a,b) {if(!a) b()}',
'function f( a, b ) {\n if ( a ) b( )\n}\n\nfunction g( a, b ) {\n if ( !a ) b( )\n}');
bt('a=[];',
'a = [ ];');
bt('a=[b,c,d];',
'a = [ b, c, d ];');
bt('a= f[b];',
'a = f[ b ];');
opts.space_in_empty_paren = false;
opts.space_in_paren = false;
// Test that e4x literals passed through when e4x-option is enabled