From a4e55f4d1ec87056703c752644e12640e97fb5a1 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Thu, 4 Dec 2014 16:07:17 +0000 Subject: [PATCH] clang-format: [JS] Don't put top-level dict literals on a single line. These are often used for enums which apparently are easier to read if formatted with one element per line. llvm-svn: 223367 --- clang/lib/Format/TokenAnnotator.cpp | 3 +++ clang/unittests/Format/FormatTestJS.cpp | 21 ++++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 9f69243..ede60ee 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1891,6 +1891,9 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous && Left.Previous->is(tok::char_constant)) return true; + if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && + Left.NestingLevel == 0) + return true; } else if (Style.Language == FormatStyle::LK_Java) { if (Left.is(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) && Line.Last->is(tok::l_brace)) diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 0d29c17..23654a88 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -94,7 +94,10 @@ TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) { TEST_F(FormatTestJS, ES6DestructuringAssignment) { verifyFormat("var [a, b, c] = [1, 2, 3];"); - verifyFormat("var {a, b} = {a: 1, b: 2};"); + verifyFormat("var {a, b} = {\n" + " a: 1,\n" + " b: 2\n" + "};"); } TEST_F(FormatTestJS, ContainerLiterals) { @@ -140,16 +143,16 @@ TEST_F(FormatTestJS, ContainerLiterals) { TEST_F(FormatTestJS, SpacesInContainerLiterals) { verifyFormat("var arr = [1, 2, 3];"); - verifyFormat("var obj = {a: 1, b: 2, c: 3};"); + verifyFormat("f({a: 1, b: 2, c: 3});"); verifyFormat("var object_literal_with_long_name = {\n" " a: 'aaaaaaaaaaaaaaaaaa',\n" " b: 'bbbbbbbbbbbbbbbbbb'\n" "};"); - verifyFormat("var obj = {a: 1, b: 2, c: 3};", + verifyFormat("f({a: 1, b: 2, c: 3});", getChromiumStyle(FormatStyle::LK_JavaScript)); - verifyFormat("someVariable = {'a': [{}]};"); + verifyFormat("f({'a': [{}]});"); } TEST_F(FormatTestJS, SingleQuoteStrings) { @@ -238,12 +241,12 @@ TEST_F(FormatTestJS, FunctionLiterals) { " };\n" "}"); - verifyFormat("var x = {a: function() { return 1; }};", - getGoogleJSStyleWithColumns(38)); - verifyFormat("var x = {\n" + verifyFormat("f({a: function() { return 1; }});", + getGoogleJSStyleWithColumns(33)); + verifyFormat("f({\n" " a: function() { return 1; }\n" - "};", - getGoogleJSStyleWithColumns(37)); + "});", + getGoogleJSStyleWithColumns(32)); verifyFormat("return {\n" " a: function SomeFunction() {\n" -- 2.7.4