Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Prevent comma first from adding extra newline
Fixes #775
  • Loading branch information
bitwiseman committed Jan 29, 2016
1 parent a8c5086 commit 754db78
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 9 deletions.
12 changes: 11 additions & 1 deletion js/lib/beautify.js
Expand Up @@ -489,7 +489,17 @@
if (opt.comma_first && last_type === 'TK_COMMA'
&& output.just_added_newline()) {
if(output.previous_line.last() === ',') {
output.previous_line.pop();
var popped = output.previous_line.pop();
// if the comma was already at the start of the line,
// pull back onto that line and reprint the indentation
if(output.previous_line.is_empty()) {
output.previous_line.push(popped);
output.trim(true);
output.current_line.pop();
output.trim();
}

// add the comma in front of the next token
print_token_line_indentation();
output.add_token(',');
output.space_before_token = true;
Expand Down
18 changes: 16 additions & 2 deletions js/test/generated/beautify-javascript-tests.js
Expand Up @@ -338,7 +338,7 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,

reset_options();
//============================================================
// Comma-first option - (c0 = ",\n", c1 = ",\n ", c2 = ",\n ", c3 = ",\n ")
// Comma-first option - (c0 = ",\n", c1 = ",\n ", c2 = ",\n ", c3 = ",\n ", f1 = " ,\n ")
opts.comma_first = false;
bt('{a:1, b:2}', '{\n a: 1,\n b: 2\n}');
bt('var a=1, b=c[d], e=6;', 'var a = 1,\n b = c[d],\n e = 6;');
Expand All @@ -355,8 +355,17 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
bt('a=[a[1],b[4],c[d[7]]]', 'a = [a[1], b[4], c[d[7]]]');
bt('[1,2,[3,4,[5,6],7],8]', '[1, 2, [3, 4, [5, 6], 7], 8]');
bt('[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]', '[\n [\n ["1", "2"],\n ["3", "4"]\n ],\n [\n ["5", "6", "7"],\n ["8", "9", "0"]\n ],\n [\n ["1", "2", "3"],\n ["4", "5", "6", "7"],\n ["8", "9", "0"]\n ]\n]');
bt(
'changeCollection.add({\n' +
' name: "Jonathan" // New line inserted after this line on every save\n' +
' , age: 25\n' +
'});',
'changeCollection.add({\n' +
' name: "Jonathan" // New line inserted after this line on every save\n' +
' ,\n age: 25\n' +
'});');

// Comma-first option - (c0 = "\n, ", c1 = "\n , ", c2 = "\n , ", c3 = "\n , ")
// Comma-first option - (c0 = "\n, ", c1 = "\n , ", c2 = "\n , ", c3 = "\n , ", f1 = ", ")
opts.comma_first = true;
bt('{a:1, b:2}', '{\n a: 1\n , b: 2\n}');
bt('var a=1, b=c[d], e=6;', 'var a = 1\n , b = c[d]\n , e = 6;');
Expand All @@ -373,6 +382,11 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
bt('a=[a[1],b[4],c[d[7]]]', 'a = [a[1], b[4], c[d[7]]]');
bt('[1,2,[3,4,[5,6],7],8]', '[1, 2, [3, 4, [5, 6], 7], 8]');
bt('[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]', '[\n [\n ["1", "2"]\n , ["3", "4"]\n ]\n , [\n ["5", "6", "7"]\n , ["8", "9", "0"]\n ]\n , [\n ["1", "2", "3"]\n , ["4", "5", "6", "7"]\n , ["8", "9", "0"]\n ]\n]');
bt(
'changeCollection.add({\n' +
' name: "Jonathan" // New line inserted after this line on every save\n' +
' , age: 25\n' +
'});');


reset_options();
Expand Down
11 changes: 10 additions & 1 deletion python/jsbeautifier/__init__.py
Expand Up @@ -524,7 +524,16 @@ def print_token(self, current_token, s=None):

if self.opts.comma_first and self.last_type == 'TK_COMMA' and self.output.just_added_newline():
if self.output.previous_line.last() == ',':
self.output.previous_line.pop()
# if the comma was already at the start of the line,
# pull back onto that line and reprint the indentation
popped = self.output.previous_line.pop()
if self.output.previous_line.is_empty():
self.output.previous_line.push(popped)
self.output.trim(True)
self.output.current_line.pop()
self.output.trim()

# add the comma in front of the next token
self.print_token_line_indentation(current_token)
self.output.add_token(',')
self.output.space_before_token = True
Expand Down
18 changes: 16 additions & 2 deletions python/jsbeautifier/tests/generated/tests.py
Expand Up @@ -164,7 +164,7 @@ def unicode_char(value):

self.reset_options();
#============================================================
# Comma-first option - (c0 = ",\n", c1 = ",\n ", c2 = ",\n ", c3 = ",\n ")
# Comma-first option - (c0 = ",\n", c1 = ",\n ", c2 = ",\n ", c3 = ",\n ", f1 = " ,\n ")
self.options.comma_first = false
bt('{a:1, b:2}', '{\n a: 1,\n b: 2\n}')
bt('var a=1, b=c[d], e=6;', 'var a = 1,\n b = c[d],\n e = 6;')
Expand All @@ -181,8 +181,17 @@ def unicode_char(value):
bt('a=[a[1],b[4],c[d[7]]]', 'a = [a[1], b[4], c[d[7]]]')
bt('[1,2,[3,4,[5,6],7],8]', '[1, 2, [3, 4, [5, 6], 7], 8]')
bt('[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]', '[\n [\n ["1", "2"],\n ["3", "4"]\n ],\n [\n ["5", "6", "7"],\n ["8", "9", "0"]\n ],\n [\n ["1", "2", "3"],\n ["4", "5", "6", "7"],\n ["8", "9", "0"]\n ]\n]')
bt(
'changeCollection.add({\n' +
' name: "Jonathan" // New line inserted after this line on every save\n' +
' , age: 25\n' +
'});',
'changeCollection.add({\n' +
' name: "Jonathan" // New line inserted after this line on every save\n' +
' ,\n age: 25\n' +
'});')

# Comma-first option - (c0 = "\n, ", c1 = "\n , ", c2 = "\n , ", c3 = "\n , ")
# Comma-first option - (c0 = "\n, ", c1 = "\n , ", c2 = "\n , ", c3 = "\n , ", f1 = ", ")
self.options.comma_first = true
bt('{a:1, b:2}', '{\n a: 1\n , b: 2\n}')
bt('var a=1, b=c[d], e=6;', 'var a = 1\n , b = c[d]\n , e = 6;')
Expand All @@ -199,6 +208,11 @@ def unicode_char(value):
bt('a=[a[1],b[4],c[d[7]]]', 'a = [a[1], b[4], c[d[7]]]')
bt('[1,2,[3,4,[5,6],7],8]', '[1, 2, [3, 4, [5, 6], 7], 8]')
bt('[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]', '[\n [\n ["1", "2"]\n , ["3", "4"]\n ]\n , [\n ["5", "6", "7"]\n , ["8", "9", "0"]\n ]\n , [\n ["1", "2", "3"]\n , ["4", "5", "6", "7"]\n , ["8", "9", "0"]\n ]\n]')
bt(
'changeCollection.add({\n' +
' name: "Jonathan" // New line inserted after this line on every save\n' +
' , age: 25\n' +
'});')


self.reset_options();
Expand Down
23 changes: 20 additions & 3 deletions test/data/javascript/tests.js
Expand Up @@ -166,15 +166,19 @@ exports.test_data = {
c0: ',\\n',
c1: ',\\n ',
c2: ',\\n ',
c3: ',\\n '
c3: ',\\n ',
// edge cases where engine bails
f1: ' ,\\n '
}, {
options: [
{ name: "comma_first", value: "true" }
],
c0: '\\n, ',
c1: '\\n , ',
c2: '\\n , ',
c3: '\\n , '
c3: '\\n , ',
// edge cases where engine bails
f1: ', '
}
],
tests: [
Expand All @@ -200,7 +204,20 @@ exports.test_data = {

{ input: '[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]',
output: '[\n [\n ["1", "2"]{{c2}}["3", "4"]\n ]{{c1}}[\n ["5", "6", "7"]{{c2}}["8", "9", "0"]\n ]{{c1}}[\n ["1", "2", "3"]{{c2}}["4", "5", "6", "7"]{{c2}}["8", "9", "0"]\n ]\n]' },

{
input: [
'changeCollection.add({',
' name: "Jonathan" // New line inserted after this line on every save',
' , age: 25',
'});'
],
output: [
'changeCollection.add({',
' name: "Jonathan" // New line inserted after this line on every save',
' {{f1}}age: 25',
'});'
]
}
],
}, {
name: "New Test Suite"
Expand Down

0 comments on commit 754db78

Please sign in to comment.