Comma-first JS formatter

2020-06-10 10:38发布

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.

1条回答
倾城 Initia
2楼-- · 2020-06-10 11:30

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
查看更多
登录 后发表回答