From e712375150e3a754f5ab0f0381339a040deee57c Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Tue, 30 Sep 2014 17:57:06 +0000 Subject: [PATCH] clang-format: [JS] Support AllowShortFunctionsOnASingleLine. Specifically, this also counts for stuff like (with style "inline"): var x = function() { return 1; }; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@218689 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/Format.cpp | 1 + lib/Format/TokenAnnotator.cpp | 7 ++++ unittests/Format/FormatTestJS.cpp | 68 +++++++++++++++++++++++++++++-- 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index e70bad9f3b..d07f68d022 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -414,6 +414,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { GoogleStyle.BreakBeforeTernaryOperators = false; GoogleStyle.MaxEmptyLinesToKeep = 3; GoogleStyle.SpacesInContainerLiterals = false; + GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; } else if (Language == FormatStyle::LK_Proto) { GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; GoogleStyle.SpacesInContainerLiterals = false; diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 68e16268df..43721ef95c 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1734,6 +1734,13 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, Style.Language == FormatStyle::LK_Proto) { // Don't enums onto single lines in protocol buffers. return true; + } else if (Style.Language == FormatStyle::LK_JavaScript && + Right.is(tok::r_brace) && Left.is(tok::l_brace) && + !Left.Children.empty()) { + // Support AllowShortFunctionsOnASingleLine for JavaScript. + return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None || + (Left.NestingLevel == 0 && Line.Level == 0 && + Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Inline); } else if (isAllmanBrace(Left) || isAllmanBrace(Right)) { return Style.BreakBeforeBraces == FormatStyle::BS_Allman || Style.BreakBeforeBraces == FormatStyle::BS_GNU; diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 09d5f1feb2..bc9bbad601 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -172,7 +172,9 @@ TEST_F(FormatTestJS, FormatsFreestandingFunctions) { TEST_F(FormatTestJS, FunctionLiterals) { verifyFormat("doFoo(function() {});"); verifyFormat("doFoo(function() { return 1; });"); - verifyFormat("var func = function() { return 1; };"); + verifyFormat("var func = function() {\n" + " return 1;\n" + "};"); verifyFormat("return {\n" " body: {\n" " setAttribute: function(key, val) { this[key] = val; },\n" @@ -180,7 +182,14 @@ TEST_F(FormatTestJS, FunctionLiterals) { " style: {direction: ''}\n" " }\n" "};"); - EXPECT_EQ("abc = xyz ? function() { return 1; } : function() { return -1; };", + // FIXME: The formatting here probably isn't ideal. + EXPECT_EQ("abc = xyz ?\n" + " function() {\n" + " return 1;\n" + " } :\n" + " function() {\n" + " return -1;\n" + "};", format("abc=xyz?function(){return 1;}:function(){return -1;};")); verifyFormat("var closure = goog.bind(\n" @@ -218,6 +227,57 @@ TEST_F(FormatTestJS, FunctionLiterals) { "};"); } +TEST_F(FormatTestJS, InliningFunctionLiterals) { + FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); + Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; + verifyFormat("var func = function() {\n" + " return 1;\n" + "};", + Style); + verifyFormat("var func = doSomething(function() { return 1; });", Style); + verifyFormat("var outer = function() {\n" + " var inner = function() { return 1; }\n" + "};", + Style); + verifyFormat("function outer1(a, b) {\n" + " function inner1(a, b) { return a; }\n" + "}", + Style); + + Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; + verifyFormat("var func = function() { return 1; };", Style); + verifyFormat("var func = doSomething(function() { return 1; });", Style); + verifyFormat( + "var outer = function() { var inner = function() { return 1; } };", + Style); + verifyFormat("function outer1(a, b) {\n" + " function inner1(a, b) { return a; }\n" + "}", + Style); + + Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; + verifyFormat("var func = function() {\n" + " return 1;\n" + "};", + Style); + verifyFormat("var func = doSomething(function() {\n" + " return 1;\n" + "});", + Style); + verifyFormat("var outer = function() {\n" + " var inner = function() {\n" + " return 1;\n" + " }\n" + "};", + Style); + verifyFormat("function outer1(a, b) {\n" + " function inner1(a, b) {\n" + " return a;\n" + " }\n" + "}", + Style); +} + TEST_F(FormatTestJS, MultipleFunctionLiterals) { verifyFormat("promise.then(\n" " function success() {\n" @@ -265,7 +325,9 @@ TEST_F(FormatTestJS, MultipleFunctionLiterals) { } TEST_F(FormatTestJS, ReturnStatements) { - verifyFormat("function() { return [hello, world]; }"); + verifyFormat("function() {\n" + " return [hello, world];\n" + "}"); } TEST_F(FormatTestJS, ClosureStyleComments) { -- 2.40.0