Skip to content

Commit

Permalink
Merge pull request #1264 from cejast/async-arrow-whitespace
Browse files Browse the repository at this point in the history
Whitespace between async and arrow function
Closes #896
  • Loading branch information
bitwiseman authored Sep 29, 2017
2 parents 5921ccc + 72f7701 commit 021e9b7
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 5 deletions.
4 changes: 2 additions & 2 deletions js/lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -878,8 +878,8 @@ function Beautifier(js_source_text, options) {
}
}

// Should be a space between await and an IIFE
if (current_token.text === '(' && last_type === 'TK_RESERVED' && flags.last_word === 'await') {
// Should be a space between await and an IIFE, or async and an arrow function
if (current_token.text === '(' && last_type === 'TK_RESERVED' && in_array(flags.last_word, ['await', 'async'])) {
output.space_before_token = true;
}

Expand Down
4 changes: 2 additions & 2 deletions js/src/javascript/beautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,8 @@ function Beautifier(js_source_text, options) {
}
}

// Should be a space between await and an IIFE
if (current_token.text === '(' && last_type === 'TK_RESERVED' && flags.last_word === 'await') {
// Should be a space between await and an IIFE, or async and an arrow function
if (current_token.text === '(' && last_type === 'TK_RESERVED' && in_array(flags.last_word, ['await', 'async'])) {
output.space_before_token = true;
}

Expand Down
38 changes: 38 additions & 0 deletions js/test/generated/beautify-javascript-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,44 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,

// ensure that this doesn't break anyone with the async library
bt('async.map(function(t) {})');

// async on arrow function. should have a space after async
bt(
'async() => {}',
// -- output --
'async () => {}');

// async on arrow function. should have a space after async
bt(
'async() => {\n' +
' return 5;\n' +
'}',
// -- output --
'async () => {\n' +
' return 5;\n' +
'}');

// async on arrow function returning expression. should have a space after async
bt(
'async() => 5;',
// -- output --
'async () => 5;');

// async on arrow function returning object literal. should have a space after async
bt(
'async(x) => ({\n' +
' foo: "5"\n' +
'})',
// -- output --
'async (x) => ({\n' +
' foo: "5"\n' +
'})');
bt(
'async (x) => {\n' +
' return x * 2;\n' +
'}');
bt('async () => 5;');
bt('async x => x * 2;');


//============================================================
Expand Down
2 changes: 1 addition & 1 deletion python/jsbeautifier/javascript/beautifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ def handle_start_expr(self, current_token):
# TODO: option space_before_conditional
self.output.space_before_token = True

elif current_token.text == '(' and self.last_type == 'TK_RESERVED' and self.flags.last_word == 'await':
elif current_token.text == '(' and self.last_type == 'TK_RESERVED' and self.flags.last_word in ['await', 'async']:
self.output.space_before_token = True


Expand Down
38 changes: 38 additions & 0 deletions python/jsbeautifier/tests/generated/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,44 @@ def unicode_char(value):

# ensure that this doesn't break anyone with the async library
bt('async.map(function(t) {})')

# async on arrow function. should have a space after async
bt(
'async() => {}',
# -- output --
'async () => {}')

# async on arrow function. should have a space after async
bt(
'async() => {\n' +
' return 5;\n' +
'}',
# -- output --
'async () => {\n' +
' return 5;\n' +
'}')

# async on arrow function returning expression. should have a space after async
bt(
'async() => 5;',
# -- output --
'async () => 5;')

# async on arrow function returning object literal. should have a space after async
bt(
'async(x) => ({\n' +
' foo: "5"\n' +
'})',
# -- output --
'async (x) => ({\n' +
' foo: "5"\n' +
'})')
bt(
'async (x) => {\n' +
' return x * 2;\n' +
'}')
bt('async () => 5;')
bt('async x => x * 2;')


#============================================================
Expand Down
29 changes: 29 additions & 0 deletions test/data/javascript/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,35 @@ exports.test_data = {
{
comment: "ensure that this doesn't break anyone with the async library",
unchanged: "async.map(function(t) {})"
},
{
comment: "async on arrow function. should have a space after async",
input_: "async() => {}",
output: "async () => {}"
},
{
comment: "async on arrow function. should have a space after async",
input_: "async() => {\n return 5;\n}",
output: "async () => {\n return 5;\n}"
},
{
comment: "async on arrow function returning expression. should have a space after async",
input_: "async() => 5;",
output: "async () => 5;"
},
{
comment: "async on arrow function returning object literal. should have a space after async",
input_: "async(x) => ({\n foo: \"5\"\n})",
output: "async (x) => ({\n foo: \"5\"\n})"
},
{
unchanged: "async (x) => {\n return x * 2;\n}"
},
{
unchanged: "async () => 5;"
},
{
unchanged: "async x => x * 2;"
}
]
}, {
Expand Down

0 comments on commit 021e9b7

Please sign in to comment.