From: Daniel Jasper Date: Tue, 20 May 2014 11:14:57 +0000 (+0000) Subject: clang-format: [JS] Understand top-level function literals properly. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b46eb10258c61d1253c6bdf64836ebbb73877d19;p=clang clang-format: [JS] Understand top-level function literals properly. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@209205 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index e2613fbc17..5af743bba5 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -763,6 +763,10 @@ void UnwrappedLineParser::parseStructuralElement() { return; case tok::identifier: { StringRef Text = FormatTok->TokenText; + if (Style.Language == FormatStyle::LK_JavaScript && Text == "function") { + tryToParseJSFunction(); + break; + } nextToken(); if (Line->Tokens.size() == 1) { if (FormatTok->Tok.is(tok::colon)) { diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index f38bf895f2..657f10874e 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -31,7 +31,9 @@ protected: return Result; } - static std::string format(llvm::StringRef Code, const FormatStyle &Style) { + static std::string format( + llvm::StringRef Code, + const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) { return format(Code, 0, Code.size(), Style); } @@ -116,6 +118,8 @@ TEST_F(FormatTestJS, Closures) { " style: {direction: ''}\n" " }\n" "};"); + EXPECT_EQ("abc = xyz ? function() { return 1; } : function() { return -1; };", + format("abc=xyz?function(){return 1;}:function(){return -1;};")); } TEST_F(FormatTestJS, ReturnStatements) {