Skip to content

Commit

Permalink
clang-format: [JS] whitespace after async in arrow functions.
Browse files Browse the repository at this point in the history
Summary:
Async arrow functions should be marked with a whitespace after the async keyword, before the parameter list:
    x = async () => foo();

Before:
    x = async() => foo();

This makes it easier to tell apart an async arrow function from a call to a function called async.

Reviewers: bkramer

Subscribers: cfe-commits, klimek

Differential Revision: https://reviews.llvm.org/D30399

llvm-svn: 296330
  • Loading branch information
mprobst committed Feb 27, 2017
1 parent 88d081b commit 20371c3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
8 changes: 8 additions & 0 deletions clang/lib/Format/TokenAnnotator.cpp
Expand Up @@ -2220,6 +2220,14 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
} else if (Style.Language == FormatStyle::LK_JavaScript) {
if (Left.is(TT_JsFatArrow))
return true;
if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) &&
Right.MatchingParen) {
const FormatToken *Next = Right.MatchingParen->getNextNonComment();
// An async arrow function, for example: `x = async () => foo();`,
// as opposed to calling a function called async: `x = async();`
if (Next && Next->is(TT_JsFatArrow))
return true;
}
if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) ||
(Right.is(TT_TemplateString) && Right.TokenText.startswith("}")))
return false;
Expand Down
2 changes: 2 additions & 0 deletions clang/unittests/Format/FormatTestJS.cpp
Expand Up @@ -463,6 +463,8 @@ TEST_F(FormatTestJS, AsyncFunctions) {
verifyFormat("export async function f() {\n"
" return fetch(x);\n"
"}");
verifyFormat("let x = async () => f();");
verifyFormat("let x = async();");
verifyFormat("class X {\n"
" async asyncMethod() {\n"
" return fetch(1);\n"
Expand Down

0 comments on commit 20371c3

Please sign in to comment.