From 71bd6fc3cc738663bfa8888cec4be678a8a75dc6 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; } }; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@210887 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/UnwrappedLineParser.cpp | 5 +++++ unittests/Format/FormatTestJS.cpp | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 6a8156f92e..71ba893cf3 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/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/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 485ccd624e..7f5507ee26 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/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.50.1