Skip to content

Commit

Permalink
async function should stay on the same line
Browse files Browse the repository at this point in the history
Fixes #630
Closes #634
  • Loading branch information
Xiot authored and bitwiseman committed Mar 8, 2015
1 parent 0fa9753 commit 46cf321
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
9 changes: 7 additions & 2 deletions js/lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,11 @@
}
}

// Should be a space between await and an IIFE
if(current_token.text === '(' && last_type === 'TK_RESERVED' && flags.last_word === 'await'){
output.space_before_token = true;
}

// Support of this kind of newline preservation.
// a = (b &&
// (c || d));
Expand Down Expand Up @@ -862,7 +867,7 @@
}
}
if (last_type === 'TK_RESERVED' || last_type === 'TK_WORD') {
if (last_type === 'TK_RESERVED' && in_array(flags.last_text, ['get', 'set', 'new', 'return', 'export'])) {
if (last_type === 'TK_RESERVED' && in_array(flags.last_text, ['get', 'set', 'new', 'return', 'export', 'async'])) {
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 Expand Up @@ -1471,7 +1476,7 @@

// words which should always start on new line.
this.line_starters = 'continue,try,throw,return,var,let,const,if,switch,case,default,for,while,break,function,import,export'.split(',');
var reserved_words = this.line_starters.concat(['do', 'in', 'else', 'get', 'set', 'new', 'catch', 'finally', 'typeof', 'yield']);
var reserved_words = this.line_starters.concat(['do', 'in', 'else', 'get', 'set', 'new', 'catch', 'finally', 'typeof', 'yield', 'async', 'await']);

var n_newlines, whitespace_before_token, in_html_comment, tokens, parser_pos;
var input_length;
Expand Down
14 changes: 14 additions & 0 deletions js/test/beautify-javascript-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,20 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,

// New Test Suite

// Async / await tests
bt('async function foo() {}');
bt('let w = async function foo() {}');
bt('async function foo() {}\nvar x = await foo();');

// async function as an input to another function
bt('wrapper(async function foo() {})');

// await on inline anonymous function. should have a space after await
bt('async function() {\n var w = await(async function() {\n return await foo();\n })();\n}', 'async function() {\n var w = await (async function() {\n return await foo();\n })();\n}');

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

// Old tests
bt('');
test_fragment(' return .5');
Expand Down
8 changes: 6 additions & 2 deletions python/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,10 @@ 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':
self.output.space_before_token = True


# Support of this kind of newline preservation:
# a = (b &&
# (c || d));
Expand Down Expand Up @@ -801,7 +805,7 @@ 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']:
if self.last_type == 'TK_RESERVED' and self.flags.last_text in ['get', 'set', 'new', 'return', 'export', 'async']:
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 Expand Up @@ -1326,7 +1330,7 @@ class Tokenizer:

# Words which always should start on a new line
line_starters = 'continue,try,throw,return,var,let,const,if,switch,case,default,for,while,break,function,import,export'.split(',')
reserved_words = line_starters + ['do', 'in', 'else', 'get', 'set', 'new', 'catch', 'finally', 'typeof', 'yield']
reserved_words = line_starters + ['do', 'in', 'else', 'get', 'set', 'new', 'catch', 'finally', 'typeof', 'yield', 'async', 'await']

def __init__ (self, input, opts, indent_string):
self.input = input
Expand Down
14 changes: 14 additions & 0 deletions python/jsbeautifier/tests/testjsbeautifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ def unicode_char(value):

# New Test Suite

# Async / await tests
bt('async function foo() {}')
bt('let w = async function foo() {}')
bt('async function foo() {}\nvar x = await foo();')

# async function as an input to another function
bt('wrapper(async function foo() {})')

# await on inline anonymous function. should have a space after await
bt('async function() {\n var w = await(async function() {\n return await foo();\n })();\n}', 'async function() {\n var w = await (async function() {\n return await foo();\n })();\n}')

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

# Old tests
bt('')
test_fragment(' return .5')
Expand Down
21 changes: 21 additions & 0 deletions test/data/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ exports.test_data = {
],
}, {
name: "New Test Suite"
},
{
name: "Async / await tests",
description: "ES7 async / await tests",
tests: [
{ input: "async function foo() {}" },
{ input: "let w = async function foo() {}" },
{ input: "async function foo() {}\nvar x = await foo();"},
{
comment: "async function as an input to another function",
input: "wrapper(async function foo() {})"},
{
comment: "await on inline anonymous function. should have a space after await",
input: "async function() {\n var w = await(async function() {\n return await foo();\n })();\n}",
output: "async function() {\n var w = await (async function() {\n return await foo();\n })();\n}"
},
{
comment: "ensure that this doesn't break anyone with the async library",
input: "async.map(function(t) {})"
}
]
},
// =======================================================
// New tests groups should be added above this line.
Expand Down

0 comments on commit 46cf321

Please sign in to comment.