Skip to content

Commit

Permalink
Merge pull request #1240 from aecepoglu/unindent-chains-2
Browse files Browse the repository at this point in the history
undindent-chained-methods option. Resolves #482
  • Loading branch information
bitwiseman committed Sep 7, 2017
2 parents b4fde17 + 258a361 commit ed61dc1
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -122,6 +122,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|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
-k, --keep-array-indentation Preserve array indentation
-x, --unescape-strings Decode printable characters encoded in xNN notation
Expand Down Expand Up @@ -150,6 +151,7 @@ Which correspond to the underscored option keys for both library interfaces
"jslint_happy": false,
"space_after_anon_function": false,
"brace_style": "collapse",
"unindent_chained_methods": false,
"break_chained_methods": false,
"keep_array_indentation": false,
"unescape_strings": false,
Expand Down
7 changes: 5 additions & 2 deletions js/lib/beautify.js
Expand Up @@ -451,6 +451,7 @@ function Beautifier(js_source_text, options) {
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;
Expand Down Expand Up @@ -738,7 +739,7 @@ function Beautifier(js_source_text, options) {
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) {
remove_redundant_indentation(output, previous_flags);
}
}
Expand Down Expand Up @@ -767,7 +768,9 @@ function Beautifier(js_source_text, options) {
) {

set_mode(MODE.Statement);
indent();
if (!opt.unindent_chained_methods) {
indent();
}

handle_whitespace_and_comments(current_token, true);

Expand Down
3 changes: 3 additions & 0 deletions js/lib/cli.js
Expand Up @@ -73,6 +73,7 @@ var path = require('path'),
"jslint_happy": Boolean,
"space_after_anon_function": Boolean,
"brace_style": "brace_style", //See above for validation
"unindent_chained_methods": Boolean,
"break_chained_methods": Boolean,
"keep_array_indentation": Boolean,
"unescape_strings": Boolean,
Expand Down Expand Up @@ -124,6 +125,7 @@ var path = require('path'),
"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"],
Expand Down Expand Up @@ -336,6 +338,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|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');
msg.push(' -k, --keep-array-indentation Preserve array indentation');
msg.push(' -x, --unescape-strings Decode printable characters encoded in xNN notation');
Expand Down
7 changes: 5 additions & 2 deletions js/src/javascript/beautifier.js
Expand Up @@ -200,6 +200,7 @@ function Beautifier(js_source_text, options) {
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;
Expand Down Expand Up @@ -487,7 +488,7 @@ function Beautifier(js_source_text, options) {
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) {
remove_redundant_indentation(output, previous_flags);
}
}
Expand Down Expand Up @@ -516,7 +517,9 @@ function Beautifier(js_source_text, options) {
) {

set_mode(MODE.Statement);
indent();
if (!opt.unindent_chained_methods) {
indent();
}

handle_whitespace_and_comments(current_token, true);

Expand Down
32 changes: 32 additions & 0 deletions js/test/generated/beautify-javascript-tests.js
Expand Up @@ -968,6 +968,38 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
');');


//============================================================
// Unindent chained functions - ()
reset_options();
opts.unindent_chained_methods = true;
bt(
'f().f().f()\n' +
' .f().f();',
// -- output --
'f().f().f()\n' +
'.f().f();');
bt(
'f()\n' +
' .f()\n' +
' .f();',
// -- output --
'f()\n' +
'.f()\n' +
'.f();');
bt(
'f(function() {\n' +
' f()\n' +
' .f()\n' +
' .f();\n' +
'});',
// -- output --
'f(function() {\n' +
' f()\n' +
' .f()\n' +
' .f();\n' +
'});');


//============================================================
// Space in parens tests - (s = "", e = "")
reset_options();
Expand Down
5 changes: 3 additions & 2 deletions python/jsbeautifier/javascript/beautifier.py
Expand Up @@ -371,7 +371,7 @@ def restore_mode(self):
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:
remove_redundant_indentation(self.output, self.previous_flags)


Expand All @@ -397,7 +397,8 @@ def start_of_statement(self, current_token):
):

self.set_mode(MODE.Statement)
self.indent()
if not self.opts.unindent_chained_methods:
self.indent()

self.handle_whitespace_and_comments(current_token, True);

Expand Down
1 change: 1 addition & 0 deletions python/jsbeautifier/javascript/options.py
Expand Up @@ -41,6 +41,7 @@ def __init__(self):
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
Expand Down
32 changes: 32 additions & 0 deletions python/jsbeautifier/tests/generated/tests.py
Expand Up @@ -796,6 +796,38 @@ def unicode_char(value):
');')


#============================================================
# Unindent chained functions - ()
self.reset_options();
self.options.unindent_chained_methods = true
bt(
'f().f().f()\n' +
' .f().f();',
# -- output --
'f().f().f()\n' +
'.f().f();')
bt(
'f()\n' +
' .f()\n' +
' .f();',
# -- output --
'f()\n' +
'.f()\n' +
'.f();')
bt(
'f(function() {\n' +
' f()\n' +
' .f()\n' +
' .f();\n' +
'});',
# -- output --
'f(function() {\n' +
' f()\n' +
' .f()\n' +
' .f();\n' +
'});')


#============================================================
# Space in parens tests - (s = "", e = "")
self.reset_options();
Expand Down
47 changes: 47 additions & 0 deletions test/data/javascript/tests.js
Expand Up @@ -405,6 +405,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",
Expand Down

0 comments on commit ed61dc1

Please sign in to comment.