Skip to content

Commit

Permalink
Fixed yield function spacing
Browse files Browse the repository at this point in the history
Fixes #1206
  • Loading branch information
bitwiseman committed Sep 9, 2017
1 parent f56a513 commit 453bdc9
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 24 deletions.
10 changes: 6 additions & 4 deletions js/lib/beautify.js
Expand Up @@ -310,7 +310,7 @@ function generateMapFromStrings(list) {
result[list[x].replace(/-/g, '_')] = list[x];
}
return result;
}
}

function sanitizeOperatorPosition(opPosition) {
opPosition = opPosition || OPERATOR_POSITION.before_newline;
Expand Down Expand Up @@ -595,7 +595,7 @@ function Beautifier(js_source_text, options) {
return out;
}

var newline_restricted_tokens = ['break', 'continue', 'return', 'throw'];
var newline_restricted_tokens = ['break', 'continue', 'return', 'throw', 'yield'];

function allow_wrap_or_preserved_newline(force_linewrap) {
force_linewrap = (force_linewrap === undefined) ? false : force_linewrap;
Expand Down Expand Up @@ -742,7 +742,7 @@ function Beautifier(js_source_text, options) {
if (
(last_type === 'TK_RESERVED' && in_array(flags.last_text, ['var', 'let', 'const']) && current_token.type === 'TK_WORD') ||
(last_type === 'TK_RESERVED' && flags.last_text === 'do') ||
(last_type === 'TK_RESERVED' && in_array(flags.last_text, ['return', 'throw']) && !current_token.wanted_newline) ||
(last_type === 'TK_RESERVED' && in_array(flags.last_text, newline_restricted_tokens) && !current_token.wanted_newline) ||
(last_type === 'TK_RESERVED' && flags.last_text === 'else' &&
!(current_token.type === 'TK_RESERVED' && current_token.text === 'if' && !current_token.comments_before.length)) ||
(last_type === 'TK_END_EXPR' && (previous_flags.mode === MODE.ForInitializer || previous_flags.mode === MODE.Conditional)) ||
Expand Down Expand Up @@ -1164,7 +1164,9 @@ function Beautifier(js_source_text, options) {
}
}
if (last_type === 'TK_RESERVED' || last_type === 'TK_WORD') {
if (last_type === 'TK_RESERVED' && in_array(flags.last_text, ['get', 'set', 'new', 'return', 'export', 'async'])) {
if (last_type === 'TK_RESERVED' && (
in_array(flags.last_text, ['get', 'set', 'new', 'export', 'async']) ||
in_array(flags.last_text, newline_restricted_tokens))) {
output.space_before_token = true;
} else if (last_type === 'TK_RESERVED' && flags.last_text === 'default' && last_last_text === 'export') {
output.space_before_token = true;
Expand Down
8 changes: 5 additions & 3 deletions js/src/javascript/beautifier.js
Expand Up @@ -363,7 +363,7 @@ function Beautifier(js_source_text, options) {
return out;
}

var newline_restricted_tokens = ['break', 'continue', 'return', 'throw'];
var newline_restricted_tokens = ['break', 'continue', 'return', 'throw', 'yield'];

function allow_wrap_or_preserved_newline(force_linewrap) {
force_linewrap = (force_linewrap === undefined) ? false : force_linewrap;
Expand Down Expand Up @@ -510,7 +510,7 @@ function Beautifier(js_source_text, options) {
if (
(last_type === 'TK_RESERVED' && in_array(flags.last_text, ['var', 'let', 'const']) && current_token.type === 'TK_WORD') ||
(last_type === 'TK_RESERVED' && flags.last_text === 'do') ||
(last_type === 'TK_RESERVED' && in_array(flags.last_text, ['return', 'throw']) && !current_token.wanted_newline) ||
(last_type === 'TK_RESERVED' && in_array(flags.last_text, newline_restricted_tokens) && !current_token.wanted_newline) ||
(last_type === 'TK_RESERVED' && flags.last_text === 'else' &&
!(current_token.type === 'TK_RESERVED' && current_token.text === 'if' && !current_token.comments_before.length)) ||
(last_type === 'TK_END_EXPR' && (previous_flags.mode === MODE.ForInitializer || previous_flags.mode === MODE.Conditional)) ||
Expand Down Expand Up @@ -932,7 +932,9 @@ function Beautifier(js_source_text, options) {
}
}
if (last_type === 'TK_RESERVED' || last_type === 'TK_WORD') {
if (last_type === 'TK_RESERVED' && in_array(flags.last_text, ['get', 'set', 'new', 'return', 'export', 'async'])) {
if (last_type === 'TK_RESERVED' && (
in_array(flags.last_text, ['get', 'set', 'new', 'export', 'async']) ||
in_array(flags.last_text, newline_restricted_tokens))) {
output.space_before_token = true;
} else if (last_type === 'TK_RESERVED' && flags.last_text === 'default' && last_last_text === 'export') {
output.space_before_token = true;
Expand Down
1 change: 1 addition & 0 deletions js/test/generated/beautify-javascript-tests.js
Expand Up @@ -1687,6 +1687,7 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
bt('yield /foo\\//;');
bt('result = yield pgClient.query_(queryString);');
bt('yield [1, 2]');
bt('yield function() {};');
bt('yield* bar();');

// yield should have no space between yield and star
Expand Down
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions python/jsbeautifier/javascript/beautifier.py
Expand Up @@ -268,7 +268,7 @@ def is_expression(self, mode):
return mode in [MODE.Expression, MODE.ForInitializer, MODE.Conditional]


_newline_restricted_tokens = ['break','continue','return', 'throw']
_newline_restricted_tokens = ['break','continue','return', 'throw', 'yield']
def allow_wrap_or_preserved_newline(self, current_token, force_linewrap = False):
# never wrap the first token of a line.
if self.output.just_added_newline():
Expand Down Expand Up @@ -383,7 +383,7 @@ def start_of_statement(self, current_token):
if (
(self.last_type == 'TK_RESERVED' and self.flags.last_text in ['var', 'let', 'const'] and current_token.type == 'TK_WORD') \
or (self.last_type == 'TK_RESERVED' and self.flags.last_text== 'do') \
or (self.last_type == 'TK_RESERVED' and self.flags.last_text in ['return', 'throw'] and not current_token.wanted_newline) \
or (self.last_type == 'TK_RESERVED' and self.flags.last_text in self._newline_restricted_tokens and not current_token.wanted_newline) \
or (self.last_type == 'TK_RESERVED' and self.flags.last_text == 'else' \
and not (current_token.type == 'TK_RESERVED' and current_token.text == 'if' and not len(current_token.comments_before))) \
or (self.last_type == 'TK_END_EXPR' and (self.previous_flags.mode == MODE.ForInitializer or self.previous_flags.mode == MODE.Conditional)) \
Expand Down Expand Up @@ -729,7 +729,10 @@ def handle_word(self, current_token):
self.print_newline(True)

if self.last_type == 'TK_RESERVED' or self.last_type == 'TK_WORD':
if self.last_type == 'TK_RESERVED' and self.flags.last_text in ['get', 'set', 'new', 'return', 'export', 'async']:
if self.last_type == 'TK_RESERVED' and (
self.flags.last_text in ['get', 'set', 'new', 'export', 'async'] or
self.flags.last_text in self._newline_restricted_tokens
):
self.output.space_before_token = True
elif self.last_type == 'TK_RESERVED' and self.flags.last_text == 'default' and self.last_last_text == 'export':
self.output.space_before_token = True
Expand Down
1 change: 1 addition & 0 deletions python/jsbeautifier/tests/generated/tests.py
Expand Up @@ -1515,6 +1515,7 @@ def unicode_char(value):
bt('yield /foo\\//;')
bt('result = yield pgClient.query_(queryString);')
bt('yield [1, 2]')
bt('yield function() {};')
bt('yield* bar();')

# yield should have no space between yield and star
Expand Down
1 change: 1 addition & 0 deletions test/data/javascript/tests.js
Expand Up @@ -761,6 +761,7 @@ exports.test_data = {
{ unchanged: 'yield /foo\\\\//;' },
{ unchanged: 'result = yield pgClient.query_(queryString);' },
{ unchanged: 'yield [1, 2]' },
{ unchanged: 'yield function() {};' },
{ unchanged: "yield* bar();" },
{
comment: "yield should have no space between yield and star",
Expand Down

0 comments on commit 453bdc9

Please sign in to comment.