From 5217a8b84f5b0854537c0e185196dc1de9463070 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Fri, 13 Jun 2014 07:02:04 +0000 Subject: [PATCH] clang-format: [JS] Understand named function literals. Before: return {a: function SomeFunction(){// ... return 1; } } ; After: return { a: function SomeFunction() { // ... return 1; } }; llvm-svn: 210887 --- clang/lib/Format/UnwrappedLineParser.cpp | 5 +++++ clang/unittests/Format/FormatTestJS.cpp | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 6a8156f..71ba893 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -907,6 +907,11 @@ bool UnwrappedLineParser::tryToParseLambdaIntroducer() { void UnwrappedLineParser::tryToParseJSFunction() { nextToken(); + + // Consume function name. + if (FormatTok->is(tok::identifier)) + nextToken(); + if (FormatTok->isNot(tok::l_paren)) return; nextToken(); diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 485ccd6..7f5507e 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -138,7 +138,7 @@ TEST_F(FormatTestJS, GoogScopes) { "}); // goog.scope"); } -TEST_F(FormatTestJS, Closures) { +TEST_F(FormatTestJS, FunctionLiterals) { verifyFormat("doFoo(function() { return 1; });"); verifyFormat("var func = function() { return 1; };"); verifyFormat("return {\n" @@ -177,6 +177,13 @@ TEST_F(FormatTestJS, Closures) { " a: function() { return 1; }\n" "};", getGoogleJSStyleWithColumns(37)); + + verifyFormat("return {\n" + " a: function SomeFunction() {\n" + " // ...\n" + " return 1;\n" + " }\n" + "};"); } TEST_F(FormatTestJS, MultipleFunctionLiterals) { -- 2.7.4