From a2ba20bb878b4d893dc8e53cf4be2778bde48ae1 Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Fri, 12 Oct 2018 18:17:25 -0700 Subject: [PATCH] Fix object literal async indenting Fixes #1573 --- js/src/javascript/beautifier.js | 2 +- js/test/generated/beautify-javascript-tests.js | 9 +++++++++ python/jsbeautifier/javascript/beautifier.py | 3 ++- python/jsbeautifier/tests/generated/tests.py | 9 +++++++++ test/data/javascript/tests.js | 12 ++++++++++++ 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/js/src/javascript/beautifier.js b/js/src/javascript/beautifier.js index d2a2111b1..94aaf83d2 100644 --- a/js/src/javascript/beautifier.js +++ b/js/src/javascript/beautifier.js @@ -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)); diff --git a/js/test/generated/beautify-javascript-tests.js b/js/test/generated/beautify-javascript-tests.js index 70e6d3968..bf50159a8 100644 --- a/js/test/generated/beautify-javascript-tests.js +++ b/js/test/generated/beautify-javascript-tests.js @@ -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' + + '}'); //============================================================ diff --git a/python/jsbeautifier/javascript/beautifier.py b/python/jsbeautifier/javascript/beautifier.py index 4813793f6..6750b737c 100644 --- a/python/jsbeautifier/javascript/beautifier.py +++ b/python/jsbeautifier/javascript/beautifier.py @@ -398,6 +398,7 @@ def start_of_statement(self, current_token): 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 @@ def handle_word(self, current_token): '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: diff --git a/python/jsbeautifier/tests/generated/tests.py b/python/jsbeautifier/tests/generated/tests.py index 840a94d34..5d7534f2c 100644 --- a/python/jsbeautifier/tests/generated/tests.py +++ b/python/jsbeautifier/tests/generated/tests.py @@ -2019,6 +2019,15 @@ def unicode_char(value): ' 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' + + '}') #============================================================ diff --git a/test/data/javascript/tests.js b/test/data/javascript/tests.js index 67fe30e84..0518f46b4 100644 --- a/test/data/javascript/tests.js +++ b/test/data/javascript/tests.js @@ -991,6 +991,18 @@ exports.test_data = { ' e = (await foo()),', ' f = 4;' ] + }, + { + unchanged: [ + 'a = {', + ' myVar: async function() {', + ' return a;', + ' },', + ' myOtherVar: async function() {', + ' yield b;', + ' }', + '}' + ] } ] }, {