Added support for object literal shorthand generators

Fixes #941
Closes #942
This commit is contained in:
Liam Newman 2016-08-29 18:04:19 -07:00
parent 2f5284c089
commit 1b52eb1f90
2 changed files with 20 additions and 8 deletions

View File

@ -739,7 +739,9 @@ if (!Object.values) {
} else if (!(last_type === 'TK_RESERVED' && current_token.text === '(') && last_type !== 'TK_WORD' && last_type !== 'TK_OPERATOR') { } else if (!(last_type === 'TK_RESERVED' && current_token.text === '(') && last_type !== 'TK_WORD' && last_type !== 'TK_OPERATOR') {
output.space_before_token = true; output.space_before_token = true;
} else if ((last_type === 'TK_RESERVED' && (flags.last_word === 'function' || flags.last_word === 'typeof')) || } 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 () // function() vs function ()
// yield*() vs yield* () // yield*() vs yield* ()
// function*() vs function* () // function*() vs function* ()
@ -1073,7 +1075,9 @@ if (!Object.values) {
} else if (last_type === 'TK_STRING') { } else if (last_type === 'TK_STRING') {
prefix = 'NEWLINE'; prefix = 'NEWLINE';
} else if (last_type === 'TK_RESERVED' || last_type === 'TK_WORD' || } 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'; prefix = 'SPACE';
} else if (last_type === 'TK_START_BLOCK') { } else if (last_type === 'TK_START_BLOCK') {
if (flags.inline_frame) { if (flags.inline_frame) {
@ -1273,8 +1277,9 @@ if (!Object.values) {
var space_before = true; var space_before = true;
var space_after = true; var space_after = true;
var in_ternary = false; var in_ternary = false;
var isGeneratorAsterisk = current_token.text === '*' && last_type === 'TK_RESERVED' && var isGeneratorAsterisk = current_token.text === '*' &&
(flags.last_text === 'function' || flags.last_text === 'yield'); ((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, ['-', '+']) && ( var isUnary = in_array(current_token.text, ['-', '+']) && (
in_array(last_type, ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS', 'TK_OPERATOR']) || in_array(last_type, ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS', 'TK_OPERATOR']) ||
in_array(flags.last_text, Tokenizer.line_starters) || in_array(flags.last_text, Tokenizer.line_starters) ||
@ -1393,6 +1398,7 @@ if (!Object.values) {
print_newline(); print_newline();
} }
} else if (isGeneratorAsterisk) { } else if (isGeneratorAsterisk) {
allow_wrap_or_preserved_newline();
space_before = false; space_before = false;
space_after = false; space_after = false;
} }

View File

@ -732,7 +732,9 @@ class Beautifier:
elif not (self.last_type == 'TK_RESERVED' and current_token.text == '(') and self.last_type not in ['TK_WORD', 'TK_OPERATOR']: 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 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 \ 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 (), typeof() vs typeof ()
# function*() vs function* (), yield*() vs yield* () # function*() vs function* (), yield*() vs yield* ()
if self.opts.space_after_anon_function: if self.opts.space_after_anon_function:
@ -1017,7 +1019,9 @@ class Beautifier:
elif self.last_type == 'TK_STRING': elif self.last_type == 'TK_STRING':
prefix = 'NEWLINE' prefix = 'NEWLINE'
elif self.last_type == 'TK_RESERVED' or self.last_type == 'TK_WORD' or \ 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' prefix = 'SPACE'
elif self.last_type == 'TK_START_BLOCK': elif self.last_type == 'TK_START_BLOCK':
if self.flags.inline_frame: if self.flags.inline_frame:
@ -1195,8 +1199,9 @@ class Beautifier:
space_before = True space_before = True
space_after = True space_after = True
in_ternary = False in_ternary = False
isGeneratorAsterisk = current_token.text == '*' and self.last_type == 'TK_RESERVED' and \ isGeneratorAsterisk = current_token.text == '*' and \
(self.flags.last_text == 'function' or self.flags.last_text == 'yield') ((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 ['+', '-'] \ isUnary = current_token.text in ['+', '-'] \
and (self.last_type in ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS', 'TK_OPERATOR'] \ 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 == ',') or self.flags.last_text in Tokenizer.line_starters or self.flags.last_text == ',')
@ -1300,6 +1305,7 @@ class Beautifier:
self.print_newline() self.print_newline()
elif isGeneratorAsterisk: elif isGeneratorAsterisk:
self.allow_wrap_or_preserved_newline(current_token)
space_before = False space_before = False
space_after = False space_after = False