From 1b52eb1f90daaefa6ff0fa7736f97fbfc58093c1 Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Mon, 29 Aug 2016 18:04:19 -0700 Subject: [PATCH] Added support for object literal shorthand generators Fixes #941 Closes #942 --- js/lib/beautify.js | 14 ++++++++++---- python/jsbeautifier/__init__.py | 14 ++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/js/lib/beautify.js b/js/lib/beautify.js index 6b511eb2..9da36cd1 100644 --- a/js/lib/beautify.js +++ b/js/lib/beautify.js @@ -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* () @@ -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) { @@ -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) || @@ -1393,6 +1398,7 @@ if (!Object.values) { print_newline(); } } else if (isGeneratorAsterisk) { + allow_wrap_or_preserved_newline(); space_before = false; space_after = false; } diff --git a/python/jsbeautifier/__init__.py b/python/jsbeautifier/__init__.py index e8b18fde..4458b86e 100644 --- a/python/jsbeautifier/__init__.py +++ b/python/jsbeautifier/__init__.py @@ -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']: 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: @@ -1017,7 +1019,9 @@ class Beautifier: 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: @@ -1195,8 +1199,9 @@ class Beautifier: 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 == ',') @@ -1300,6 +1305,7 @@ class Beautifier: self.print_newline() elif isGeneratorAsterisk: + self.allow_wrap_or_preserved_newline(current_token) space_before = False space_after = False