From 28d8a5ab43f1b0c8d34335142a8da6ead58c5ea2 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 7 Sep 2016 23:01:13 +0000 Subject: [PATCH] clang-format: [JavaScript] Change default AllowShortFunctionsOnASingleLine for Google style to "empty". llvm-svn: 280878 --- clang/lib/Format/Format.cpp | 2 +- clang/unittests/Format/FormatTestJS.cpp | 81 ++++++++++++++++++++++++--------- 2 files changed, 60 insertions(+), 23 deletions(-) diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 7fea162..7ada8a5 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -610,7 +610,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { } else if (Language == FormatStyle::LK_JavaScript) { GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; GoogleStyle.AlignOperands = false; - GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; + GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; GoogleStyle.AlwaysBreakBeforeMultilineStrings = false; GoogleStyle.BreakBeforeTernaryOperators = false; GoogleStyle.CommentPragmas = "@(export|requirecss|return|see|visibility) "; diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 22ed04f..528986a 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -161,7 +161,11 @@ TEST_F(FormatTestJS, ES6DestructuringAssignment) { } 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" " link: function() {\n" " f(); //\n" @@ -212,7 +216,11 @@ TEST_F(FormatTestJS, ContainerLiterals) { verifyFormat("x = foo && {a: 123};"); // 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};"); // Computed keys. @@ -326,11 +334,15 @@ TEST_F(FormatTestJS, FormatsNamespaces) { TEST_F(FormatTestJS, FormatsFreestandingFunctions) { 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" "}\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" "}"); verifyFormat("function f() {}"); @@ -350,7 +362,9 @@ TEST_F(FormatTestJS, GeneratorFunctions) { " yield 1;\n" "}\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" "}"); verifyFormat("class X {\n" - " async asyncMethod() { return fetch(1); }\n" + " async asyncMethod() {\n" + " return fetch(1);\n" + " }\n" "}"); verifyFormat("function initialize() {\n" " // Comment.\n" @@ -423,8 +439,10 @@ TEST_F(FormatTestJS, ColumnLayoutForArrayLiterals) { } TEST_F(FormatTestJS, FunctionLiterals) { + FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); + Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; verifyFormat("doFoo(function() {});"); - verifyFormat("doFoo(function() { return 1; });"); + verifyFormat("doFoo(function() { return 1; });", Style); verifyFormat("var func = function() {\n" " return 1;\n" "};"); @@ -438,7 +456,8 @@ TEST_F(FormatTestJS, FunctionLiterals) { " getAttribute: function(key) { return this[key]; },\n" " style: {direction: ''}\n" " }\n" - "};"); + "};", + Style); verifyFormat("abc = xyz ? function() {\n" " return 1;\n" "} : function() {\n" @@ -476,13 +495,6 @@ TEST_F(FormatTestJS, FunctionLiterals) { " // code\n" " });"); - verifyFormat("f({a: function() { return 1; }});", - getGoogleJSStyleWithColumns(33)); - verifyFormat("f({\n" - " a: function() { return 1; }\n" - "});", - getGoogleJSStyleWithColumns(32)); - verifyFormat("return {\n" " a: function SomeFunction() {\n" " // ...\n" @@ -510,6 +522,15 @@ TEST_F(FormatTestJS, FunctionLiterals) { " .doSomethingElse(\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) { @@ -570,6 +591,8 @@ TEST_F(FormatTestJS, InliningFunctionLiterals) { } TEST_F(FormatTestJS, MultipleFunctionLiterals) { + FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); + Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; verifyFormat("promise.then(\n" " function success() {\n" " doFoo();\n" @@ -606,7 +629,8 @@ TEST_F(FormatTestJS, MultipleFunctionLiterals) { " .thenCatch(function(error) {\n" " body();\n" " body();\n" - " });"); + " });", + Style); verifyFormat("getSomeLongPromise()\n" " .then(function(value) {\n" " body();\n" @@ -619,7 +643,8 @@ TEST_F(FormatTestJS, MultipleFunctionLiterals) { verifyFormat("getSomeLongPromise()\n" " .then(function(value) { body(); })\n" - " .thenCatch(function(error) { body(); });"); + " .thenCatch(function(error) { body(); });", + Style); verifyFormat("return [aaaaaaaaaaaaaaaaaaaaaa]\n" " .aaaaaaa(function() {\n" @@ -633,7 +658,9 @@ TEST_F(FormatTestJS, ArrowFunctions) { " return a;\n" "};"); verifyFormat("var x = (a) => {\n" - " function y() { return 42; }\n" + " function y() {\n" + " return 42;\n" + " }\n" " return a;\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("var x: (y: number) => string;"); verifyFormat("var x: P string>;"); - verifyFormat("var x = {y: function(): z { return 1; }};"); - verifyFormat("var x = {y: function(): {a: number} { return 1; }};"); + verifyFormat("var x = {\n" + " 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" " {longReturnValue: string[]} {}", getGoogleJSStyleWithColumns(60)); @@ -930,7 +965,7 @@ TEST_F(FormatTestJS, ClassDeclarations) { verifyFormat("class C {\n ['x' + 2]: 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 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.P implements i.I {}"); verifyFormat("class Test {\n" @@ -1093,7 +1128,9 @@ TEST_F(FormatTestJS, Modules) { verifyFormat("export default () => {};"); verifyFormat("export interface Foo { foo: number; }\n" "export class Bar {\n" - " blah(): string { return this.blah; };\n" + " blah(): string {\n" + " return this.blah;\n" + " };\n" "}"); } -- 2.7.4