mirror of
https://github.com/beautifier/js-beautify.git
synced 2025-02-17 11:50:53 +00:00
Merge pull request #1556 from MacKLess/named_function
Add option for space before named function
This commit is contained in:
commit
5e1d87bd50
@ -180,6 +180,7 @@ Beautifier Options:
|
||||
-E, --space-in-empty-paren Add a single space inside empty paren, ie. f( )
|
||||
-j, --jslint-happy Enable jslint-stricter mode
|
||||
-a, --space-after-anon-function Add a space before an anonymous function's parens, ie. function ()
|
||||
--space-after-named-function Add a space before a named function's parens, i.e. function example ()
|
||||
-b, --brace-style [collapse|expand|end-expand|none][,preserve-inline] [collapse,preserve-inline]
|
||||
-u, --unindent-chained-methods Don't indent chained method calls
|
||||
-B, --break-chained-methods Break chained method calls across subsequent lines
|
||||
|
@ -78,6 +78,7 @@ var path = require('path'),
|
||||
"space_in_empty_paren": Boolean,
|
||||
"jslint_happy": Boolean,
|
||||
"space_after_anon_function": Boolean,
|
||||
"space_after_named_function": Boolean,
|
||||
"brace_style": "brace_style", //See above for validation
|
||||
"unindent_chained_methods": Boolean,
|
||||
"break_chained_methods": Boolean,
|
||||
@ -354,6 +355,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(' --space_after_named_function Add a space before a named function\'s parens, ie. function example ()');
|
||||
msg.push(' -b, --brace-style [collapse|expand|end-expand|none][,preserve-inline] [collapse,preserve-inline]');
|
||||
msg.push(' -u, --unindent-chained-methods Don\'t indent chained method calls');
|
||||
msg.push(' -B, --break-chained-methods Break chained method calls across subsequent lines');
|
||||
|
@ -567,6 +567,19 @@ Beautifier.prototype.handle_start_expr = function(current_token) {
|
||||
}
|
||||
} else if (this._flags.last_token.type === TOKEN.WORD) {
|
||||
this._output.space_before_token = false;
|
||||
|
||||
// function name() vs function name ()
|
||||
// function* name() vs function* name ()
|
||||
// async name() vs async name ()
|
||||
if (this._options.space_after_named_function) {
|
||||
// peek starts at next character so -1 is current token
|
||||
var peek_back_three = this._tokens.peek(-4);
|
||||
var peek_back_two = this._tokens.peek(-3);
|
||||
if (reserved_array(peek_back_two, ['async', 'function']) ||
|
||||
(reserved_array(peek_back_three, ['async', 'function']) && peek_back_two.text === '*')) {
|
||||
this._output.space_before_token = true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Support preserving wrapped arrow function expressions
|
||||
// a.b('c',
|
||||
|
@ -69,6 +69,7 @@ function Options(options) {
|
||||
this.space_in_empty_paren = this._get_boolean('space_in_empty_paren');
|
||||
this.jslint_happy = this._get_boolean('jslint_happy');
|
||||
this.space_after_anon_function = this._get_boolean('space_after_anon_function');
|
||||
this.space_after_named_function = this._get_boolean('space_after_named_function');
|
||||
this.keep_array_indentation = this._get_boolean('keep_array_indentation');
|
||||
this.space_before_conditional = this._get_boolean('space_before_conditional', true);
|
||||
this.unescape_strings = this._get_boolean('unescape_strings');
|
||||
@ -83,6 +84,7 @@ function Options(options) {
|
||||
if (this.jslint_happy) {
|
||||
this.space_after_anon_function = true;
|
||||
}
|
||||
|
||||
}
|
||||
Options.prototype = new BaseOptions();
|
||||
|
||||
|
@ -3151,6 +3151,14 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function () {}');
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function y(){}',
|
||||
// -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function y() {}');
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
@ -3163,6 +3171,18 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'var x = {\n' +
|
||||
' x: function () {}\n' +
|
||||
'}');
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
'x: function y(){}\n' +
|
||||
'}',
|
||||
// -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
' x: function y() {}\n' +
|
||||
'}');
|
||||
bt(
|
||||
'function () {\n' +
|
||||
' var a, b, c, d, e = [],\n' +
|
||||
@ -3199,6 +3219,12 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function () {},\n' +
|
||||
' d = \'\';');
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function yoohoo() {}, d = \'\';',
|
||||
// -- output --
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function yoohoo() {},\n' +
|
||||
' d = \'\';');
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function() {},\n' +
|
||||
'd = \'\';',
|
||||
@ -3214,6 +3240,14 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'function () {\n' +
|
||||
' alert(x);\n' +
|
||||
'}');
|
||||
bt(
|
||||
'var o2=$.extend(a);function yoohoo(){alert(x);}',
|
||||
// -- output --
|
||||
'var o2 = $.extend(a);\n' +
|
||||
'\n' +
|
||||
'function yoohoo() {\n' +
|
||||
' alert(x);\n' +
|
||||
'}');
|
||||
bt(
|
||||
'function*() {\n' +
|
||||
' yield 1;\n' +
|
||||
@ -3222,10 +3256,18 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'function* () {\n' +
|
||||
' yield 1;\n' +
|
||||
'}');
|
||||
bt(
|
||||
'function* yoohoo() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}');
|
||||
bt(
|
||||
'function* x() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}');
|
||||
bt(
|
||||
'async x() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}');
|
||||
|
||||
// jslint and space after anon function - (jslint_happy = "true", space_after_anon_function = "false")
|
||||
reset_options();
|
||||
@ -3244,6 +3286,14 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function () {}');
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function y(){}',
|
||||
// -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function y() {}');
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
@ -3256,6 +3306,18 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'var x = {\n' +
|
||||
' x: function () {}\n' +
|
||||
'}');
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
'x: function y(){}\n' +
|
||||
'}',
|
||||
// -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
' x: function y() {}\n' +
|
||||
'}');
|
||||
bt(
|
||||
'function () {\n' +
|
||||
' var a, b, c, d, e = [],\n' +
|
||||
@ -3292,6 +3354,12 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function () {},\n' +
|
||||
' d = \'\';');
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function yoohoo() {}, d = \'\';',
|
||||
// -- output --
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function yoohoo() {},\n' +
|
||||
' d = \'\';');
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function() {},\n' +
|
||||
'd = \'\';',
|
||||
@ -3307,6 +3375,14 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'function () {\n' +
|
||||
' alert(x);\n' +
|
||||
'}');
|
||||
bt(
|
||||
'var o2=$.extend(a);function yoohoo(){alert(x);}',
|
||||
// -- output --
|
||||
'var o2 = $.extend(a);\n' +
|
||||
'\n' +
|
||||
'function yoohoo() {\n' +
|
||||
' alert(x);\n' +
|
||||
'}');
|
||||
bt(
|
||||
'function*() {\n' +
|
||||
' yield 1;\n' +
|
||||
@ -3315,10 +3391,18 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'function* () {\n' +
|
||||
' yield 1;\n' +
|
||||
'}');
|
||||
bt(
|
||||
'function* yoohoo() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}');
|
||||
bt(
|
||||
'function* x() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}');
|
||||
bt(
|
||||
'async x() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}');
|
||||
|
||||
// jslint and space after anon function - (jslint_happy = "false", space_after_anon_function = "true")
|
||||
reset_options();
|
||||
@ -3337,6 +3421,14 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function () {}');
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function y(){}',
|
||||
// -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function y() {}');
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
@ -3349,6 +3441,18 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'var x = {\n' +
|
||||
' x: function () {}\n' +
|
||||
'}');
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
'x: function y(){}\n' +
|
||||
'}',
|
||||
// -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
' x: function y() {}\n' +
|
||||
'}');
|
||||
bt(
|
||||
'function () {\n' +
|
||||
' var a, b, c, d, e = [],\n' +
|
||||
@ -3385,6 +3489,12 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function () {},\n' +
|
||||
' d = \'\';');
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function yoohoo() {}, d = \'\';',
|
||||
// -- output --
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function yoohoo() {},\n' +
|
||||
' d = \'\';');
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function() {},\n' +
|
||||
'd = \'\';',
|
||||
@ -3400,6 +3510,14 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'function () {\n' +
|
||||
' alert(x);\n' +
|
||||
'}');
|
||||
bt(
|
||||
'var o2=$.extend(a);function yoohoo(){alert(x);}',
|
||||
// -- output --
|
||||
'var o2 = $.extend(a);\n' +
|
||||
'\n' +
|
||||
'function yoohoo() {\n' +
|
||||
' alert(x);\n' +
|
||||
'}');
|
||||
bt(
|
||||
'function*() {\n' +
|
||||
' yield 1;\n' +
|
||||
@ -3408,10 +3526,18 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'function* () {\n' +
|
||||
' yield 1;\n' +
|
||||
'}');
|
||||
bt(
|
||||
'function* yoohoo() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}');
|
||||
bt(
|
||||
'function* x() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}');
|
||||
bt(
|
||||
'async x() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}');
|
||||
|
||||
// jslint and space after anon function - (jslint_happy = "false", space_after_anon_function = "false")
|
||||
reset_options();
|
||||
@ -3430,6 +3556,14 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function() {}');
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function y(){}',
|
||||
// -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function y() {}');
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
@ -3442,6 +3576,18 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'var x = {\n' +
|
||||
' x: function() {}\n' +
|
||||
'}');
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
'x: function y(){}\n' +
|
||||
'}',
|
||||
// -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
' x: function y() {}\n' +
|
||||
'}');
|
||||
bt(
|
||||
'function () {\n' +
|
||||
' var a, b, c, d, e = [],\n' +
|
||||
@ -3483,6 +3629,12 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function() {},\n' +
|
||||
' d = \'\';');
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function yoohoo() {}, d = \'\';',
|
||||
// -- output --
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function yoohoo() {},\n' +
|
||||
' d = \'\';');
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function() {},\n' +
|
||||
'd = \'\';',
|
||||
@ -3498,14 +3650,177 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'function() {\n' +
|
||||
' alert(x);\n' +
|
||||
'}');
|
||||
bt(
|
||||
'var o2=$.extend(a);function yoohoo(){alert(x);}',
|
||||
// -- output --
|
||||
'var o2 = $.extend(a);\n' +
|
||||
'\n' +
|
||||
'function yoohoo() {\n' +
|
||||
' alert(x);\n' +
|
||||
'}');
|
||||
bt(
|
||||
'function*() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}');
|
||||
bt(
|
||||
'function* yoohoo() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}');
|
||||
bt(
|
||||
'function* x() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}');
|
||||
bt(
|
||||
'async x() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}');
|
||||
|
||||
// jslint and space after anon function - (space_after_named_function = "true")
|
||||
reset_options();
|
||||
set_name('jslint and space after anon function - (space_after_named_function = "true")');
|
||||
opts.space_after_named_function = true;
|
||||
bt(
|
||||
'a=typeof(x)',
|
||||
// -- output --
|
||||
'a = typeof(x)');
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function(){}',
|
||||
// -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function() {}');
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function y(){}',
|
||||
// -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function y () {}');
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
'x: function(){}\n' +
|
||||
'}',
|
||||
// -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
' x: function() {}\n' +
|
||||
'}');
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
'x: function y(){}\n' +
|
||||
'}',
|
||||
// -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
' x: function y () {}\n' +
|
||||
'}');
|
||||
bt(
|
||||
'function () {\n' +
|
||||
' var a, b, c, d, e = [],\n' +
|
||||
' f;\n' +
|
||||
'}',
|
||||
// -- output --
|
||||
'function() {\n' +
|
||||
' var a, b, c, d, e = [],\n' +
|
||||
' f;\n' +
|
||||
'}');
|
||||
bt(
|
||||
'switch(x) {case 0: case 1: a(); break; default: break}',
|
||||
// -- output --
|
||||
'switch (x) {\n' +
|
||||
' case 0:\n' +
|
||||
' case 1:\n' +
|
||||
' a();\n' +
|
||||
' break;\n' +
|
||||
' default:\n' +
|
||||
' break\n' +
|
||||
'}');
|
||||
bt(
|
||||
'switch(x){case -1:break;case !y:break;}',
|
||||
// -- output --
|
||||
'switch (x) {\n' +
|
||||
' case -1:\n' +
|
||||
' break;\n' +
|
||||
' case !y:\n' +
|
||||
' break;\n' +
|
||||
'}');
|
||||
|
||||
// typical greasemonkey start
|
||||
test_fragment(
|
||||
'// comment 2\n' +
|
||||
'(function()');
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function() {}, d = \'\';',
|
||||
// -- output --
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function() {},\n' +
|
||||
' d = \'\';');
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function yoohoo() {}, d = \'\';',
|
||||
// -- output --
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function yoohoo () {},\n' +
|
||||
' d = \'\';');
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function() {},\n' +
|
||||
'd = \'\';',
|
||||
// -- output --
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function() {},\n' +
|
||||
' d = \'\';');
|
||||
bt(
|
||||
'var o2=$.extend(a);function(){alert(x);}',
|
||||
// -- output --
|
||||
'var o2 = $.extend(a);\n' +
|
||||
'\n' +
|
||||
'function() {\n' +
|
||||
' alert(x);\n' +
|
||||
'}');
|
||||
bt(
|
||||
'var o2=$.extend(a);function yoohoo(){alert(x);}',
|
||||
// -- output --
|
||||
'var o2 = $.extend(a);\n' +
|
||||
'\n' +
|
||||
'function yoohoo () {\n' +
|
||||
' alert(x);\n' +
|
||||
'}');
|
||||
bt(
|
||||
'function*() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}');
|
||||
bt(
|
||||
'function* yoohoo() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}',
|
||||
// -- output --
|
||||
'function* yoohoo () {\n' +
|
||||
' yield 1;\n' +
|
||||
'}');
|
||||
bt(
|
||||
'function* x() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}',
|
||||
// -- output --
|
||||
'function* x () {\n' +
|
||||
' yield 1;\n' +
|
||||
'}');
|
||||
bt(
|
||||
'async x() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}',
|
||||
// -- output --
|
||||
'async x () {\n' +
|
||||
' yield 1;\n' +
|
||||
'}');
|
||||
|
||||
|
||||
//============================================================
|
||||
|
@ -152,6 +152,7 @@ Output options:
|
||||
-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 ()
|
||||
--space-after-named-function Add a space before a named function's parens, i.e. function example ()
|
||||
-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
|
||||
@ -217,7 +218,7 @@ def main():
|
||||
'space-in-paren', 'space-in-empty-paren', 'jslint-happy', 'space-after-anon-function',
|
||||
'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'])
|
||||
'e4x', 'end-with-newline', 'comma-first', 'operator-position=', 'wrap-line-length', 'editorconfig', 'space-after-named-function'])
|
||||
except getopt.GetoptError as ex:
|
||||
print(ex, file=sys.stderr)
|
||||
return usage(sys.stderr)
|
||||
@ -257,6 +258,8 @@ def main():
|
||||
js_options.jslint_happy = True
|
||||
elif opt in ('--space_after_anon_function', '-a'):
|
||||
js_options.space_after_anon_function = True
|
||||
elif opt in ('--space_after_named_function'):
|
||||
js_options.space_after_named_function = True
|
||||
elif opt in ('--eval-code'):
|
||||
js_options.eval_code = True
|
||||
elif opt in ('--brace-style', '-b'):
|
||||
|
@ -495,6 +495,17 @@ class Beautifier:
|
||||
self.allow_wrap_or_preserved_newline(current_token)
|
||||
elif self._flags.last_token.type == TOKEN.WORD:
|
||||
self._output.space_before_token = False
|
||||
# function name() vs function name ()
|
||||
# function* name() vs function* name ()
|
||||
# async name() vs async name ()
|
||||
if self._options.space_after_named_function:
|
||||
# peek starts at next character so -1 is current token
|
||||
peek_back_three = self._tokens.peek(-4)
|
||||
peek_back_two = self._tokens.peek(-3)
|
||||
if reserved_array(peek_back_two, ['async', 'function']) or (
|
||||
reserved_array(peek_back_three, ['async', 'function']) and
|
||||
peek_back_two.text == '*'):
|
||||
self._output.space_before_token = True
|
||||
else:
|
||||
# Support preserving wrapped arrow function expressions
|
||||
# a.b('c',
|
||||
|
@ -75,6 +75,7 @@ class BeautifierOptions(BaseOptions):
|
||||
self.space_in_empty_paren = self._get_boolean('space_in_empty_paren')
|
||||
self.jslint_happy = self._get_boolean('jslint_happy')
|
||||
self.space_after_anon_function = self._get_boolean('space_after_anon_function')
|
||||
self.space_after_named_function = self._get_boolean('space_after_named_function')
|
||||
self.keep_array_indentation = self._get_boolean('keep_array_indentation')
|
||||
self.space_before_conditional = self._get_boolean('space_before_conditional', True)
|
||||
self.unescape_strings = self._get_boolean('unescape_strings')
|
||||
@ -90,4 +91,4 @@ class BeautifierOptions(BaseOptions):
|
||||
if self.jslint_happy:
|
||||
self.space_after_anon_function = True
|
||||
|
||||
self.eval_code = False
|
||||
self.eval_code = False
|
||||
|
@ -2911,6 +2911,14 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function () {}')
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function y(){}',
|
||||
# -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function y() {}')
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
@ -2923,6 +2931,18 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'var x = {\n' +
|
||||
' x: function () {}\n' +
|
||||
'}')
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
'x: function y(){}\n' +
|
||||
'}',
|
||||
# -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
' x: function y() {}\n' +
|
||||
'}')
|
||||
bt(
|
||||
'function () {\n' +
|
||||
' var a, b, c, d, e = [],\n' +
|
||||
@ -2959,6 +2979,12 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function () {},\n' +
|
||||
' d = \'\';')
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function yoohoo() {}, d = \'\';',
|
||||
# -- output --
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function yoohoo() {},\n' +
|
||||
' d = \'\';')
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function() {},\n' +
|
||||
'd = \'\';',
|
||||
@ -2974,6 +3000,14 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'function () {\n' +
|
||||
' alert(x);\n' +
|
||||
'}')
|
||||
bt(
|
||||
'var o2=$.extend(a);function yoohoo(){alert(x);}',
|
||||
# -- output --
|
||||
'var o2 = $.extend(a);\n' +
|
||||
'\n' +
|
||||
'function yoohoo() {\n' +
|
||||
' alert(x);\n' +
|
||||
'}')
|
||||
bt(
|
||||
'function*() {\n' +
|
||||
' yield 1;\n' +
|
||||
@ -2982,10 +3016,18 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'function* () {\n' +
|
||||
' yield 1;\n' +
|
||||
'}')
|
||||
bt(
|
||||
'function* yoohoo() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}')
|
||||
bt(
|
||||
'function* x() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}')
|
||||
bt(
|
||||
'async x() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}')
|
||||
|
||||
# jslint and space after anon function - (jslint_happy = "true", space_after_anon_function = "false")
|
||||
self.reset_options()
|
||||
@ -3003,6 +3045,14 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function () {}')
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function y(){}',
|
||||
# -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function y() {}')
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
@ -3015,6 +3065,18 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'var x = {\n' +
|
||||
' x: function () {}\n' +
|
||||
'}')
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
'x: function y(){}\n' +
|
||||
'}',
|
||||
# -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
' x: function y() {}\n' +
|
||||
'}')
|
||||
bt(
|
||||
'function () {\n' +
|
||||
' var a, b, c, d, e = [],\n' +
|
||||
@ -3051,6 +3113,12 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function () {},\n' +
|
||||
' d = \'\';')
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function yoohoo() {}, d = \'\';',
|
||||
# -- output --
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function yoohoo() {},\n' +
|
||||
' d = \'\';')
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function() {},\n' +
|
||||
'd = \'\';',
|
||||
@ -3066,6 +3134,14 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'function () {\n' +
|
||||
' alert(x);\n' +
|
||||
'}')
|
||||
bt(
|
||||
'var o2=$.extend(a);function yoohoo(){alert(x);}',
|
||||
# -- output --
|
||||
'var o2 = $.extend(a);\n' +
|
||||
'\n' +
|
||||
'function yoohoo() {\n' +
|
||||
' alert(x);\n' +
|
||||
'}')
|
||||
bt(
|
||||
'function*() {\n' +
|
||||
' yield 1;\n' +
|
||||
@ -3074,10 +3150,18 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'function* () {\n' +
|
||||
' yield 1;\n' +
|
||||
'}')
|
||||
bt(
|
||||
'function* yoohoo() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}')
|
||||
bt(
|
||||
'function* x() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}')
|
||||
bt(
|
||||
'async x() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}')
|
||||
|
||||
# jslint and space after anon function - (jslint_happy = "false", space_after_anon_function = "true")
|
||||
self.reset_options()
|
||||
@ -3095,6 +3179,14 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function () {}')
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function y(){}',
|
||||
# -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function y() {}')
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
@ -3107,6 +3199,18 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'var x = {\n' +
|
||||
' x: function () {}\n' +
|
||||
'}')
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
'x: function y(){}\n' +
|
||||
'}',
|
||||
# -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
' x: function y() {}\n' +
|
||||
'}')
|
||||
bt(
|
||||
'function () {\n' +
|
||||
' var a, b, c, d, e = [],\n' +
|
||||
@ -3143,6 +3247,12 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function () {},\n' +
|
||||
' d = \'\';')
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function yoohoo() {}, d = \'\';',
|
||||
# -- output --
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function yoohoo() {},\n' +
|
||||
' d = \'\';')
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function() {},\n' +
|
||||
'd = \'\';',
|
||||
@ -3158,6 +3268,14 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'function () {\n' +
|
||||
' alert(x);\n' +
|
||||
'}')
|
||||
bt(
|
||||
'var o2=$.extend(a);function yoohoo(){alert(x);}',
|
||||
# -- output --
|
||||
'var o2 = $.extend(a);\n' +
|
||||
'\n' +
|
||||
'function yoohoo() {\n' +
|
||||
' alert(x);\n' +
|
||||
'}')
|
||||
bt(
|
||||
'function*() {\n' +
|
||||
' yield 1;\n' +
|
||||
@ -3166,10 +3284,18 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'function* () {\n' +
|
||||
' yield 1;\n' +
|
||||
'}')
|
||||
bt(
|
||||
'function* yoohoo() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}')
|
||||
bt(
|
||||
'function* x() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}')
|
||||
bt(
|
||||
'async x() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}')
|
||||
|
||||
# jslint and space after anon function - (jslint_happy = "false", space_after_anon_function = "false")
|
||||
self.reset_options()
|
||||
@ -3187,6 +3313,14 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function() {}')
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function y(){}',
|
||||
# -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function y() {}')
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
@ -3199,6 +3333,18 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'var x = {\n' +
|
||||
' x: function() {}\n' +
|
||||
'}')
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
'x: function y(){}\n' +
|
||||
'}',
|
||||
# -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
' x: function y() {}\n' +
|
||||
'}')
|
||||
bt(
|
||||
'function () {\n' +
|
||||
' var a, b, c, d, e = [],\n' +
|
||||
@ -3240,6 +3386,12 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function() {},\n' +
|
||||
' d = \'\';')
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function yoohoo() {}, d = \'\';',
|
||||
# -- output --
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function yoohoo() {},\n' +
|
||||
' d = \'\';')
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function() {},\n' +
|
||||
'd = \'\';',
|
||||
@ -3255,14 +3407,176 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'function() {\n' +
|
||||
' alert(x);\n' +
|
||||
'}')
|
||||
bt(
|
||||
'var o2=$.extend(a);function yoohoo(){alert(x);}',
|
||||
# -- output --
|
||||
'var o2 = $.extend(a);\n' +
|
||||
'\n' +
|
||||
'function yoohoo() {\n' +
|
||||
' alert(x);\n' +
|
||||
'}')
|
||||
bt(
|
||||
'function*() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}')
|
||||
bt(
|
||||
'function* yoohoo() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}')
|
||||
bt(
|
||||
'function* x() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}')
|
||||
bt(
|
||||
'async x() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}')
|
||||
|
||||
# jslint and space after anon function - (space_after_named_function = "true")
|
||||
self.reset_options()
|
||||
self.options.space_after_named_function = true
|
||||
bt(
|
||||
'a=typeof(x)',
|
||||
# -- output --
|
||||
'a = typeof(x)')
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function(){}',
|
||||
# -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function() {}')
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function y(){}',
|
||||
# -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'function y () {}')
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
'x: function(){}\n' +
|
||||
'}',
|
||||
# -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
' x: function() {}\n' +
|
||||
'}')
|
||||
bt(
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
'x: function y(){}\n' +
|
||||
'}',
|
||||
# -- output --
|
||||
'x();\n' +
|
||||
'\n' +
|
||||
'var x = {\n' +
|
||||
' x: function y () {}\n' +
|
||||
'}')
|
||||
bt(
|
||||
'function () {\n' +
|
||||
' var a, b, c, d, e = [],\n' +
|
||||
' f;\n' +
|
||||
'}',
|
||||
# -- output --
|
||||
'function() {\n' +
|
||||
' var a, b, c, d, e = [],\n' +
|
||||
' f;\n' +
|
||||
'}')
|
||||
bt(
|
||||
'switch(x) {case 0: case 1: a(); break; default: break}',
|
||||
# -- output --
|
||||
'switch (x) {\n' +
|
||||
' case 0:\n' +
|
||||
' case 1:\n' +
|
||||
' a();\n' +
|
||||
' break;\n' +
|
||||
' default:\n' +
|
||||
' break\n' +
|
||||
'}')
|
||||
bt(
|
||||
'switch(x){case -1:break;case !y:break;}',
|
||||
# -- output --
|
||||
'switch (x) {\n' +
|
||||
' case -1:\n' +
|
||||
' break;\n' +
|
||||
' case !y:\n' +
|
||||
' break;\n' +
|
||||
'}')
|
||||
|
||||
# typical greasemonkey start
|
||||
test_fragment(
|
||||
'// comment 2\n' +
|
||||
'(function()')
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function() {}, d = \'\';',
|
||||
# -- output --
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function() {},\n' +
|
||||
' d = \'\';')
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function yoohoo() {}, d = \'\';',
|
||||
# -- output --
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function yoohoo () {},\n' +
|
||||
' d = \'\';')
|
||||
bt(
|
||||
'var a2, b2, c2, d2 = 0, c = function() {},\n' +
|
||||
'd = \'\';',
|
||||
# -- output --
|
||||
'var a2, b2, c2, d2 = 0,\n' +
|
||||
' c = function() {},\n' +
|
||||
' d = \'\';')
|
||||
bt(
|
||||
'var o2=$.extend(a);function(){alert(x);}',
|
||||
# -- output --
|
||||
'var o2 = $.extend(a);\n' +
|
||||
'\n' +
|
||||
'function() {\n' +
|
||||
' alert(x);\n' +
|
||||
'}')
|
||||
bt(
|
||||
'var o2=$.extend(a);function yoohoo(){alert(x);}',
|
||||
# -- output --
|
||||
'var o2 = $.extend(a);\n' +
|
||||
'\n' +
|
||||
'function yoohoo () {\n' +
|
||||
' alert(x);\n' +
|
||||
'}')
|
||||
bt(
|
||||
'function*() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}')
|
||||
bt(
|
||||
'function* yoohoo() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}',
|
||||
# -- output --
|
||||
'function* yoohoo () {\n' +
|
||||
' yield 1;\n' +
|
||||
'}')
|
||||
bt(
|
||||
'function* x() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}',
|
||||
# -- output --
|
||||
'function* x () {\n' +
|
||||
' yield 1;\n' +
|
||||
'}')
|
||||
bt(
|
||||
'async x() {\n' +
|
||||
' yield 1;\n' +
|
||||
'}',
|
||||
# -- output --
|
||||
'async x () {\n' +
|
||||
' yield 1;\n' +
|
||||
'}')
|
||||
|
||||
|
||||
#============================================================
|
||||
|
@ -1910,28 +1910,39 @@ exports.test_data = {
|
||||
{ name: "space_after_anon_function", value: "true" }
|
||||
],
|
||||
f: ' ',
|
||||
c: ''
|
||||
c: '',
|
||||
nf: ''
|
||||
}, {
|
||||
options: [
|
||||
{ name: "jslint_happy", value: "true" },
|
||||
{ name: "space_after_anon_function", value: "false" }
|
||||
],
|
||||
f: ' ',
|
||||
c: ''
|
||||
c: '',
|
||||
nf: ''
|
||||
}, {
|
||||
options: [
|
||||
{ name: "jslint_happy", value: "false" },
|
||||
{ name: "space_after_anon_function", value: "true" }
|
||||
],
|
||||
f: ' ',
|
||||
c: ' '
|
||||
c: ' ',
|
||||
nf: ''
|
||||
}, {
|
||||
options: [
|
||||
{ name: "jslint_happy", value: "false" },
|
||||
{ name: "space_after_anon_function", value: "false" }
|
||||
],
|
||||
f: '',
|
||||
c: ' '
|
||||
c: ' ',
|
||||
nf: ''
|
||||
}, {
|
||||
options: [
|
||||
{ name: "space_after_named_function", value: "true" }
|
||||
],
|
||||
f: '',
|
||||
c: ' ',
|
||||
nf: ' '
|
||||
}
|
||||
|
||||
|
||||
@ -1944,10 +1955,18 @@ exports.test_data = {
|
||||
input_: 'x();\n\nfunction(){}',
|
||||
output: 'x();\n\nfunction{{f}}() {}'
|
||||
},
|
||||
{
|
||||
input_: 'x();\n\nfunction y(){}',
|
||||
output: 'x();\n\nfunction y{{nf}}() {}'
|
||||
},
|
||||
{
|
||||
input_: 'x();\n\nvar x = {\nx: function(){}\n}',
|
||||
output: 'x();\n\nvar x = {\n x: function{{f}}() {}\n}'
|
||||
},
|
||||
{
|
||||
input_: 'x();\n\nvar x = {\nx: function y(){}\n}',
|
||||
output: 'x();\n\nvar x = {\n x: function y{{nf}}() {}\n}'
|
||||
},
|
||||
{
|
||||
input_: 'function () {\n var a, b, c, d, e = [],\n f;\n}',
|
||||
output: 'function{{f}}() {\n var a, b, c, d, e = [],\n f;\n}'
|
||||
@ -1971,6 +1990,10 @@ exports.test_data = {
|
||||
input_: 'var a2, b2, c2, d2 = 0, c = function() {}, d = \\\'\\\';',
|
||||
output: 'var a2, b2, c2, d2 = 0,\n c = function{{f}}() {},\n d = \\\'\\\';'
|
||||
},
|
||||
{
|
||||
input_: 'var a2, b2, c2, d2 = 0, c = function yoohoo() {}, d = \\\'\\\';',
|
||||
output: 'var a2, b2, c2, d2 = 0,\n c = function yoohoo{{nf}}() {},\n d = \\\'\\\';'
|
||||
},
|
||||
{
|
||||
input_: 'var a2, b2, c2, d2 = 0, c = function() {},\nd = \\\'\\\';',
|
||||
output: 'var a2, b2, c2, d2 = 0,\n c = function{{f}}() {},\n d = \\\'\\\';'
|
||||
@ -1979,8 +2002,26 @@ exports.test_data = {
|
||||
input_: 'var o2=$.extend(a);function(){alert(x);}',
|
||||
output: 'var o2 = $.extend(a);\n\nfunction{{f}}() {\n alert(x);\n}'
|
||||
},
|
||||
{ input: 'function*() {\n yield 1;\n}', output: 'function*{{f}}() {\n yield 1;\n}' },
|
||||
{ unchanged: 'function* x() {\n yield 1;\n}' }
|
||||
{
|
||||
input_: 'var o2=$.extend(a);function yoohoo(){alert(x);}',
|
||||
output: 'var o2 = $.extend(a);\n\nfunction yoohoo{{nf}}() {\n alert(x);\n}'
|
||||
},
|
||||
{
|
||||
input: 'function*() {\n yield 1;\n}',
|
||||
output: 'function*{{f}}() {\n yield 1;\n}'
|
||||
},
|
||||
{
|
||||
input: 'function* yoohoo() {\n yield 1;\n}',
|
||||
output: 'function* yoohoo{{nf}}() {\n yield 1;\n}'
|
||||
},
|
||||
{
|
||||
input: 'function* x() {\n yield 1;\n}',
|
||||
output: 'function* x{{nf}}() {\n yield 1;\n}'
|
||||
},
|
||||
{
|
||||
input: 'async x() {\n yield 1;\n}',
|
||||
output: 'async x{{nf}}() {\n yield 1;\n}'
|
||||
}
|
||||
]
|
||||
}, {
|
||||
name: "Regression tests",
|
||||
|
Loading…
x
Reference in New Issue
Block a user