[PATCH] clang-format: [JS] Only special case top level object literal assignments as enums.
Martin Probst
martinprobst at google.com
Tue Jun 9 17:09:58 PDT 2015
Hi djasper,
Top level object literals are treated as enums, and their k/v pairs are put on separate lines:
X.Y = {
A: 1,
B: 2
};
However assignments within blocks should not be affected:
function x() {
y = {a:1, b:2};
}
This change fixes the second case.
http://reviews.llvm.org/D10350
Files:
lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTestJS.cpp
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -2088,13 +2088,14 @@
Left.Previous->is(tok::char_constant))
return true;
if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) &&
- Left.NestingLevel == 0 && Left.Previous &&
+ Line.Level == 0 && Left.Previous &&
Left.Previous->is(tok::equal) &&
Line.First->isOneOf(tok::identifier, Keywords.kw_import,
- tok::kw_export) &&
+ tok::kw_export, tok::kw_const) &&
// kw_var is a pseudo-token that's a tok::identifier, so matches above.
!Line.First->is(Keywords.kw_var))
- // Enum style object literal.
+ // Object literals on the top level of a file are treated as "enum-style".
+ // Each key/value pair is put on a separate line, instead of bin-packing.
return true;
} else if (Style.Language == FormatStyle::LK_Java) {
if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -146,6 +146,10 @@
// Enum style top level assignment.
verifyFormat("X = {\n a: 123\n};");
verifyFormat("X.Y = {\n a: 123\n};");
+ // But only on the top level, otherwise its a plain object literal assignment.
+ verifyFormat("function x() {\n"
+ " y = {z: 1};\n"
+ "}");
verifyFormat("x = foo && {a: 123};");
// Arrow functions in object literals.
EMAIL PREFERENCES
http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D10350.27416.patch
Type: text/x-patch
Size: 1705 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150610/8058a987/attachment.bin>
More information about the cfe-commits
mailing list