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;
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"
" 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"
"};");
}
+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"
}
TEST_F(FormatTestJS, ReturnStatements) {
- verifyFormat("function() { return [hello, world]; }");
+ verifyFormat("function() {\n"
+ " return [hello, world];\n"
+ "}");
}
TEST_F(FormatTestJS, ClosureStyleComments) {