[PATCH] D32590: clang-format: [JS] parse async function declarations.

Martin Probst via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 27 06:20:22 PDT 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL301538: clang-format: [JS] parse async function declarations. (authored by mprobst).

Changed prior to commit:
  https://reviews.llvm.org/D32590?vs=96908&id=96909#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D32590

Files:
  cfe/trunk/lib/Format/UnwrappedLineParser.cpp
  cfe/trunk/unittests/Format/FormatTestJS.cpp


Index: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp
@@ -1040,13 +1040,15 @@
         return;
       }
 
-      // Parse function literal unless 'function' is the first token in a line
-      // in which case this should be treated as a free-standing function.
+      // Function declarations (as opposed to function expressions) are parsed
+      // on their own unwrapped line by continuing this loop. Function
+      // expressions (functions that are not on their own line) must not create
+      // a new unwrapped line, so they are special cased below.
+      size_t TokenCount = Line->Tokens.size();
       if (Style.Language == FormatStyle::LK_JavaScript &&
-          (FormatTok->is(Keywords.kw_function) ||
-           FormatTok->startsSequence(Keywords.kw_async,
-                                     Keywords.kw_function)) &&
-          Line->Tokens.size() > 0) {
+          FormatTok->is(Keywords.kw_function) &&
+          (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is(
+                                                     Keywords.kw_async)))) {
         tryToParseJSFunction();
         break;
       }
Index: cfe/trunk/unittests/Format/FormatTestJS.cpp
===================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp
@@ -485,13 +485,30 @@
                "  let x = 1;\n"
                "  return fetch(x);\n"
                "}");
+  verifyFormat("async function f() {\n"
+               "  return 1;\n"
+               "}\n"
+               "\n"
+               "function a() {\n"
+               "  return 1;\n"
+               "}\n",
+               "  async   function f() {\n"
+               "   return 1;\n"
+               "}\n"
+               "\n"
+               "   function a() {\n"
+               "  return   1;\n"
+               "}  \n");
   verifyFormat("async function* f() {\n"
                "  yield fetch(x);\n"
                "}");
   verifyFormat("export async function f() {\n"
                "  return fetch(x);\n"
                "}");
   verifyFormat("let x = async () => f();");
+  verifyFormat("let x = async function() {\n"
+               "  f();\n"
+               "};");
   verifyFormat("let x = async();");
   verifyFormat("class X {\n"
                "  async asyncMethod() {\n"


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32590.96909.patch
Type: text/x-patch
Size: 2528 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170427/1a3d8dd8/attachment-0001.bin>


More information about the cfe-commits mailing list