r223367 - clang-format: [JS] Don't put top-level dict literals on a single line.
Daniel Jasper
djasper at google.com
Thu Dec 4 08:07:17 PST 2014
Author: djasper
Date: Thu Dec 4 10:07:17 2014
New Revision: 223367
URL: http://llvm.org/viewvc/llvm-project?rev=223367&view=rev
Log:
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.
Modified:
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTestJS.cpp
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=223367&r1=223366&r2=223367&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Thu Dec 4 10:07:17 2014
@@ -1891,6 +1891,9 @@ bool TokenAnnotator::mustBreakBefore(con
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))
Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=223367&r1=223366&r2=223367&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Thu Dec 4 10:07:17 2014
@@ -94,7 +94,10 @@ TEST_F(FormatTestJS, LiteralOperatorsCan
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"
More information about the cfe-commits
mailing list