From ad9eb0d79decb3c875073f625a38aee2df3d94b3 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Mon, 30 Jun 2014 13:24:54 +0000 Subject: [PATCH] clang-format: [JS] support free-standing functions again. This worked initially but was broken by r210887. Before: function outer1(a, b) { function inner1(a, b) { return a; } inner1(a, b); } function outer2(a, b) { function inner2(a, b) { return a; } inner2(a, b); } After: function outer1(a, b) { function inner1(a, b) { return a; } inner1(a, b); } function outer2(a, b) { function inner2(a, b) { return a; } inner2(a, b); } Thanks to Adam Strzelecki for working on this. llvm-svn: 212038 --- clang/lib/Format/UnwrappedLineParser.cpp | 5 ++++- clang/unittests/Format/FormatTestJS.cpp | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 71ba893..20dd573 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -770,7 +770,10 @@ void UnwrappedLineParser::parseStructuralElement() { return; case tok::identifier: { StringRef Text = FormatTok->TokenText; - if (Style.Language == FormatStyle::LK_JavaScript && Text == "function") { + // Parse function literal unless 'function' is the first token in a line + // in which case this should be treated as a free-standing function. + if (Style.Language == FormatStyle::LK_JavaScript && Text == "function" && + Line->Tokens.size() > 0) { tryToParseJSFunction(); break; } diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 7f5507e..f8802b8 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -138,6 +138,17 @@ TEST_F(FormatTestJS, GoogScopes) { "}); // goog.scope"); } +TEST_F(FormatTestJS, FormatsFreestandingFunctions) { + verifyFormat("function outer1(a, b) {\n" + " function inner1(a, b) { return a; }\n" + " inner1(a, b);\n" + "}\n" + "function outer2(a, b) {\n" + " function inner2(a, b) { return a; }\n" + " inner2(a, b);\n" + "}"); +} + TEST_F(FormatTestJS, FunctionLiterals) { verifyFormat("doFoo(function() { return 1; });"); verifyFormat("var func = function() { return 1; };"); -- 2.7.4