Skip to content

Commit

Permalink
Refactor comma handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseman committed Jan 28, 2016
1 parent bc7f355 commit a8c5086
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 32 deletions.
25 changes: 7 additions & 18 deletions js/lib/beautify.js
Expand Up @@ -1113,31 +1113,23 @@
}

function handle_comma() {
print_token();
output.space_before_token = true;
if (flags.declaration_statement) {
if (is_expression(flags.parent.mode)) {
// do not break on comma, for(var a = 1, b = 2)
flags.declaration_assignment = false;
}

print_token();

if (flags.declaration_assignment) {
flags.declaration_assignment = false;
print_newline(false, true);
} else {
output.space_before_token = true;
} else if (opt.comma_first) {
// for comma-first, we want to allow a newline before the comma
// to turn into a newline after the comma, which we will fixup later
if (opt.comma_first) {
allow_wrap_or_preserved_newline();
}
allow_wrap_or_preserved_newline();
}
return;
}

print_token();
output.space_before_token = true;
if (flags.mode === MODE.ObjectLiteral ||
} else if (flags.mode === MODE.ObjectLiteral ||
(flags.mode === MODE.Statement && flags.parent.mode === MODE.ObjectLiteral)) {
if (flags.mode === MODE.Statement) {
restore_mode();
Expand All @@ -1146,15 +1138,12 @@
if (!flags.inline_frame) {
print_newline();
}
} else {
} else if (opt.comma_first) {
// EXPR or DO_BLOCK
// for comma-first, we want to allow a newline before the comma
// to turn into a newline after the comma, which we will fixup later
if (opt.comma_first) {
allow_wrap_or_preserved_newline();
}
allow_wrap_or_preserved_newline();
}

}

function handle_operator() {
Expand Down
22 changes: 8 additions & 14 deletions python/jsbeautifier/__init__.py
Expand Up @@ -1043,40 +1043,34 @@ def handle_equals(self, current_token):


def handle_comma(self, current_token):
self.print_token(current_token)
self.output.space_before_token = True

if self.flags.declaration_statement:
if self.is_expression(self.flags.parent.mode):
# do not break on comma, for ( var a = 1, b = 2
self.flags.declaration_assignment = False

self.print_token(current_token)

if self.flags.declaration_assignment:
self.flags.declaration_assignment = False
self.print_newline(preserve_statement_flags = True)
else:
self.output.space_before_token = True
elif self.opts.comma_first:
# for comma-first, we want to allow a newline before the comma
# to turn into a newline after the comma, which we will fixup later
if self.opts.comma_first:
self.allow_wrap_or_preserved_newline(current_token)
return

self.print_token(current_token)
self.output.space_before_token = True
self.allow_wrap_or_preserved_newline(current_token)

if self.flags.mode == MODE.ObjectLiteral \
elif self.flags.mode == MODE.ObjectLiteral \
or (self.flags.mode == MODE.Statement and self.flags.parent.mode == MODE.ObjectLiteral):
if self.flags.mode == MODE.Statement:
self.restore_mode()

if not self.flags.inline_frame:
self.print_newline()
else:
elif self.opts.comma_first:
# EXPR or DO_BLOCK
# for comma-first, we want to allow a newline before the comma
# to turn into a newline after the comma, which we will fixup later
if self.opts.comma_first:
self.allow_wrap_or_preserved_newline(current_token)
self.allow_wrap_or_preserved_newline(current_token)


def handle_operator(self, current_token):
Expand Down

0 comments on commit a8c5086

Please sign in to comment.