clang-format: [JS] Understand top-level function literals properly.

llvm-svn: 209205
This commit is contained in:
Daniel Jasper 2014-05-20 11:14:57 +00:00
parent e7fa2314af
commit 069e5f4858
2 changed files with 9 additions and 1 deletions

View File

@ -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)) {

View File

@ -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) {