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

Allman style formatting for JavaScript files

Open UAK-35 opened this issue 4 years ago • 1 comments

Description

Allman style formatting for JavaScript required as below:

  1. start and end braces on separate lines
  2. start and end square brackets on separate lines
  3. array items on new line

UAK-35 avatar May 03 '20 11:05 UAK-35

I'd also want to use Allman-8 which practically means that you use 8 space width tab characters and indent everything with a single tab character instead of 1 or more spaces.

Note that there's one special case. Following doesn't work in JavaScript because of ASI rules which have a special exception for return statement:

return
{
        status: "successful",
        user:
        {
               id: "9abf38a3-c2f5-4159-a1be-0eccbc1b2349",
               label: "John Doe",
        }
};

because it will be interpreted as (note the semicolon after return!):

return;
{
        status: "successful",
        user:
        {
               id: "9abf38a3-c2f5-4159-a1be-0eccbc1b2349",
               label: "John Doe",
        }
};

You have to either use syntax

let response =
{
        status: "successful",
        user:
        {
               id: "9abf38a3-c2f5-4159-a1be-0eccbc1b2349",
               label: "John Doe",
        }
};
return response;

or

return {
        status: "successful",
        user:
        {
               id: "9abf38a3-c2f5-4159-a1be-0eccbc1b2349",
               label: "John Doe",
        }
};

instead.

I personally think that naming the result structures is more readable in any case so I use that.

mikkorantalainen avatar Feb 02 '22 09:02 mikkorantalainen