clang-format: [JavaScript] Change default AllowShortFunctionsOnASingleLine

for Google style to "empty".

llvm-svn: 280878
This commit is contained in:
Daniel Jasper 2016-09-07 23:01:13 +00:00
parent d14c2c0139
commit 28d8a5ab43
2 changed files with 60 additions and 23 deletions

View File

@ -610,7 +610,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
} else if (Language == FormatStyle::LK_JavaScript) { } else if (Language == FormatStyle::LK_JavaScript) {
GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
GoogleStyle.AlignOperands = false; GoogleStyle.AlignOperands = false;
GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
GoogleStyle.AlwaysBreakBeforeMultilineStrings = false; GoogleStyle.AlwaysBreakBeforeMultilineStrings = false;
GoogleStyle.BreakBeforeTernaryOperators = false; GoogleStyle.BreakBeforeTernaryOperators = false;
GoogleStyle.CommentPragmas = "@(export|requirecss|return|see|visibility) "; GoogleStyle.CommentPragmas = "@(export|requirecss|return|see|visibility) ";

View File

@ -161,7 +161,11 @@ TEST_F(FormatTestJS, ES6DestructuringAssignment) {
} }
TEST_F(FormatTestJS, ContainerLiterals) { TEST_F(FormatTestJS, ContainerLiterals) {
verifyFormat("var x = {y: function(a) { return a; }};"); verifyFormat("var x = {\n"
" y: function(a) {\n"
" return a;\n"
" }\n"
"};");
verifyFormat("return {\n" verifyFormat("return {\n"
" link: function() {\n" " link: function() {\n"
" f(); //\n" " f(); //\n"
@ -212,7 +216,11 @@ TEST_F(FormatTestJS, ContainerLiterals) {
verifyFormat("x = foo && {a: 123};"); verifyFormat("x = foo && {a: 123};");
// Arrow functions in object literals. // Arrow functions in object literals.
verifyFormat("var x = {y: (a) => { return a; }};"); verifyFormat("var x = {\n"
" y: (a) => {\n"
" return a;\n"
" }\n"
"};");
verifyFormat("var x = {y: (a) => a};"); verifyFormat("var x = {y: (a) => a};");
// Computed keys. // Computed keys.
@ -326,11 +334,15 @@ TEST_F(FormatTestJS, FormatsNamespaces) {
TEST_F(FormatTestJS, FormatsFreestandingFunctions) { TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
verifyFormat("function outer1(a, b) {\n" verifyFormat("function outer1(a, b) {\n"
" function inner1(a, b) { return a; }\n" " function inner1(a, b) {\n"
" return a;\n"
" }\n"
" inner1(a, b);\n" " inner1(a, b);\n"
"}\n" "}\n"
"function outer2(a, b) {\n" "function outer2(a, b) {\n"
" function inner2(a, b) { return a; }\n" " function inner2(a, b) {\n"
" return a;\n"
" }\n"
" inner2(a, b);\n" " inner2(a, b);\n"
"}"); "}");
verifyFormat("function f() {}"); verifyFormat("function f() {}");
@ -350,7 +362,9 @@ TEST_F(FormatTestJS, GeneratorFunctions) {
" yield 1;\n" " yield 1;\n"
"}\n"); "}\n");
verifyFormat("class X {\n" verifyFormat("class X {\n"
" * generatorMethod() { yield x; }\n" " * generatorMethod() {\n"
" yield x;\n"
" }\n"
"}"); "}");
} }
@ -366,7 +380,9 @@ TEST_F(FormatTestJS, AsyncFunctions) {
" return fetch(x);\n" " return fetch(x);\n"
"}"); "}");
verifyFormat("class X {\n" verifyFormat("class X {\n"
" async asyncMethod() { return fetch(1); }\n" " async asyncMethod() {\n"
" return fetch(1);\n"
" }\n"
"}"); "}");
verifyFormat("function initialize() {\n" verifyFormat("function initialize() {\n"
" // Comment.\n" " // Comment.\n"
@ -423,8 +439,10 @@ TEST_F(FormatTestJS, ColumnLayoutForArrayLiterals) {
} }
TEST_F(FormatTestJS, FunctionLiterals) { TEST_F(FormatTestJS, FunctionLiterals) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
verifyFormat("doFoo(function() {});"); verifyFormat("doFoo(function() {});");
verifyFormat("doFoo(function() { return 1; });"); verifyFormat("doFoo(function() { return 1; });", Style);
verifyFormat("var func = function() {\n" verifyFormat("var func = function() {\n"
" return 1;\n" " return 1;\n"
"};"); "};");
@ -438,7 +456,8 @@ TEST_F(FormatTestJS, FunctionLiterals) {
" getAttribute: function(key) { return this[key]; },\n" " getAttribute: function(key) { return this[key]; },\n"
" style: {direction: ''}\n" " style: {direction: ''}\n"
" }\n" " }\n"
"};"); "};",
Style);
verifyFormat("abc = xyz ? function() {\n" verifyFormat("abc = xyz ? function() {\n"
" return 1;\n" " return 1;\n"
"} : function() {\n" "} : function() {\n"
@ -476,13 +495,6 @@ TEST_F(FormatTestJS, FunctionLiterals) {
" // code\n" " // code\n"
" });"); " });");
verifyFormat("f({a: function() { return 1; }});",
getGoogleJSStyleWithColumns(33));
verifyFormat("f({\n"
" a: function() { return 1; }\n"
"});",
getGoogleJSStyleWithColumns(32));
verifyFormat("return {\n" verifyFormat("return {\n"
" a: function SomeFunction() {\n" " a: function SomeFunction() {\n"
" // ...\n" " // ...\n"
@ -510,6 +522,15 @@ TEST_F(FormatTestJS, FunctionLiterals) {
" .doSomethingElse(\n" " .doSomethingElse(\n"
" // break\n" " // break\n"
" );"); " );");
Style.ColumnLimit = 33;
verifyFormat("f({a: function() { return 1; }});", Style);
Style.ColumnLimit = 32;
verifyFormat("f({\n"
" a: function() { return 1; }\n"
"});",
Style);
} }
TEST_F(FormatTestJS, InliningFunctionLiterals) { TEST_F(FormatTestJS, InliningFunctionLiterals) {
@ -570,6 +591,8 @@ TEST_F(FormatTestJS, InliningFunctionLiterals) {
} }
TEST_F(FormatTestJS, MultipleFunctionLiterals) { TEST_F(FormatTestJS, MultipleFunctionLiterals) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
verifyFormat("promise.then(\n" verifyFormat("promise.then(\n"
" function success() {\n" " function success() {\n"
" doFoo();\n" " doFoo();\n"
@ -606,7 +629,8 @@ TEST_F(FormatTestJS, MultipleFunctionLiterals) {
" .thenCatch(function(error) {\n" " .thenCatch(function(error) {\n"
" body();\n" " body();\n"
" body();\n" " body();\n"
" });"); " });",
Style);
verifyFormat("getSomeLongPromise()\n" verifyFormat("getSomeLongPromise()\n"
" .then(function(value) {\n" " .then(function(value) {\n"
" body();\n" " body();\n"
@ -619,7 +643,8 @@ TEST_F(FormatTestJS, MultipleFunctionLiterals) {
verifyFormat("getSomeLongPromise()\n" verifyFormat("getSomeLongPromise()\n"
" .then(function(value) { body(); })\n" " .then(function(value) { body(); })\n"
" .thenCatch(function(error) { body(); });"); " .thenCatch(function(error) { body(); });",
Style);
verifyFormat("return [aaaaaaaaaaaaaaaaaaaaaa]\n" verifyFormat("return [aaaaaaaaaaaaaaaaaaaaaa]\n"
" .aaaaaaa(function() {\n" " .aaaaaaa(function() {\n"
@ -633,7 +658,9 @@ TEST_F(FormatTestJS, ArrowFunctions) {
" return a;\n" " return a;\n"
"};"); "};");
verifyFormat("var x = (a) => {\n" verifyFormat("var x = (a) => {\n"
" function y() { return 42; }\n" " function y() {\n"
" return 42;\n"
" }\n"
" return a;\n" " return a;\n"
"};"); "};");
verifyFormat("var x = (a: type): {some: type} => {\n" verifyFormat("var x = (a: type): {some: type} => {\n"
@ -901,8 +928,16 @@ TEST_F(FormatTestJS, TypeAnnotations) {
verifyFormat("((a: string, b: number): string => a + b);"); verifyFormat("((a: string, b: number): string => a + b);");
verifyFormat("var x: (y: number) => string;"); verifyFormat("var x: (y: number) => string;");
verifyFormat("var x: P<string, (a: number) => string>;"); verifyFormat("var x: P<string, (a: number) => string>;");
verifyFormat("var x = {y: function(): z { return 1; }};"); verifyFormat("var x = {\n"
verifyFormat("var x = {y: function(): {a: number} { return 1; }};"); " y: function(): z {\n"
" return 1;\n"
" }\n"
"};");
verifyFormat("var x = {\n"
" y: function(): {a: number} {\n"
" return 1;\n"
" }\n"
"};");
verifyFormat("function someFunc(args: string[]):\n" verifyFormat("function someFunc(args: string[]):\n"
" {longReturnValue: string[]} {}", " {longReturnValue: string[]} {}",
getGoogleJSStyleWithColumns(60)); getGoogleJSStyleWithColumns(60));
@ -930,7 +965,7 @@ TEST_F(FormatTestJS, ClassDeclarations) {
verifyFormat("class C {\n ['x' + 2]: string = 12;\n}"); verifyFormat("class C {\n ['x' + 2]: string = 12;\n}");
verifyFormat("class C {\n private x: string = 12;\n}"); verifyFormat("class C {\n private x: string = 12;\n}");
verifyFormat("class C {\n private static x: string = 12;\n}"); verifyFormat("class C {\n private static x: string = 12;\n}");
verifyFormat("class C {\n static x(): string { return 'asd'; }\n}"); verifyFormat("class C {\n static x(): string {\n return 'asd';\n }\n}");
verifyFormat("class C extends P implements I {}"); verifyFormat("class C extends P implements I {}");
verifyFormat("class C extends p.P implements i.I {}"); verifyFormat("class C extends p.P implements i.I {}");
verifyFormat("class Test {\n" verifyFormat("class Test {\n"
@ -1093,7 +1128,9 @@ TEST_F(FormatTestJS, Modules) {
verifyFormat("export default () => {};"); verifyFormat("export default () => {};");
verifyFormat("export interface Foo { foo: number; }\n" verifyFormat("export interface Foo { foo: number; }\n"
"export class Bar {\n" "export class Bar {\n"
" blah(): string { return this.blah; };\n" " blah(): string {\n"
" return this.blah;\n"
" };\n"
"}"); "}");
} }