Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comma-first JS formatter

Do you know of a JS formatter that will support the comma-first coding style?

var a = 'ape'
  , b = 'bat'
  , c = 'cat'
  , d = 'dog'
  , e = 'elf'
  , f = 'fly'
  , g = 'gnu'
  , h = 'hat'
  , i = 'ibu'
  ;

So far, I've looked at JS Beautifier & SourceFormatX but couldn't find an option for it.

like image 628
Yuriy Nemtsov Avatar asked Jun 03 '12 16:06

Yuriy Nemtsov


1 Answers

I modified the jsbeautifier code a little here:

http://jsfiddle.net/RabTN/29/

press doit to see the beautified code.

I specifically modified line 1080:

        if (flags.var_line) {
            if (token_text === ',') {
                if (flags.var_line_tainted) {
                    flags.var_line_reindented = true;
                    flags.var_line_tainted = false;
                    print_newline();
                    print_token();
                    print_single_space();
                    break;
                } else {

and line 1123

        if (token_text === ',') {
            if (flags.var_line) {
                if (flags.var_line_tainted) {
                    print_newline();
                    print_token();
                    print_single_space();

                    flags.var_line_tainted = false;
                } else {
                    print_newline();
                    print_token();
                    print_single_space();
                }
            } else if (last_type === 'TK_END_BLOCK' && flags.mode !== "(EXPRESSION)") {
                print_token();
                if (flags.mode === 'OBJECT' && last_text === '}') {
                    print_newline();
                } else {
                    print_single_space();
                }
            } else {
                if (flags.mode === 'OBJECT') {
                    print_newline();
                    print_token();
                    print_single_space();
                } else {
                    // EXPR or DO_BLOCK
                    print_token();
                    print_single_space();
                }
            }
            break
like image 81
Esailija Avatar answered Oct 29 '22 14:10

Esailija