diff --git a/front_end/formatter_worker/ESTreeWalker.js b/front_end/formatter_worker/ESTreeWalker.js index 35e739c79d..64580096ce 100644 --- a/front_end/formatter_worker/ESTreeWalker.js +++ b/front_end/formatter_worker/ESTreeWalker.js @@ -102,7 +102,7 @@ export class ESTreeWalker { /** @enum {!Array.} */ const _walkOrder = { - 'AwaitExpression': ['arguments'], + 'AwaitExpression': ['argument'], 'ArrayExpression': ['elements'], 'ArrayPattern': ['elements'], 'ArrowFunctionExpression': ['params', 'body'], diff --git a/front_end/formatter_worker/JavaScriptFormatter.js b/front_end/formatter_worker/JavaScriptFormatter.js index 6e5ab9305c..6248fa7712 100644 --- a/front_end/formatter_worker/JavaScriptFormatter.js +++ b/front_end/formatter_worker/JavaScriptFormatter.js @@ -156,6 +156,12 @@ export class JavaScriptFormatter { } return node.argument ? 'ts' : 't'; } + if (node.type === 'AwaitExpression') { + if (AT.punctuator(token, ';')) { + return 't'; + } + return node.argument ? 'ts' : 't'; + } if (node.type === 'Property') { if (AT.punctuator(token, ':')) { return 'ts'; diff --git a/test/unittests/front_end/formatter_worker/JavaScriptFormatter_test.ts b/test/unittests/front_end/formatter_worker/JavaScriptFormatter_test.ts new file mode 100644 index 0000000000..da5306626c --- /dev/null +++ b/test/unittests/front_end/formatter_worker/JavaScriptFormatter_test.ts @@ -0,0 +1,32 @@ +// Copyright 2020 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +const {assert} = chai; + +import '../../../../front_end/formatter_worker/acorn/acorn.js'; +import '../../../../front_end/formatter_worker/acorn/acorn_loose.js'; + +import * as Platform from '../../../../front_end/platform/platform.js'; + +import {FormattedContentBuilder} from '../../../../front_end/formatter_worker/FormattedContentBuilder.js'; +import {JavaScriptFormatter} from '../../../../front_end/formatter_worker/JavaScriptFormatter.js'; + +function formatJavaScript(text: string): string { + // Indent using 2 spaces for these unit tests. + const builder = new FormattedContentBuilder(' '); + const formatter = new JavaScriptFormatter(builder); + const lineEndings = Platform.StringUtilities.findLineEndingIndexes(text); + formatter.format(text, lineEndings, 0, text.length); + + return builder.content(); +} + +describe('JavaScriptFormatter', () => { + it('formats await expressions correctly', () => { + const formattedCode = + formatJavaScript('(async () => { await someFunctionThatNeedsAwaiting(); callSomeOtherFunction(); })();'); + assert.strictEqual( + formattedCode, '(async()=>{\n await someFunctionThatNeedsAwaiting();\n callSomeOtherFunction();\n}\n)();\n'); + }); +});