Index: lib/Format/TokenAnnotator.cpp =================================================================== --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -372,6 +372,10 @@ ScopedContextCreator ContextCreator(*this, tok::l_square, BindingIncrease); Contexts.back().IsExpression = true; + if (Style.Language == FormatStyle::LK_JavaScript && Parent && + Parent->is(TT_JsTypeColon)) + Contexts.back().IsExpression = false; + Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr; while (CurrentToken) { @@ -439,6 +443,9 @@ Contexts.back().ColonIsDictLiteral = true; if (Left->BlockKind == BK_BracedInit) Contexts.back().IsExpression = true; + if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous && + Left->Previous->is(TT_JsTypeColon)) + Contexts.back().IsExpression = false; while (CurrentToken) { if (CurrentToken->is(tok::r_brace)) { @@ -531,6 +538,8 @@ !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) || Contexts.back().ContextKind == tok::l_paren || // function params Contexts.back().ContextKind == tok::l_square || // array type + (!Contexts.back().IsExpression && + Contexts.back().ContextKind == tok::l_brace) || // object type (Contexts.size() == 1 && Line.MustBeDeclaration)) { // method/property declaration Tok->Type = TT_JsTypeColon; Index: unittests/Format/FormatTestJS.cpp =================================================================== --- unittests/Format/FormatTestJS.cpp +++ unittests/Format/FormatTestJS.cpp @@ -1335,6 +1335,18 @@ "};"); } +TEST_F(FormatTestJS, UnionIntersectionTypesInObjectType) { + verifyFormat("let x: {x: number|null} = {x: number | null};"); + verifyFormat("let nested: {x: {y: number|null}};"); + verifyFormat("let mixed: {x: [number|null, {w: number}]};"); + verifyFormat("class X {\n" + " contructor(x: {\n" + " a: a|null,\n" + " b: b|null,\n" + " }) {}\n" + "}"); +} + TEST_F(FormatTestJS, ClassDeclarations) { verifyFormat("class C {\n x: string = 12;\n}"); verifyFormat("class C {\n x(): string => 12;\n}");