[formatter] Improve formatting for code containing await expressions

This CL adds basic formatting support for 'await' expressions. While
the result is not perfect, we no longer print the argument to the
AwaitExpression on a single line.

R=yangguo@chromium.org

Bug: chromium:1062617
Change-Id: Ic0e93b3ec6daec8fde53c8395c43ebc42bb19843
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2127006
Reviewed-by: Mathias Bynens <mathias@chromium.org>
Commit-Queue: Simon Zünd <szuend@chromium.org>
This commit is contained in:
Simon Zünd
2020-03-31 07:43:14 +00:00
committed by Commit Bot
parent 17111b3e43
commit d7f02588fc
3 changed files with 39 additions and 1 deletions
+1 -1
View File
@@ -102,7 +102,7 @@ export class ESTreeWalker {
/** @enum {!Array.<string>} */
const _walkOrder = {
'AwaitExpression': ['arguments'],
'AwaitExpression': ['argument'],
'ArrayExpression': ['elements'],
'ArrayPattern': ['elements'],
'ArrowFunctionExpression': ['params', 'body'],
@@ -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';
@@ -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');
});
});