js-beautify icon indicating copy to clipboard operation
js-beautify copied to clipboard

Comma First adds unwanted new lines to arrays

Open nckirik opened this issue 2 years ago • 4 comments

Description

when "comma_first" is set to true, new lines are added between elements each time script is run. this problem occurs only in arrays, objects look fine. also, if "max_preserve_newlines" is set to 0, this wont happen. but new lines intentionally added are stripped.

Input

The code looked like this before beautification:

a = [
  1,
  2,
  3,
  4,
  5,
 ];

b = {
a: 1,
b: 2
}
  

Current Output

The code actually looked like this after beautification: (problem shows itself after second run)

a = [
  1
  
	, 2
  
	, 3
  
	, 4
  
	, 5
 ];
b = {
	a: 1
	, b: 2
}

Expected Output

The code should have looked like this after beautification:

a = [
    1
    , 2
    , 3
    , 4
    , 5
];

b = {
    a: 1
    , b: 2
}

Environment

Browser User Agent:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36

Language Selected: Beautify JavaScript

Settings

{
  "indent_size": "4",
  "indent_char": " ",
  "max_preserve_newlines": "1",
  "preserve_newlines": true,
  "keep_array_indentation": true,
  "break_chained_methods": false,
  "indent_scripts": "normal",
  "brace_style": "collapse",
  "space_before_conditional": true,
  "unescape_strings": false,
  "jslint_happy": false,
  "end_with_newline": false,
  "wrap_line_length": "0",
  "indent_inner_html": false,
  "comma_first": true,
  "e4x": true,
  "indent_empty_lines": true,
  "space-in-paren": 1,
  "indent-with-tabs": true
}

nckirik avatar Mar 30 '22 13:03 nckirik

Beautifying multiple times results in additional empty lines:

a = [
	1
	
	
	, 2
	
	
	, 3
	
	
	, 4
	
	
	, 5
	

, ];
b = {
	a: 1
	, b: 2
}

@n-can-k

I see you have:

  "keep_array_indentation": true,

If you set that to false, you will get:

a = [
  1
  , 2
  , 3
  , 4
  , 5
 , ];
b = {
	a: 1
	, b: 2
}

Still not quite what you want, but closer. It also does not indent with tabs in this case. The comma first formatting definitely needs work.

bitwiseman avatar Mar 31 '22 03:03 bitwiseman

@bitwiseman i'll try to spare some time to see if I can work on it

nckirik avatar Mar 31 '22 22:03 nckirik

Hey do you continue to get the same result even after doing: "keep_array_indentation": false, if not please let me know, I can look into it.

pritipsingh avatar Apr 12 '22 13:04 pritipsingh

yea, it works without "keep_array_indentation": false, thanks. also, indentation looks ok

nckirik avatar Apr 14 '22 14:04 nckirik