Author: djasper Date: Mon Mar 21 12:57:31 2016 New Revision: 263961 URL: http://llvm.org/viewvc/llvm-project?rev=263961&view=rev Log: 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<Bar|Baz>`). Patch by Martin Probst. Thank you. Modified: cfe/trunk/lib/Format/FormatToken.h cfe/trunk/lib/Format/TokenAnnotator.cpp cfe/trunk/unittests/Format/FormatTestJS.cpp Modified: cfe/trunk/lib/Format/FormatToken.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatToken.h?rev=263961&r1=263960&r2=263961&view=diff ============================================================================== --- cfe/trunk/lib/Format/FormatToken.h (original) +++ cfe/trunk/lib/Format/FormatToken.h Mon Mar 21 12:57:31 2016 @@ -54,6 +54,7 @@ namespace format { TYPE(JsComputedPropertyName) \ TYPE(JsFatArrow) \ TYPE(JsTypeColon) \ + TYPE(JsTypeOperator) \ TYPE(JsTypeOptionalQuestion) \ TYPE(LambdaArrow) \ TYPE(LambdaLSquare) \ Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=263961&r1=263960&r2=263961&view=diff ============================================================================== --- cfe/trunk/lib/Format/TokenAnnotator.cpp (original) +++ cfe/trunk/lib/Format/TokenAnnotator.cpp Mon Mar 21 12:57:31 2016 @@ -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 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; Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=263961&r1=263960&r2=263961&view=diff ============================================================================== --- cfe/trunk/unittests/Format/FormatTestJS.cpp (original) +++ cfe/trunk/unittests/Format/FormatTestJS.cpp Mon Mar 21 12:57:31 2016 @@ -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<A|B> = new Foo<A|B>();"); + 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}"); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits