mirror of
https://github.com/beautifier/js-beautify.git
synced 2025-03-04 02:58:14 +00:00
Implement a new option, break_chained_methods: False(default): leave the chained methods intact, i.e a().b() -> a().b() a() -> a() .b() .b() True: always break chained methods with a newline. a().b() -> a() .b() a() -> a() .b() .b() Add a checkbox to turn the feature on/off to the frontpage.
This commit is contained in:
parent
693456699b
commit
595d8125a3
25
beautify.js
25
beautify.js
@ -82,15 +82,16 @@ function js_beautify(js_source_text, options) {
|
||||
opt_brace_style = options.brace_style ? options.brace_style : (opt_brace_style ? opt_brace_style : "collapse");
|
||||
|
||||
|
||||
var opt_indent_size = options.indent_size ? options.indent_size : 4;
|
||||
var opt_indent_char = options.indent_char ? options.indent_char : ' ';
|
||||
var opt_preserve_newlines = typeof options.preserve_newlines === 'undefined' ? true : options.preserve_newlines;
|
||||
var opt_max_preserve_newlines = typeof options.max_preserve_newlines === 'undefined' ? false : options.max_preserve_newlines;
|
||||
var opt_jslint_happy = options.jslint_happy === 'undefined' ? false : options.jslint_happy;
|
||||
var opt_keep_array_indentation = typeof options.keep_array_indentation === 'undefined' ? false : options.keep_array_indentation;
|
||||
var opt_space_before_conditional = typeof options.space_before_conditional === 'undefined' ? true : options.space_before_conditional;
|
||||
var opt_indent_case = typeof options.indent_case === 'undefined' ? false : options.indent_case;
|
||||
var opt_unescape_strings = typeof options.unescape_strings === 'undefined' ? false : options.unescape_strings;
|
||||
var opt_indent_size = options.indent_size ? options.indent_size : 4,
|
||||
opt_indent_char = options.indent_char ? options.indent_char : ' ',
|
||||
opt_preserve_newlines = typeof options.preserve_newlines === 'undefined' ? true : options.preserve_newlines,
|
||||
opt_break_chained_methods = typeof options.break_chained_methods === 'undefined' ? false : options.break_chained_methods,
|
||||
opt_max_preserve_newlines = typeof options.max_preserve_newlines === 'undefined' ? false : options.max_preserve_newlines,
|
||||
opt_jslint_happy = options.jslint_happy === 'undefined' ? false : options.jslint_happy,
|
||||
opt_keep_array_indentation = typeof options.keep_array_indentation === 'undefined' ? false : options.keep_array_indentation,
|
||||
opt_space_before_conditional = typeof options.space_before_conditional === 'undefined' ? true : options.space_before_conditional,
|
||||
opt_indent_case = typeof options.indent_case === 'undefined' ? false : options.indent_case,
|
||||
opt_unescape_strings = typeof options.unescape_strings === 'undefined' ? false : options.unescape_strings;
|
||||
|
||||
just_added_newline = false;
|
||||
|
||||
@ -808,8 +809,10 @@ function js_beautify(js_source_text, options) {
|
||||
if (is_special_word(last_text)) {
|
||||
print_single_space();
|
||||
} else if (last_text === ')') {
|
||||
flags.chain_extra_indentation = 1;
|
||||
print_newline(true /* ignore_repeated */, false /* reset_statement_flags */);
|
||||
if (opt_break_chained_methods || wanted_newline) {
|
||||
flags.chain_extra_indentation = 1;
|
||||
print_newline(true /* ignore_repeated */, false /* reset_statement_flags */);
|
||||
}
|
||||
}
|
||||
|
||||
print_token();
|
||||
|
47
index.html
47
index.html
@ -148,6 +148,7 @@ function read_settings_from_cookie() {
|
||||
$('#detect-packers').attr('checked', $.cookie('detect-packers') !== 'off');
|
||||
$('#preserve-newlines').attr('checked', $.cookie('preserve-newlines') !== 'off');
|
||||
$('#keep-array-indentation').attr('checked', $.cookie('keep-array-indentation') === 'on');
|
||||
$('#break-chained-methods').attr('checked', $.cookie('break-chained-methods') === 'on');
|
||||
$('#indent-scripts').val(any($.cookie('indent-scripts'), 'normal'));
|
||||
$('#space-before-conditional').attr('checked', $.cookie('space-before-conditional') !== 'off');
|
||||
$('#unescape-strings').attr('checked', $.cookie('unescape-strings') === 'on');
|
||||
@ -160,6 +161,7 @@ function store_settings_to_cookie() {
|
||||
$.cookie('detect-packers', $('#detect-packers').attr('checked') ? 'on' : 'off', opts);
|
||||
$.cookie('preserve-newlines', $('#preserve-newlines').attr('checked') ? 'on' : 'off', opts);
|
||||
$.cookie('keep-array-indentation', $('#keep-array-indentation').attr('checked') ? 'on' : 'off', opts);
|
||||
$.cookie('break-chained-methods', $('#break-chained-methods').attr('checked') ? 'on' : 'off', opts);
|
||||
$.cookie('space-before-conditional', $('#space-before-conditional').attr('checked') ? 'on' : 'off', opts);
|
||||
$.cookie('unescape-strings', $('#unescape-strings').attr('checked') ? 'on' : 'off', opts);
|
||||
$.cookie('indent-scripts', $('#indent-scripts').val(), opts);
|
||||
@ -206,41 +208,29 @@ function unpacker_filter(source) {
|
||||
}
|
||||
|
||||
|
||||
function beautify() {
|
||||
function beautify()
|
||||
{
|
||||
if (the.beautify_in_progress) return;
|
||||
|
||||
store_settings_to_cookie();
|
||||
|
||||
the.beautify_in_progress = true;
|
||||
|
||||
var source;
|
||||
if (the.editor) {
|
||||
source = the.editor.getValue();
|
||||
} else {
|
||||
source = $('#source').val();
|
||||
}
|
||||
var indent_size = $('#tabsize').val();
|
||||
var indent_char = indent_size == 1 ? '\t' : ' ';
|
||||
var preserve_newlines = $('#preserve-newlines').attr('checked');
|
||||
var keep_array_indentation = $('#keep-array-indentation').attr('checked');
|
||||
var indent_scripts = $('#indent-scripts').val();
|
||||
var brace_style = $('#brace-style').val();
|
||||
var space_before_conditional = $('#space-before-conditional').attr('checked');
|
||||
var unescape_strings = $('#unescape-strings').attr('checked');
|
||||
var source = the.editor ? the.editor.getValue() : $('#source').val(),
|
||||
output,
|
||||
opts = {};
|
||||
|
||||
opts.indent_size = $('#tabsize').val();
|
||||
opts.indent_char = opts.indent_size == 1 ? '\t' : ' ';
|
||||
opts.preserve_newlines = $('#preserve-newlines').attr('checked');
|
||||
opts.keep_array_indentation = $('#keep-array-indentation').attr('checked');
|
||||
opts.break_chained_methods = $('#break-chained-methods').attr('checked');
|
||||
opts.indent_scripts = $('#indent-scripts').val();
|
||||
opts.brace_style = $('#brace-style').val();
|
||||
opts.space_before_conditional = $('#space-before-conditional').attr('checked');
|
||||
opts.unescape_strings = $('#unescape-strings').attr('checked');
|
||||
opts.space_after_anon_function = true;
|
||||
|
||||
var opts = {
|
||||
indent_size: indent_size,
|
||||
indent_char: indent_char,
|
||||
preserve_newlines:preserve_newlines,
|
||||
brace_style: brace_style,
|
||||
keep_array_indentation:keep_array_indentation,
|
||||
space_after_anon_function:true,
|
||||
space_before_conditional: space_before_conditional,
|
||||
unescape_strings: unescape_strings,
|
||||
indent_scripts:indent_scripts};
|
||||
|
||||
var output;
|
||||
if (looks_like_html(source)) {
|
||||
output = style_html(source, opts);
|
||||
} else {
|
||||
@ -338,9 +328,10 @@ $(function() {
|
||||
<input class="checkbox" type="checkbox" id="preserve-newlines"><label for="preserve-newlines"> Preserve empty lines?</label><br>
|
||||
<input class="checkbox" type="checkbox" id="detect-packers"><label for="detect-packers"> Detect packers and obfuscators?</label><br>
|
||||
<input class="checkbox" type="checkbox" id="keep-array-indentation"><label for="keep-array-indentation"> Keep array indentation?</label><br>
|
||||
<input class="checkbox" type="checkbox" id="break-chained-methods"><label for="break-chained-methods"> Break lines on chained methods?</label><br>
|
||||
<input class="checkbox" type="checkbox" id="space-before-conditional"><label for="space-before-conditional"> Space before conditional: "if(x)" / "if (x)"</label><br>
|
||||
<input class="checkbox" type="checkbox" id="unescape-strings"><label for="unescape-strings"> Unescape printable chars encoded as \xNN or \uNNNN?</label>
|
||||
<br><a href="?without-codemirror" class="turn-off-codemirror">Turn off bells and whistles?</a>
|
||||
<br><a href="?without-codemirror" class="turn-off-codemirror">Use a simple textarea for code input?</a>
|
||||
|
||||
|
||||
</td></tr>
|
||||
|
@ -43,6 +43,7 @@ class BeautifierOptions:
|
||||
self.keep_function_indentation = False
|
||||
self.eval_code = False
|
||||
self.unescape_strings = False
|
||||
self.break_chained_methods = False
|
||||
|
||||
|
||||
|
||||
@ -1138,8 +1139,9 @@ class Beautifier:
|
||||
if self.is_special_word(self.last_text):
|
||||
self.append(' ')
|
||||
elif self.last_text == ')':
|
||||
self.flags.chain_extra_indentation = 1;
|
||||
self.append_newline(True, False)
|
||||
if self.opts.break_chained_methods or self.wanted_newline:
|
||||
self.flags.chain_extra_indentation = 1;
|
||||
self.append_newline(True, False)
|
||||
self.append(token_text)
|
||||
|
||||
def handle_unknown(self, token_text):
|
||||
|
@ -148,7 +148,7 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
bt("for(var a=1,b=2)", "for (var a = 1, b = 2)");
|
||||
bt("for(var a=1,b=2,c=3)", "for (var a = 1, b = 2, c = 3)");
|
||||
bt("for(var a=1,b=2,c=3;d<3;d++)", "for (var a = 1, b = 2, c = 3; d < 3; d++)");
|
||||
bt("function x(){(a||b).c()}", "function x() {\n (a || b)\n .c()\n}");
|
||||
bt("function x(){(a||b).c()}", "function x() {\n (a || b).c()\n}");
|
||||
bt("function x(){return - 1}", "function x() {\n return -1\n}");
|
||||
bt("function x(){return ! a}", "function x() {\n return !a\n}");
|
||||
|
||||
@ -465,6 +465,7 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
bt('if (foo) // comment\n(bar());');
|
||||
bt('if (foo) // comment\n/asdf/;');
|
||||
|
||||
self.options.break_chained_methods = True
|
||||
bt('foo.bar().baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat)');
|
||||
bt('foo.bar().baz().cucumber(fat); foo.bar().baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat);\nfoo.bar()\n .baz()\n .cucumber(fat)');
|
||||
bt('foo.bar().baz().cucumber(fat)\n foo.bar().baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat)\nfoo.bar()\n .baz()\n .cucumber(fat)');
|
||||
@ -500,6 +501,7 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
options.keep_array_indentation = False
|
||||
options.brace_style = 'collapse'
|
||||
options.indent_level = 0
|
||||
options.break_chained_methods = False
|
||||
|
||||
cls.options = options
|
||||
cls.wrapregex = re.compile('^(.+)$', re.MULTILINE)
|
||||
|
@ -14,7 +14,8 @@ var opts = {
|
||||
jslint_happy: false,
|
||||
keep_array_indentation: false,
|
||||
brace_style: 'collapse',
|
||||
space_before_conditional: true
|
||||
space_before_conditional: true,
|
||||
break_chained_methods: false
|
||||
};
|
||||
|
||||
function test_beautifier(input)
|
||||
@ -191,7 +192,7 @@ function run_beautifier_tests(test_obj)
|
||||
bt("for(var a=1,b=2)", "for (var a = 1, b = 2)");
|
||||
bt("for(var a=1,b=2,c=3)", "for (var a = 1, b = 2, c = 3)");
|
||||
bt("for(var a=1,b=2,c=3;d<3;d++)", "for (var a = 1, b = 2, c = 3; d < 3; d++)");
|
||||
bt("function x(){(a||b).c()}", "function x() {\n (a || b)\n .c()\n}");
|
||||
bt("function x(){(a||b).c()}", "function x() {\n (a || b).c()\n}");
|
||||
bt("function x(){return - 1}", "function x() {\n return -1\n}");
|
||||
bt("function x(){return ! a}", "function x() {\n return !a\n}");
|
||||
|
||||
@ -522,6 +523,7 @@ function run_beautifier_tests(test_obj)
|
||||
bt('if(foo) // comment\n/asdf/;');
|
||||
|
||||
|
||||
opts.break_chained_methods = true;
|
||||
bt('foo.bar().baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat)');
|
||||
bt('foo.bar().baz().cucumber(fat); foo.bar().baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat);\nfoo.bar()\n .baz()\n .cucumber(fat)');
|
||||
bt('foo.bar().baz().cucumber(fat)\n foo.bar().baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat)\nfoo.bar()\n .baz()\n .cucumber(fat)');
|
||||
|
Loading…
x
Reference in New Issue
Block a user