clang-format: [JS] Fix formatting of generator functions.
authorDaniel Jasper <djasper@google.com>
Tue, 1 Nov 2016 06:22:59 +0000 (06:22 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 1 Nov 2016 06:22:59 +0000 (06:22 +0000)
Before:
  var x = {
    a: function*
() {
  //
}
  }

After:
  var x = {
    a: function*() {
      //
    }
  }

llvm-svn: 285670

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

index bd55e11..5a0d592 100644 (file)
@@ -859,7 +859,8 @@ private:
     if (!CurrentToken->isOneOf(TT_LambdaLSquare, TT_ForEachMacro,
                                TT_FunctionLBrace, TT_ImplicitStringLiteral,
                                TT_InlineASMBrace, TT_JsFatArrow, TT_LambdaArrow,
-                               TT_RegexLiteral, TT_TemplateString))
+                               TT_OverloadedOperator, TT_RegexLiteral,
+                               TT_TemplateString))
       CurrentToken->Type = TT_Unknown;
     CurrentToken->Role.reset();
     CurrentToken->MatchingParen = nullptr;
index 42bee8c..f38cfa9 100644 (file)
@@ -1230,9 +1230,11 @@ void UnwrappedLineParser::tryToParseJSFunction() {
   // Consume "function".
   nextToken();
 
-  // Consume * (generator function).
-  if (FormatTok->is(tok::star))
+  // Consume * (generator function). Treat it like C++'s overloaded operators.
+  if (FormatTok->is(tok::star)) {
+    FormatTok->Type = TT_OverloadedOperator;
     nextToken();
+  }
 
   // Consume function name.
   if (FormatTok->is(tok::identifier))
index 71d09db..846abaf 100644 (file)
@@ -384,6 +384,11 @@ TEST_F(FormatTestJS, GeneratorFunctions) {
                "    yield x;\n"
                "  }\n"
                "}");
+  verifyFormat("var x = {\n"
+               "  a: function*() {\n"
+               "    //\n"
+               "  }\n"
+               "}\n");
 }
 
 TEST_F(FormatTestJS, AsyncFunctions) {