Skip to content

Commit

Permalink
Format yield* the same as function*
Browse files Browse the repository at this point in the history
Fixes #920
Closes #921
  • Loading branch information
jgeurts authored and bitwiseman committed Aug 29, 2016
1 parent 8f0793f commit 98b3c53
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 15 deletions.
9 changes: 6 additions & 3 deletions js/lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,8 +739,10 @@ 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')) {
(flags.last_text === '*' && (last_last_text === 'function' || last_last_text === 'yield'))) {
// function() vs function ()
// yield*() vs yield* ()
// function*() vs function* ()
if (opt.space_after_anon_function) {
output.space_before_token = true;
}
Expand Down Expand Up @@ -1071,7 +1073,7 @@ 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')) {
(flags.last_text === '*' && (last_last_text === 'function' || last_last_text === 'yield'))) {
prefix = 'SPACE';
} else if (last_type === 'TK_START_BLOCK') {
if (flags.inline_frame) {
Expand Down Expand Up @@ -1271,7 +1273,8 @@ 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';
var isGeneratorAsterisk = current_token.text === '*' && last_type === 'TK_RESERVED' &&
(flags.last_text === 'function' || flags.last_text === 'yield');
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
18 changes: 15 additions & 3 deletions js/test/generated/beautify-javascript-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,21 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
'}');


reset_options();
//============================================================
// Yield tests
bt('yield /foo\\//;');
bt('result = yield pgClient.query_(queryString);');
bt('yield [1, 2]');
bt('yield* bar();');

// yield should have no space between yield and star
bt('yield * bar();', 'yield* bar();');

// yield should have space between star and generator
bt('yield *bar();', 'yield* bar();');


reset_options();
//============================================================
// Async / await tests
Expand Down Expand Up @@ -2608,15 +2623,13 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
// allow unescaped / in char classes
bt('a(/[a/b]/);b()', 'a(/[a/b]/);\nb()');
bt('typeof /foo\\//;');
bt('yield /foo\\//;');
bt('throw /foo\\//;');
bt('do /foo\\//;');
bt('return /foo\\//;');
bt('switch (a) {\n case /foo\\//:\n b\n}');
bt('if (a) /foo\\//\nelse /foo\\//;');
bt('if (foo) /regex/.test();');
bt('for (index in [1, 2, 3]) /^test$/i.test(s)');
bt('result = yield pgClient.query_(queryString);');
bt('function foo() {\n return [\n "one",\n "two"\n ];\n}');
bt('a=[[1,2],[4,5],[7,8]]', 'a = [\n [1, 2],\n [4, 5],\n [7, 8]\n]');
bt('a=[[1,2],[4,5],function(){},[7,8]]', 'a = [\n [1, 2],\n [4, 5],\n function() {},\n [7, 8]\n]');
Expand All @@ -2637,7 +2650,6 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
bt('return;');
bt('return\nfunc');
bt('catch(e)', 'catch (e)');
bt('yield [1, 2]');
bt('var a=1,b={foo:2,bar:3},{baz:4,wham:5},c=4;', 'var a = 1,\n b = {\n foo: 2,\n bar: 3\n },\n {\n baz: 4,\n wham: 5\n }, c = 4;');
bt('var a=1,b={foo:2,bar:3},{baz:4,wham:5},\nc=4;', 'var a = 1,\n b = {\n foo: 2,\n bar: 3\n },\n {\n baz: 4,\n wham: 5\n },\n c = 4;');

Expand Down
8 changes: 5 additions & 3 deletions python/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +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'):
(self.flags.last_text == '*' and (self.last_last_text =='function' or self.last_last_text =='yield')):
# function() vs function (), typeof() vs typeof ()
# function*() vs function* (), yield*() vs yield* ()
if self.opts.space_after_anon_function:
self.output.space_before_token = True
elif self.last_type == 'TK_RESERVED' and (self.flags.last_text in Tokenizer.line_starters or self.flags.last_text == 'catch'):
Expand Down Expand Up @@ -1016,7 +1017,7 @@ 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'):
(self.flags.last_text == '*' and (self.last_last_text == 'function' or self.last_last_text == 'yield')):
prefix = 'SPACE'
elif self.last_type == 'TK_START_BLOCK':
if self.flags.inline_frame:
Expand Down Expand Up @@ -1194,7 +1195,8 @@ 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'
isGeneratorAsterisk = current_token.text == '*' and self.last_type == 'TK_RESERVED' and \
(self.flags.last_text == 'function' or self.flags.last_text == 'yield')
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
18 changes: 15 additions & 3 deletions python/jsbeautifier/tests/generated/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,21 @@ def unicode_char(value):
'}')


self.reset_options();
#============================================================
# Yield tests
bt('yield /foo\\//;')
bt('result = yield pgClient.query_(queryString);')
bt('yield [1, 2]')
bt('yield* bar();')

# yield should have no space between yield and star
bt('yield * bar();', 'yield* bar();')

# yield should have space between star and generator
bt('yield *bar();', 'yield* bar();')


self.reset_options();
#============================================================
# Async / await tests
Expand Down Expand Up @@ -2443,15 +2458,13 @@ def unicode_char(value):
# allow unescaped / in char classes
bt('a(/[a/b]/);b()', 'a(/[a/b]/);\nb()')
bt('typeof /foo\\//;')
bt('yield /foo\\//;')
bt('throw /foo\\//;')
bt('do /foo\\//;')
bt('return /foo\\//;')
bt('switch (a) {\n case /foo\\//:\n b\n}')
bt('if (a) /foo\\//\nelse /foo\\//;')
bt('if (foo) /regex/.test();')
bt('for (index in [1, 2, 3]) /^test$/i.test(s)')
bt('result = yield pgClient.query_(queryString);')
bt('function foo() {\n return [\n "one",\n "two"\n ];\n}')
bt('a=[[1,2],[4,5],[7,8]]', 'a = [\n [1, 2],\n [4, 5],\n [7, 8]\n]')
bt('a=[[1,2],[4,5],function(){},[7,8]]', 'a = [\n [1, 2],\n [4, 5],\n function() {},\n [7, 8]\n]')
Expand All @@ -2472,7 +2485,6 @@ def unicode_char(value):
bt('return;')
bt('return\nfunc')
bt('catch(e)', 'catch (e)')
bt('yield [1, 2]')
bt('var a=1,b={foo:2,bar:3},{baz:4,wham:5},c=4;', 'var a = 1,\n b = {\n foo: 2,\n bar: 3\n },\n {\n baz: 4,\n wham: 5\n }, c = 4;')
bt('var a=1,b={foo:2,bar:3},{baz:4,wham:5},\nc=4;', 'var a = 1,\n b = {\n foo: 2,\n bar: 3\n },\n {\n baz: 4,\n wham: 5\n },\n c = 4;')

Expand Down
22 changes: 19 additions & 3 deletions test/data/javascript/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,25 @@ exports.test_data = {
comment: 'catch-all, includes brackets and other various code',
unchanged: inputlib.operator_position.catch_all
}]
}, {
name: "Yield tests",
description: "ES6 yield tests",
tests: [
{ unchanged: 'yield /foo\\\\//;' },
{ unchanged: 'result = yield pgClient.query_(queryString);' },
{ unchanged: 'yield [1, 2]' },
{ unchanged: "yield* bar();" },
{
comment: "yield should have no space between yield and star",
input: "yield * bar();",
output: "yield* bar();"
},
{
comment: "yield should have space between star and generator",
input: "yield *bar();",
output: "yield* bar();"
}
]
}, {
name: "Async / await tests",
description: "ES7 async / await tests",
Expand Down Expand Up @@ -2421,7 +2440,6 @@ exports.test_data = {
output: "a(/[a/b]/);\nb()"
},
{ unchanged: 'typeof /foo\\\\//;' },
{ unchanged: 'yield /foo\\\\//;' },
{ unchanged: 'throw /foo\\\\//;' },
{ unchanged: 'do /foo\\\\//;' },
{ unchanged: 'return /foo\\\\//;' },
Expand All @@ -2430,7 +2448,6 @@ exports.test_data = {

{ unchanged: 'if (foo) /regex/.test();' },
{ unchanged: "for (index in [1, 2, 3]) /^test$/i.test(s)" },
{ unchanged: 'result = yield pgClient.query_(queryString);' },

{ unchanged: 'function foo() {\n return [\n "one",\n "two"\n ];\n}' },
{ input: 'a=[[1,2],[4,5],[7,8]]', output: "a = [\n [1, 2],\n [4, 5],\n [7, 8]\n]" },
Expand Down Expand Up @@ -2473,7 +2490,6 @@ exports.test_data = {
{ unchanged: 'return;' },
{ unchanged: 'return\nfunc' },
{ input: 'catch(e)', output: 'catch (e)' },
{ unchanged: 'yield [1, 2]' },

{
input: 'var a=1,b={foo:2,bar:3},{baz:4,wham:5},c=4;',
Expand Down

0 comments on commit 98b3c53

Please sign in to comment.