]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] whitespace after async in arrow functions.
authorMartin Probst <martin@probst.io>
Mon, 27 Feb 2017 11:15:53 +0000 (11:15 +0000)
committerMartin Probst <martin@probst.io>
Mon, 27 Feb 2017 11:15:53 +0000 (11:15 +0000)
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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@296330 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTestJS.cpp

index fbd230c806e02c022ee36c899ed78fb13f1bbab9..b3dc314dbaef934cb7d3600a82c9d54dc52794e1 100644 (file)
@@ -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;
index fe87bf248ff17e130e02a363d689e8440c13de27..112b124398b4b266964d49c647467bb4e92ab558 100644 (file)
@@ -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"