mirror of
https://github.com/beautifier/js-beautify.git
synced 2024-11-23 12:49:40 +00:00
undindent-chained-methods option. Resolves #482
This commit is contained in:
parent
bdb91a2c30
commit
c5350f987e
@ -99,6 +99,7 @@ Beautifier Options:
|
||||
-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-preserve-inline|collapse|expand|end-expand|none] ["collapse"]
|
||||
-u, --unindent-chained-methods Don't indent chained method calls
|
||||
-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
|
||||
@ -126,6 +127,7 @@ These largely correspond to the underscored option keys for both library interfa
|
||||
"keep_array_indentation": false,
|
||||
"keep_function_indentation": false,
|
||||
"space_before_conditional": true,
|
||||
"unindent_chained_methods": false,
|
||||
"break_chained_methods": false,
|
||||
"eval_code": false,
|
||||
"unescape_strings": false,
|
||||
|
@ -318,6 +318,7 @@ if (!Object.values) {
|
||||
opt.indent_char = options.indent_char ? options.indent_char : ' ';
|
||||
opt.eol = options.eol ? options.eol : 'auto';
|
||||
opt.preserve_newlines = (options.preserve_newlines === undefined) ? true : options.preserve_newlines;
|
||||
opt.unindent_chained_methods = (options.unindent_chained_methods === undefined) ? false : options.unindent_chained_methods;
|
||||
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;
|
||||
@ -609,7 +610,7 @@ if (!Object.values) {
|
||||
if (flag_store.length > 0) {
|
||||
previous_flags = flags;
|
||||
flags = flag_store.pop();
|
||||
if (previous_flags.mode === MODE.Statement) {
|
||||
if (previous_flags.mode === MODE.Statement && !opt.unindent_chained_methods) {
|
||||
output.remove_redundant_indentation(previous_flags);
|
||||
}
|
||||
}
|
||||
@ -637,7 +638,10 @@ if (!Object.values) {
|
||||
) {
|
||||
|
||||
set_mode(MODE.Statement);
|
||||
indent();
|
||||
|
||||
if (!opt.unindent_chained_methods) {
|
||||
indent();
|
||||
}
|
||||
|
||||
if (last_type === 'TK_RESERVED' && in_array(flags.last_text, ['var', 'let', 'const']) && current_token.type === 'TK_WORD') {
|
||||
flags.declaration_statement = true;
|
||||
@ -2330,4 +2334,4 @@ if (!Object.values) {
|
||||
global.js_beautify = js_beautify;
|
||||
}
|
||||
|
||||
}());
|
||||
}());
|
||||
|
@ -57,6 +57,7 @@ var fs = require('fs'),
|
||||
"space_after_anon_function": Boolean,
|
||||
// TODO: expand-strict is obsolete, now identical to expand. Remove in future version
|
||||
"brace_style": ["collapse", "expand", "end-expand", "collapse-preserve-inline", "expand-strict", "none"],
|
||||
"unindent_chained_methods": Boolean,
|
||||
"break_chained_methods": Boolean,
|
||||
"keep_array_indentation": Boolean,
|
||||
"unescape_strings": Boolean,
|
||||
@ -102,6 +103,7 @@ var fs = require('fs'),
|
||||
"j": ["--jslint_happy"],
|
||||
"a": ["--space_after_anon_function"],
|
||||
"b": ["--brace_style"],
|
||||
"u": ["--unindent_chained_methods"],
|
||||
"B": ["--break_chained_methods"],
|
||||
"k": ["--keep_array_indentation"],
|
||||
"x": ["--unescape_strings"],
|
||||
@ -253,6 +255,7 @@ function usage(err) {
|
||||
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|collapse-preserve-inline|end-expand|none] ["collapse"]');
|
||||
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');
|
||||
msg.push(' -k, --keep-array-indentation Preserve array indentation');
|
||||
msg.push(' -x, --unescape-strings Decode printable characters encoded in xNN notation');
|
||||
|
@ -401,6 +401,35 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
|
||||
'});');
|
||||
|
||||
|
||||
reset_options();
|
||||
//============================================================
|
||||
// Unindent chained functions - ()
|
||||
opts.unindent_chained_methods = true;
|
||||
bt(
|
||||
'f().f().f()\n' +
|
||||
' .f().f();',
|
||||
'f().f().f()\n' +
|
||||
'.f().f();');
|
||||
bt(
|
||||
'f()\n' +
|
||||
' .f()\n' +
|
||||
' .f();',
|
||||
'f()\n' +
|
||||
'.f()\n' +
|
||||
'.f();');
|
||||
bt(
|
||||
'f(function() {\n' +
|
||||
' f()\n' +
|
||||
' .f()\n' +
|
||||
' .f();\n' +
|
||||
'});',
|
||||
'f(function() {\n' +
|
||||
' f()\n' +
|
||||
' .f()\n' +
|
||||
' .f();\n' +
|
||||
'});');
|
||||
|
||||
|
||||
reset_options();
|
||||
//============================================================
|
||||
// Space in parens tests - (s = "", e = "")
|
||||
|
@ -79,6 +79,7 @@ class BeautifierOptions:
|
||||
self.eval_code = False
|
||||
self.unescape_strings = False
|
||||
self.wrap_line_length = 0
|
||||
self.unindent_chained_methods = False
|
||||
self.break_chained_methods = False
|
||||
self.end_with_newline = False
|
||||
self.comma_first = False
|
||||
@ -593,7 +594,7 @@ class Beautifier:
|
||||
if len(self.flag_store) > 0:
|
||||
self.previous_flags = self.flags
|
||||
self.flags = self.flag_store.pop()
|
||||
if self.previous_flags.mode == MODE.Statement:
|
||||
if self.previous_flags.mode == MODE.Statement and not self.opts.unindent_chained_methods:
|
||||
self.output.remove_redundant_indentation(self.previous_flags)
|
||||
|
||||
|
||||
@ -618,7 +619,9 @@ class Beautifier:
|
||||
):
|
||||
|
||||
self.set_mode(MODE.Statement)
|
||||
self.indent()
|
||||
|
||||
if not self.opts.unindent_chained_methods:
|
||||
self.indent()
|
||||
|
||||
if self.last_type == 'TK_RESERVED' and self.flags.last_text in ['var', 'let', 'const'] and current_token.type == 'TK_WORD':
|
||||
self.flags.declaration_statement = True
|
||||
@ -1335,6 +1338,9 @@ class Beautifier:
|
||||
if self.start_of_statement(current_token):
|
||||
# The conditional starts the statement if appropriate.
|
||||
pass
|
||||
elif self.opts.unindent_chained_methods:
|
||||
self.deindent()
|
||||
|
||||
|
||||
if self.last_type == 'TK_RESERVED' and self.is_special_word(self.flags.last_text):
|
||||
self.output.space_before_token = True
|
||||
|
@ -226,6 +226,35 @@ class TestJSBeautifier(unittest.TestCase):
|
||||
'});')
|
||||
|
||||
|
||||
self.reset_options();
|
||||
#============================================================
|
||||
# Unindent chained functions - ()
|
||||
self.options.unindent_chained_methods = true
|
||||
bt(
|
||||
'f().f().f()\n' +
|
||||
' .f().f();',
|
||||
'f().f().f()\n' +
|
||||
'.f().f();')
|
||||
bt(
|
||||
'f()\n' +
|
||||
' .f()\n' +
|
||||
' .f();',
|
||||
'f()\n' +
|
||||
'.f()\n' +
|
||||
'.f();')
|
||||
bt(
|
||||
'f(function() {\n' +
|
||||
' f()\n' +
|
||||
' .f()\n' +
|
||||
' .f();\n' +
|
||||
'});',
|
||||
'f(function() {\n' +
|
||||
' f()\n' +
|
||||
' .f()\n' +
|
||||
' .f();\n' +
|
||||
'});')
|
||||
|
||||
|
||||
self.reset_options();
|
||||
#============================================================
|
||||
# Space in parens tests - (s = "", e = "")
|
||||
|
@ -239,6 +239,53 @@ exports.test_data = {
|
||||
]
|
||||
}
|
||||
],
|
||||
}, {
|
||||
name: "Unindent chained functions",
|
||||
description: "Don't indent chained functions if unindent_chained_functions is true",
|
||||
matrix: [{
|
||||
options: [
|
||||
{ name: "unindent_chained_methods", value: "true" }
|
||||
]
|
||||
}],
|
||||
tests: [{
|
||||
input: [
|
||||
'f().f().f()',
|
||||
' .f().f();',
|
||||
],
|
||||
output: [
|
||||
'f().f().f()',
|
||||
'.f().f();'
|
||||
]
|
||||
},
|
||||
{
|
||||
input: [
|
||||
'f()',
|
||||
' .f()',
|
||||
' .f();'
|
||||
],
|
||||
output: [
|
||||
'f()',
|
||||
'.f()',
|
||||
'.f();'
|
||||
]
|
||||
},
|
||||
{
|
||||
input: [
|
||||
'f(function() {',
|
||||
' f()',
|
||||
' .f()',
|
||||
' .f();',
|
||||
'});'
|
||||
],
|
||||
output: [
|
||||
'f(function() {',
|
||||
' f()',
|
||||
' .f()',
|
||||
' .f();',
|
||||
'});'
|
||||
]
|
||||
}
|
||||
],
|
||||
}, {
|
||||
name: "Space in parens tests",
|
||||
description: "put space inside parens",
|
||||
|
Loading…
Reference in New Issue
Block a user