beautifier/js-beautify

Comma First adds unwanted new lines to arrays

Open

#2,010 opened on Mar 30, 2022

View on GitHub
 (5 comments) (0 reactions) (0 assignees)JavaScript (8,342 stars) (1,460 forks)batch import
good first issuelanguage: javascripttype: bug

Description

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
}

Contributor guide