From 5e89d2b26c5a3179ad4649d7b7a23a785a5d75fd Mon Sep 17 00:00:00 2001
From: Martin Probst <martin@probst.io>
Date: Wed, 22 Jun 2016 14:35:14 +0000
Subject: [PATCH] clang-format: [JS] Do not break before 'as'.

Summary:
'as' is a pseudo operator, so automatic semicolon insertion kicks in and the
code fails to part.

Reviewers: djasper

Subscribers: klimek

Differential Revision: http://reviews.llvm.org/D21576

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@273422 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/Format/TokenAnnotator.cpp     | 2 ++
 unittests/Format/FormatTestJS.cpp | 7 +++++--
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp
index fe7e001daaf..9c4a6268abe 100644
--- a/lib/Format/TokenAnnotator.cpp
+++ b/lib/Format/TokenAnnotator.cpp
@@ -2373,6 +2373,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
       return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
     if (Right.is(Keywords.kw_in))
       return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
+    if (Right.is(Keywords.kw_as))
+      return false; // must not break before as in 'x as type' casts
   }
 
   if (Left.is(tok::at))
diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp
index f71571c16a3..4feb55dae24 100644
--- a/unittests/Format/FormatTestJS.cpp
+++ b/unittests/Format/FormatTestJS.cpp
@@ -1051,8 +1051,8 @@ TEST_F(FormatTestJS, Modules) {
   // ... but not if from is just an identifier.
   verifyFormat("export {\n"
                "  from as from,\n"
-               "  someSurprisinglyLongVariable\n"
-               "      as from\n"
+               "  someSurprisinglyLongVariable as\n"
+               "      from\n"
                "};",
                getGoogleJSStyleWithColumns(20));
   verifyFormat("export class C {\n"
@@ -1205,6 +1205,9 @@ TEST_F(FormatTestJS, TemplateStrings) {
 TEST_F(FormatTestJS, CastSyntax) {
   verifyFormat("var x = <type>foo;");
   verifyFormat("var x = foo as type;");
+  verifyFormat("let x = (a + b) as\n"
+               "    LongTypeIsLong;",
+               getGoogleJSStyleWithColumns(20));
   verifyFormat("foo = <Bar[]>[\n"
                "  1,  //\n"
                "  2\n"
-- 
GitLab