r303106 - clang-format: [JS] for async loops.

Martin Probst via cfe-commits cfe-commits at lists.llvm.org
Mon May 15 12:33:20 PDT 2017


Author: mprobst
Date: Mon May 15 14:33:20 2017
New Revision: 303106

URL: http://llvm.org/viewvc/llvm-project?rev=303106&view=rev
Log:
clang-format: [JS] for async loops.

Summary:
JavaScript supports asynchronous loop iteration in async functions:

    for async (const x of y) ...

Reviewers: djasper

Subscribers: klimek, cfe-commits

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

Modified:
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/lib/Format/UnwrappedLineParser.cpp
    cfe/trunk/unittests/Format/FormatTestJS.cpp

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=303106&r1=303105&r2=303106&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Mon May 15 14:33:20 2017
@@ -576,9 +576,12 @@ private:
       }
       break;
     case tok::kw_for:
-      if (Style.Language == FormatStyle::LK_JavaScript && Tok->Previous &&
-          Tok->Previous->is(tok::period))
-        break;
+      if (Style.Language == FormatStyle::LK_JavaScript)
+        if (Tok->Previous && Tok->Previous->is(tok::period))
+          break;
+        // JS' for async ( ...
+        if (CurrentToken->is(Keywords.kw_async))
+          next();
       Contexts.back().ColonIsForRangeExpr = true;
       next();
       if (!parseParens())
@@ -2249,6 +2252,10 @@ bool TokenAnnotator::spaceRequiredBefore
   } else if (Style.Language == FormatStyle::LK_JavaScript) {
     if (Left.is(TT_JsFatArrow))
       return true;
+    // for async ( ...
+    if (Right.is(tok::l_paren) && Left.is(Keywords.kw_async) &&
+        Left.Previous && Left.Previous->is(tok::kw_for))
+      return true;
     if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) &&
         Right.MatchingParen) {
       const FormatToken *Next = Right.MatchingParen->getNextNonComment();

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=303106&r1=303105&r2=303106&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Mon May 15 14:33:20 2017
@@ -1635,6 +1635,10 @@ void UnwrappedLineParser::parseForOrWhil
   assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
          "'for', 'while' or foreach macro expected");
   nextToken();
+  // JS' for async ( ...
+  if (Style.Language == FormatStyle::LK_JavaScript &&
+      FormatTok->is(Keywords.kw_async))
+    nextToken();
   if (FormatTok->Tok.is(tok::l_paren))
     parseParens();
   if (FormatTok->Tok.is(tok::l_brace)) {

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=303106&r1=303105&r2=303106&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Mon May 15 14:33:20 2017
@@ -548,6 +548,15 @@ TEST_F(FormatTestJS, AsyncFunctions) {
                "  // Comment.\n"
                "  return async.then();\n"
                "}\n");
+  verifyFormat("for async (const x of y) {\n"
+               "  console.log(x);\n"
+               "}\n");
+  verifyFormat("function asyncLoop() {\n"
+               "  for async (const x of y) {\n"
+               "    console.log(x);\n"
+               "  }\n"
+               "}\n");
+
 }
 
 TEST_F(FormatTestJS, FunctionParametersTrailingComma) {




More information about the cfe-commits mailing list