summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang/lib/Format/TokenAnnotator.cpp8
-rw-r--r--clang/unittests/Format/FormatTestJS.cpp13
2 files changed, 15 insertions, 6 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index caca6458df4..c6c81a48912 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1952,7 +1952,13 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
Left.Previous->is(tok::char_constant))
return true;
if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) &&
- Left.NestingLevel == 0)
+ Left.NestingLevel == 0 && Left.Previous &&
+ Left.Previous->is(tok::equal) &&
+ Line.First->isOneOf(tok::identifier, Keywords.kw_import,
+ tok::kw_export) &&
+ // kw_var is a pseudo-token that's a tok::identifier, so matches above.
+ !Line.First->is(Keywords.kw_var))
+ // Enum style object literal.
return true;
} else if (Style.Language == FormatStyle::LK_Java) {
if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 55e89da6216..045b280f187 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -94,10 +94,7 @@ TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) {
TEST_F(FormatTestJS, ES6DestructuringAssignment) {
verifyFormat("var [a, b, c] = [1, 2, 3];");
- verifyFormat("var {a, b} = {\n"
- " a: 1,\n"
- " b: 2\n"
- "};");
+ verifyFormat("var {a, b} = {a: 1, b: 2};");
}
TEST_F(FormatTestJS, ContainerLiterals) {
@@ -139,6 +136,12 @@ TEST_F(FormatTestJS, ContainerLiterals) {
" return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
" }\n"
"};");
+ // Simple object literal, as opposed to enum style below.
+ verifyFormat("var obj = {a: 123};");
+ // Enum style top level assignment.
+ verifyFormat("X = {\n a: 123\n};");
+ verifyFormat("X.Y = {\n a: 123\n};");
+ verifyFormat("x = foo && {a: 123};");
}
TEST_F(FormatTestJS, SpacesInContainerLiterals) {
@@ -545,7 +548,7 @@ TEST_F(FormatTestJS, Modules) {
getGoogleJSStyleWithColumns(20));
verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';");
verifyFormat("import * as lib from 'some/module.js';");
- verifyFormat("var x = {\n import: 1\n};\nx.import = 2;");
+ verifyFormat("var x = {import: 1};\nx.import = 2;");
verifyFormat("export function fn() {\n"
" return 'fn';\n"
OpenPOWER on IntegriCloud