Comma-first JS formatter

◇◆丶佛笑我妖孽 提交于 2019-12-04 18:15:33

问题


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:


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


来源:https://stackoverflow.com/questions/10872184/comma-first-js-formatter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!