Merge pull request #1574 from bitwiseman/object-async

Fix object literal async indenting
This commit is contained in:
Liam Newman 2018-10-13 14:29:39 -07:00 committed by GitHub
commit 74cb7c407c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 2 deletions

View File

@ -473,7 +473,7 @@ Beautifier.prototype.start_of_statement = function(current_token) {
var start = false;
start = start || reserved_array(this._flags.last_token, ['var', 'let', 'const']) && current_token.type === TOKEN.WORD;
start = start || reserved_word(this._flags.last_token, 'do');
start = start || (reserved_array(this._flags.last_token, newline_restricted_tokens) && !current_token.newlines);
start = start || (!(this._flags.parent.mode === MODE.ObjectLiteral && this._flags.mode === MODE.Statement)) && reserved_array(this._flags.last_token, newline_restricted_tokens) && !current_token.newlines;
start = start || reserved_word(this._flags.last_token, 'else') &&
!(reserved_word(current_token, 'if') && !current_token.comments_before);
start = start || (this._flags.last_token.type === TOKEN.END_EXPR && (this._previous_flags.mode === MODE.ForInitializer || this._previous_flags.mode === MODE.Conditional));

View File

@ -2249,6 +2249,15 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
' d = 3,\n' +
' e = (await foo()),\n' +
' f = 4;');
bt(
'a = {\n' +
' myVar: async function() {\n' +
' return a;\n' +
' },\n' +
' myOtherVar: async function() {\n' +
' yield b;\n' +
' }\n' +
'}');
//============================================================

View File

@ -398,6 +398,7 @@ class Beautifier:
current_token.type == TOKEN.WORD)
start = start or reserved_word(self._flags.last_token, 'do')
start = start or (
not (self._flags.parent.mode == MODE.ObjectLiteral and self._flags.mode == MODE.Statement) and
reserved_array(self._flags.last_token, self._newline_restricted_tokens) and
not current_token.newlines)
start = start or (
@ -711,7 +712,7 @@ class Beautifier:
'set', 'get'] and self._flags.mode != MODE.ObjectLiteral:
current_token.type = TOKEN.WORD
elif current_token.text == 'import' and self._tokens.peek().text == '(':
current_token.type = TOKEN.WORD
current_token.type = TOKEN.WORD
elif current_token.text in ['as', 'from'] and not self._flags.import_block:
current_token.type = TOKEN.WORD
elif self._flags.mode == MODE.ObjectLiteral:

View File

@ -2019,6 +2019,15 @@ class TestJSBeautifier(unittest.TestCase):
' d = 3,\n' +
' e = (await foo()),\n' +
' f = 4;')
bt(
'a = {\n' +
' myVar: async function() {\n' +
' return a;\n' +
' },\n' +
' myOtherVar: async function() {\n' +
' yield b;\n' +
' }\n' +
'}')
#============================================================

View File

@ -991,6 +991,18 @@ exports.test_data = {
' e = (await foo()),',
' f = 4;'
]
},
{
unchanged: [
'a = {',
' myVar: async function() {',
' return a;',
' },',
' myOtherVar: async function() {',
' yield b;',
' }',
'}'
]
}
]
}, {