mirror of
https://github.com/beautifier/js-beautify.git
synced 2025-02-17 03:38:08 +00:00
Added an option for the old "function ()" syntax.
It's in the code, and for command-line, though. Won't push it to the web interface, as it's too trivial.
This commit is contained in:
parent
6e2d444c45
commit
37fc405b1a
@ -7,6 +7,7 @@ var test_result = '';
|
||||
var indent_size = 4;
|
||||
var indent_char = ' ';
|
||||
var preserve_newlines = true;
|
||||
var space_after_anon_function = true;
|
||||
|
||||
function lazy_escape(str)
|
||||
{
|
||||
@ -17,7 +18,11 @@ function bt(input, expected)
|
||||
{
|
||||
expected = expected || input;
|
||||
|
||||
var result = js_beautify(input, {indent_size: indent_size, indent_char: indent_char, preserve_newlines: preserve_newlines});
|
||||
var result = js_beautify(input, {
|
||||
indent_size: indent_size,
|
||||
indent_char: indent_char,
|
||||
preserve_newlines: preserve_newlines,
|
||||
space_after_anon_function: space_after_anon_function});
|
||||
|
||||
if (result !== expected) {
|
||||
test_result +=
|
||||
@ -50,6 +55,7 @@ function test_js_beautify()
|
||||
indent_char = ' ';
|
||||
test_result = '';
|
||||
preserve_newlines = true;
|
||||
space_after_anon_function = true;
|
||||
|
||||
bt('');
|
||||
bt('a = 1', 'a = 1');
|
||||
@ -106,7 +112,6 @@ function test_js_beautify()
|
||||
bt('switch(x){case -1:break;case !y:break;}', 'switch (x) {\ncase -1:\n break;\ncase !y:\n break;\n}');
|
||||
bt('a !== b');
|
||||
bt('if (a) b(); else c();', "if (a) b();\nelse c();");
|
||||
bt("// comment\n(function()", "// comment\n(function ()"); // typical greasemonkey start
|
||||
bt("// comment\n(function something()"); // typical greasemonkey start
|
||||
bt("{\n\n x();\n\n}"); // was: duplicating newlines
|
||||
bt('if (a in b)');
|
||||
@ -144,7 +149,6 @@ function test_js_beautify()
|
||||
bt("do {} while (1);");
|
||||
bt("do {\n} while (1);", "do {} while (1);");
|
||||
bt("do {\n\n} while (1);");
|
||||
bt("var a, b, c, d = 0, c = function() {}, d = '';", "var a, b, c, d = 0,\nc = function () {},\nd = '';");
|
||||
bt("var a = x(a, b, c)");
|
||||
bt("delete x if (a) b();", "delete x\nif (a) b();");
|
||||
bt("delete x[x] if (a) b();", "delete x[x]\nif (a) b();");
|
||||
@ -190,9 +194,6 @@ function test_js_beautify()
|
||||
bt("<!--\nsomething();\n-->\n<!--\nsomething();\n-->", "<!--\nsomething();\n-->\n<!--\nsomething();\n-->");
|
||||
bt("<!--\nif(i<0){bla();}\n-->\n<!--\nif(i<0){bla();}\n-->", "<!--\nif (i < 0) {\n bla();\n}\n-->\n<!--\nif (i < 0) {\n bla();\n}\n-->");
|
||||
|
||||
bt('var o=$.extend(a,function(){alert(x);}', 'var o = $.extend(a, function () {\n alert(x);\n}');
|
||||
bt('var o=$.extend(a);function(){alert(x);}', 'var o = $.extend(a);\nfunction () {\n alert(x);\n}');
|
||||
|
||||
bt('{foo();--bar;}', '{\n foo();\n --bar;\n}');
|
||||
bt('{foo();++bar;}', '{\n foo();\n ++bar;\n}');
|
||||
bt('{--bar;}', '{\n --bar;\n}');
|
||||
@ -205,6 +206,20 @@ function test_js_beautify()
|
||||
// allow unescaped / in char classes
|
||||
bt('a(/[a/b]/);b()', "a(/[a/b]/);\nb()");
|
||||
|
||||
space_after_anon_function = true;
|
||||
|
||||
bt("// comment 1\n(function()", "// comment 1\n(function ()"); // typical greasemonkey start
|
||||
bt("var a1, b1, c1, d1 = 0, c = function() {}, d = '';", "var a1, b1, c1, d1 = 0,\nc = function () {},\nd = '';");
|
||||
bt('var o1=$.extend(a,function(){alert(x);}', 'var o1 = $.extend(a, function () {\n alert(x);\n}');
|
||||
bt('var o1=$.extend(a);function(){alert(x);}', 'var o1 = $.extend(a);\nfunction () {\n alert(x);\n}');
|
||||
|
||||
space_after_anon_function = false;
|
||||
|
||||
bt("// comment 2\n(function()", "// comment 2\n(function()"); // typical greasemonkey start
|
||||
bt("var a2, b2, c2, d2 = 0, c = function() {}, d = '';", "var a2, b2, c2, d2 = 0,\nc = function() {},\nd = '';");
|
||||
bt('var o2=$.extend(a,function(){alert(x);}', 'var o2 = $.extend(a, function() {\n alert(x);\n}');
|
||||
bt('var o2=$.extend(a);function(){alert(x);}', 'var o2 = $.extend(a);\nfunction() {\n alert(x);\n}');
|
||||
|
||||
indent_size = 1;
|
||||
indent_char = ' ';
|
||||
bt('{ one_char() }', "{\n one_char()\n}");
|
||||
|
17
beautify.js
17
beautify.js
@ -22,6 +22,9 @@
|
||||
preserve_newlines (default true) — whether existing line breaks should be preserved,
|
||||
indent_level (default 0) — initial indentation level, you probably won't need this ever,
|
||||
|
||||
space_after_anon_function (default true) — if true, then space is added between "function ()"
|
||||
(jslint is happy about this); if false, then the common "function()" output is used.
|
||||
|
||||
e.g
|
||||
|
||||
js_beautify(js_source_text, {indent_size: 1, indent_char: '\t'});
|
||||
@ -40,12 +43,13 @@ function js_beautify(js_source_text, options)
|
||||
var indent_level;
|
||||
|
||||
|
||||
options = options || {};
|
||||
options = options || {};
|
||||
var opt_indent_size = options.indent_size || 4;
|
||||
var opt_indent_char = options.indent_char || ' ';
|
||||
var opt_preserve_newlines =
|
||||
typeof options.preserve_newlines === 'undefined' ? true : options.preserve_newlines;
|
||||
var opt_indent_level = options.indent_level || 0; // starting indentation
|
||||
var opt_space_after_anon_function = options.space_after_anon_function === 'undefined' ? false : options.space_after_anon_function;
|
||||
|
||||
|
||||
function trim_output()
|
||||
@ -485,6 +489,11 @@ function js_beautify(js_source_text, options)
|
||||
// do nothing on (( and )( and ][ and ]( ..
|
||||
} else if (last_type !== 'TK_WORD' && last_type !== 'TK_OPERATOR') {
|
||||
print_space();
|
||||
} else if (last_word === 'function') {
|
||||
// function() vs function ()
|
||||
if (opt_space_after_anon_function) {
|
||||
print_space();
|
||||
}
|
||||
} else if (in_array(last_word, line_starters)) {
|
||||
print_space();
|
||||
}
|
||||
@ -586,13 +595,13 @@ function js_beautify(js_source_text, options)
|
||||
} else if ((last_type === 'TK_START_EXPR' || last_text === '=' || last_text === ',') && token_text === 'function') {
|
||||
// no need to force newline on 'function': (function
|
||||
// DONOTHING
|
||||
} else if (last_type === 'TK_WORD' && (last_text === 'return' || last_text === 'throw')) {
|
||||
} else if (last_text === 'return' || last_text === 'throw') {
|
||||
// no newline between 'return nnn'
|
||||
print_space();
|
||||
} else if (last_type !== 'TK_END_EXPR') {
|
||||
if ((last_type !== 'TK_START_EXPR' || token_text !== 'var') && last_text !== ':') {
|
||||
// no need to force newline on 'var': for (var x = 0...)
|
||||
if (token_text === 'if' && last_type === 'TK_WORD' && last_word === 'else') {
|
||||
if (token_text === 'if' && last_word === 'else') {
|
||||
// no newline for } else if {
|
||||
print_space();
|
||||
} else {
|
||||
@ -683,7 +692,7 @@ function js_beautify(js_source_text, options)
|
||||
print_token();
|
||||
print_newline();
|
||||
} else {
|
||||
// EXPR od DO_BLOCK
|
||||
// EXPR or DO_BLOCK
|
||||
print_token();
|
||||
print_space();
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ function do_js_beautify()
|
||||
document.getElementById('content').value = style_html(js_source, indent_size, indent_char, 80);
|
||||
} else {
|
||||
document.getElementById('content').value =
|
||||
js_beautify(unpacker_filter(js_source), {indent_size: indent_size, indent_char: indent_char, preserve_newlines:preserve_newlines});
|
||||
js_beautify(unpacker_filter(js_source), {indent_size: indent_size, indent_char: indent_char, preserve_newlines:preserve_newlines, space_after_anon_function:true});
|
||||
}
|
||||
|
||||
document.getElementById('beautify').disabled = false;
|
||||
|
Loading…
x
Reference in New Issue
Block a user