From: Daniel Jasper Date: Fri, 31 Oct 2014 17:50:40 +0000 (+0000) Subject: clang-format: [js] Updates to Google's JavaScript style. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=316eb7c1fb768fffc1cdcd7c8e576ff8917fc718;p=clang clang-format: [js] Updates to Google's JavaScript style. The style guide is changing.. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@220977 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 41dc09b11d..0920137870 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -664,6 +664,7 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State, if (Current.TokenText == "function") State.Stack.back().JSFunctionInlined = !Newline && Previous && Previous->Type != TT_DictLiteral && + Previous->Type != TT_ConditionalExpr && // If the unnamed function is the only parameter to another function, // we can likely inline it and come up with a good format. (Previous->isNot(tok::l_paren) || Previous->ParameterCount > 1); diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 4f3b86a4a7..300bd626ed 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -416,7 +416,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { GoogleStyle.ColumnLimit = 100; GoogleStyle.SpaceAfterCStyleCast = true; } else if (Language == FormatStyle::LK_JavaScript) { - GoogleStyle.BreakBeforeTernaryOperators = false; + GoogleStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; GoogleStyle.MaxEmptyLinesToKeep = 3; GoogleStyle.SpacesInContainerLiterals = false; GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 83b74216fb..c91d378dd4 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -8709,7 +8709,7 @@ TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { Style.Language = FormatStyle::LK_JavaScript; Style.BreakBeforeTernaryOperators = true; EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); - EXPECT_FALSE(Style.BreakBeforeTernaryOperators); + EXPECT_FALSE(Style.SpacesInContainerLiterals); Style.BreakBeforeTernaryOperators = true; EXPECT_EQ(0, parseConfiguration("---\n" @@ -8718,7 +8718,7 @@ TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { "Language: JavaScript\n" "IndentWidth: 76\n" "...\n", &Style).value()); - EXPECT_FALSE(Style.BreakBeforeTernaryOperators); + EXPECT_FALSE(Style.SpacesInContainerLiterals); EXPECT_EQ(76u, Style.IndentWidth); EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); } diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 6ee150c662..30d7c37818 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -55,29 +55,27 @@ TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) { verifyFormat("a != = b;"); verifyFormat("a === b;"); - verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10)); + verifyFormat("aaaaaaa\n === b;", getGoogleJSStyleWithColumns(10)); verifyFormat("a !== b;"); - verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10)); - verifyFormat("if (a + b + c +\n" - " d !==\n" - " e + f + g)\n" + verifyFormat("aaaaaaa\n !== b;", getGoogleJSStyleWithColumns(10)); + verifyFormat("if (a + b + c + d\n" + " !== e + f + g)\n" " q();", getGoogleJSStyleWithColumns(20)); verifyFormat("a >> >= b;"); verifyFormat("a >>> b;"); - verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10)); + verifyFormat("aaaaaaa\n >>> b;", getGoogleJSStyleWithColumns(10)); verifyFormat("a >>>= b;"); - verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10)); - verifyFormat("if (a + b + c +\n" - " d >>>\n" - " e + f + g)\n" + verifyFormat("aaaaaaa\n >>>= b;", getGoogleJSStyleWithColumns(10)); + verifyFormat("if (a + b + c + d\n" + " >>> e + f + g)\n" " q();", getGoogleJSStyleWithColumns(20)); - verifyFormat("var x = aaaaaaaaaa ?\n" - " bbbbbb :\n" - " ccc;", + verifyFormat("var x = aaaaaaaaaa\n" + " ? bbbbbb\n" + " : ccc;", getGoogleJSStyleWithColumns(20)); verifyFormat("var b = a.map((x) => x + 1);"); @@ -183,11 +181,10 @@ TEST_F(FormatTestJS, FunctionLiterals) { " }\n" "};"); // FIXME: The formatting here probably isn't ideal. - EXPECT_EQ("abc = xyz ?\n" - " function() {\n" - " return 1;\n" - " } :\n" - " function() {\n" + EXPECT_EQ("abc = xyz ? function() {\n" + " return 1;\n" + " }\n" + " : function() {\n" " return -1;\n" "};", format("abc=xyz?function(){return 1;}:function(){return -1;};"));