From 703ec475b843fd296ecf1287fdf9a580c64e4103 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Mon, 21 Mar 2016 17:57:31 +0000 Subject: [PATCH] clang-format: [JS] no space in union and intersection types. The operators | and & in types, as opposed to the bitwise operators, should not have whitespace around them (e.g. `Foo`). Patch by Martin Probst. Thank you. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@263961 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/FormatToken.h | 1 + lib/Format/TokenAnnotator.cpp | 13 +++++++++++++ unittests/Format/FormatTestJS.cpp | 8 ++++++++ 3 files changed, 22 insertions(+) diff --git a/lib/Format/FormatToken.h b/lib/Format/FormatToken.h index 980c7be81d..64d236bfb3 100644 --- a/lib/Format/FormatToken.h +++ b/lib/Format/FormatToken.h @@ -54,6 +54,7 @@ namespace format { TYPE(JsComputedPropertyName) \ TYPE(JsFatArrow) \ TYPE(JsTypeColon) \ + TYPE(JsTypeOperator) \ TYPE(JsTypeOptionalQuestion) \ TYPE(LambdaArrow) \ TYPE(LambdaLSquare) \ diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 96b1df82e5..69b703d03c 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -141,6 +141,9 @@ private: Left->Previous->is(TT_BinaryOperator))) { // static_assert, if and while usually contain expressions. Contexts.back().IsExpression = true; + } else if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous && + Left->Previous->is(Keywords.kw_function)) { + Contexts.back().IsExpression = false; } else if (Left->Previous && Left->Previous->is(tok::r_square) && Left->Previous->MatchingParen && Left->Previous->MatchingParen->is(TT_LambdaLSquare)) { @@ -518,6 +521,14 @@ private: Tok->Type = TT_InlineASMColon; } break; + case tok::pipe: + case tok::amp: + // | and & in declarations/type expressions represent union and + // intersection types, respectively. + if (Style.Language == FormatStyle::LK_JavaScript && + !Contexts.back().IsExpression) + Tok->Type = TT_JsTypeOperator; + break; case tok::kw_if: case tok::kw_while: if (CurrentToken && CurrentToken->is(tok::l_paren)) { @@ -2051,6 +2062,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, return true; if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion)) return false; + if (Left.is(TT_JsTypeOperator) || Right.is(TT_JsTypeOperator)) + return false; if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) && Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) return false; diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 822d572b94..c39667613c 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -847,6 +847,14 @@ TEST_F(FormatTestJS, TypeAnnotations) { getGoogleJSStyleWithColumns(60)); } +TEST_F(FormatTestJS, UnionIntersectionTypes) { + verifyFormat("let x: A|B = A | B;"); + verifyFormat("let x: A&B|C = A & B;"); + verifyFormat("let x: Foo = new Foo();"); + verifyFormat("function(x: A|B): C&D {}"); + verifyFormat("function(x: A|B = A | B): C&D {}"); +} + TEST_F(FormatTestJS, ClassDeclarations) { verifyFormat("class C {\n x: string = 12;\n}"); verifyFormat("class C {\n x(): string => 12;\n}"); -- 2.40.0