mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-09-29 16:57:00 +08:00
[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:
@@ -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');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user