From ebb54bf870a30aff0e47d852d1366c15a3b51ac6 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 1 Mar 2017 19:26:12 +0000 Subject: [PATCH] clang-format: [JS/TS] Properly understand cast expressions. Many things were wrong: - We didn't always allow wrapping after "as", which can be necessary. - We used to Undestand the identifier after "as" as a start of a name. - We didn't properly parse the structure of the expression with "as" having the precedence of relational operators git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@296659 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/TokenAnnotator.cpp | 7 +++++-- unittests/Format/FormatTestJS.cpp | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 907e173c9e..341421e222 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1158,7 +1158,8 @@ private: if (Tok.isNot(tok::identifier) || !Tok.Previous) return false; - if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof)) + if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof, + Keywords.kw_as)) return false; if (Style.Language == FormatStyle::LK_JavaScript && Tok.Previous->is(Keywords.kw_in)) @@ -1533,7 +1534,7 @@ private: Current->is(Keywords.kw_instanceof)) return prec::Relational; if (Style.Language == FormatStyle::LK_JavaScript && - Current->is(Keywords.kw_in)) + Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) return prec::Relational; if (Current->is(TT_BinaryOperator) || Current->is(tok::comma)) return Current->getPrecedence(); @@ -2531,6 +2532,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, 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(Keywords.kw_as)) + return true; if (Left.is(Keywords.kw_declare) && Right.isOneOf(Keywords.kw_module, tok::kw_namespace, Keywords.kw_function, tok::kw_class, tok::kw_enum, diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index d6e14edf87..88f081326d 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -1502,6 +1502,9 @@ TEST_F(FormatTestJS, CastSyntax) { verifyFormat("x = x as {a: string};"); verifyFormat("x = x as (string);"); verifyFormat("x = x! as (string);"); + verifyFormat("var x = something.someFunction() as\n" + " something;", + getGoogleJSStyleWithColumns(40)); } TEST_F(FormatTestJS, TypeArguments) { -- 2.50.1