Skip to content

Commit

Permalink
Added support for object literal shorthand generators
Browse files Browse the repository at this point in the history
Fixes #941
Closes #942
  • Loading branch information
bitwiseman committed Aug 30, 2016
1 parent 2f5284c commit 1b52eb1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
14 changes: 10 additions & 4 deletions js/lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,9 @@ if (!Object.values) {
} else if (!(last_type === 'TK_RESERVED' && current_token.text === '(') && last_type !== 'TK_WORD' && last_type !== 'TK_OPERATOR') {
output.space_before_token = true;
} else if ((last_type === 'TK_RESERVED' && (flags.last_word === 'function' || flags.last_word === 'typeof')) ||
(flags.last_text === '*' && (last_last_text === 'function' || last_last_text === 'yield'))) {
(flags.last_text === '*' &&
(in_array(last_last_text, ['function', 'yield']) ||
(flags.mode === MODE.ObjectLiteral && in_array(last_last_text, ['{', ',']))))) {
// function() vs function ()
// yield*() vs yield* ()
// function*() vs function* ()
Expand Down Expand Up @@ -1073,7 +1075,9 @@ if (!Object.values) {
} else if (last_type === 'TK_STRING') {
prefix = 'NEWLINE';
} else if (last_type === 'TK_RESERVED' || last_type === 'TK_WORD' ||
(flags.last_text === '*' && (last_last_text === 'function' || last_last_text === 'yield'))) {
(flags.last_text === '*' &&
(in_array(last_last_text, ['function', 'yield']) ||
(flags.mode === MODE.ObjectLiteral && in_array(last_last_text, ['{', ',']))))) {
prefix = 'SPACE';
} else if (last_type === 'TK_START_BLOCK') {
if (flags.inline_frame) {
Expand Down Expand Up @@ -1273,8 +1277,9 @@ if (!Object.values) {
var space_before = true;
var space_after = true;
var in_ternary = false;
var isGeneratorAsterisk = current_token.text === '*' && last_type === 'TK_RESERVED' &&
(flags.last_text === 'function' || flags.last_text === 'yield');
var isGeneratorAsterisk = current_token.text === '*' &&
((last_type === 'TK_RESERVED' && in_array(flags.last_text, ['function', 'yield'])) ||
(flags.mode === MODE.ObjectLiteral && in_array(last_type, ['TK_START_BLOCK', 'TK_COMMA'])));
var isUnary = in_array(current_token.text, ['-', '+']) && (
in_array(last_type, ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS', 'TK_OPERATOR']) ||
in_array(flags.last_text, Tokenizer.line_starters) ||
Expand Down Expand Up @@ -1393,6 +1398,7 @@ if (!Object.values) {
print_newline();
}
} else if (isGeneratorAsterisk) {
allow_wrap_or_preserved_newline();
space_before = false;
space_after = false;
}
Expand Down
14 changes: 10 additions & 4 deletions python/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,9 @@ def handle_start_expr(self, current_token):
elif not (self.last_type == 'TK_RESERVED' and current_token.text == '(') and self.last_type not in ['TK_WORD', 'TK_OPERATOR']:
self.output.space_before_token = True
elif (self.last_type == 'TK_RESERVED' and (self.flags.last_word == 'function' or self.flags.last_word == 'typeof')) or \
(self.flags.last_text == '*' and (self.last_last_text =='function' or self.last_last_text =='yield')):
(self.flags.last_text == '*' and (
self.last_last_text in ['function', 'yield'] or
(self.flags.mode == MODE.ObjectLiteral and self.last_last_text in ['{', ',']))):
# function() vs function (), typeof() vs typeof ()
# function*() vs function* (), yield*() vs yield* ()
if self.opts.space_after_anon_function:
Expand Down Expand Up @@ -1017,7 +1019,9 @@ def handle_word(self, current_token):
elif self.last_type == 'TK_STRING':
prefix = 'NEWLINE'
elif self.last_type == 'TK_RESERVED' or self.last_type == 'TK_WORD' or \
(self.flags.last_text == '*' and (self.last_last_text == 'function' or self.last_last_text == 'yield')):
(self.flags.last_text == '*' and (
self.last_last_text in ['function', 'yield'] or
(self.flags.mode == MODE.ObjectLiteral and self.last_last_text in ['{', ',']))):
prefix = 'SPACE'
elif self.last_type == 'TK_START_BLOCK':
if self.flags.inline_frame:
Expand Down Expand Up @@ -1195,8 +1199,9 @@ def handle_operator(self, current_token):
space_before = True
space_after = True
in_ternary = False
isGeneratorAsterisk = current_token.text == '*' and self.last_type == 'TK_RESERVED' and \
(self.flags.last_text == 'function' or self.flags.last_text == 'yield')
isGeneratorAsterisk = current_token.text == '*' and \
((self.last_type == 'TK_RESERVED' and self.flags.last_text in ['function', 'yield']) or
(self.flags.mode == MODE.ObjectLiteral and self.last_type in ['TK_START_BLOCK', 'TK_COMMA']))
isUnary = current_token.text in ['+', '-'] \
and (self.last_type in ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS', 'TK_OPERATOR'] \
or self.flags.last_text in Tokenizer.line_starters or self.flags.last_text == ',')
Expand Down Expand Up @@ -1300,6 +1305,7 @@ def handle_operator(self, current_token):
self.print_newline()

elif isGeneratorAsterisk:
self.allow_wrap_or_preserved_newline(current_token)
space_before = False
space_after = False

Expand Down

0 comments on commit 1b52eb1

Please sign in to comment.