From 51b10ccf7ab813af35c2e3d1e3c52f3f95006fc4 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Tue, 1 Nov 2016 06:22:59 +0000 Subject: [PATCH] clang-format: [JS] Fix formatting of generator functions. Before: var x = { a: function* () { // } } After: var x = { a: function*() { // } } git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@285670 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/TokenAnnotator.cpp | 3 ++- lib/Format/UnwrappedLineParser.cpp | 6 ++++-- unittests/Format/FormatTestJS.cpp | 5 +++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index bd55e114a6..5a0d592a14 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -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; diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 42bee8c72f..f38cfa999e 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -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)) diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 71d09db7c0..846abaffeb 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -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) { -- 2.50.1