clang-format: [JS] Support AllowShortFunctionsOnASingleLine.
Specifically, this also counts for stuff like (with style "inline"):
var x = function() {
return 1;
};
llvm-svn: 218689
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 09d5f1f..bc9bbad 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -172,7 +172,9 @@
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 @@
" 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, 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, ReturnStatements) {
- verifyFormat("function() { return [hello, world]; }");
+ verifyFormat("function() {\n"
+ " return [hello, world];\n"
+ "}");
}
TEST_F(FormatTestJS, ClosureStyleComments) {