This revision was automatically updated to reflect the committed changes. Closed by commit rL273603: clang-format: [JS] recognize more type locations. (authored by mprobst).
Changed prior to commit: http://reviews.llvm.org/D21597?vs=61706&id=61711#toc Repository: rL LLVM http://reviews.llvm.org/D21597 Files: cfe/trunk/lib/Format/FormatToken.h cfe/trunk/lib/Format/TokenAnnotator.cpp cfe/trunk/unittests/Format/FormatTestJS.cpp Index: cfe/trunk/lib/Format/TokenAnnotator.cpp =================================================================== --- cfe/trunk/lib/Format/TokenAnnotator.cpp +++ cfe/trunk/lib/Format/TokenAnnotator.cpp @@ -134,6 +134,10 @@ if (Left->is(TT_OverloadedOperatorLParen)) { Contexts.back().IsExpression = false; + } else if (Style.Language == FormatStyle::LK_JavaScript && + Line.startsWith(Keywords.kw_type, tok::identifier)) { + // type X = (...); + Contexts.back().IsExpression = false; } else if (Left->Previous && (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_decltype, tok::kw_if, tok::kw_while, tok::l_paren, @@ -147,6 +151,10 @@ Keywords.kw_function)))) { // function(...) or function f(...) Contexts.back().IsExpression = false; + } else if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous && + Left->Previous->is(TT_JsTypeColon)) { + // let x: (SomeType); + Contexts.back().IsExpression = false; } else if (Left->Previous && Left->Previous->is(tok::r_square) && Left->Previous->MatchingParen && Left->Previous->MatchingParen->is(TT_LambdaLSquare)) { @@ -913,6 +921,9 @@ void modifyContext(const FormatToken &Current) { if (Current.getPrecedence() == prec::Assignment && !Line.First->isOneOf(tok::kw_template, tok::kw_using, tok::kw_return) && + // Type aliases use `type X = ...;` in TypeScript. + !(Style.Language == FormatStyle::LK_JavaScript && + Line.startsWith(Keywords.kw_type, tok::identifier)) && (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) { Contexts.back().IsExpression = true; if (!Line.startsWith(TT_UnaryOperator)) { Index: cfe/trunk/lib/Format/FormatToken.h =================================================================== --- cfe/trunk/lib/Format/FormatToken.h +++ cfe/trunk/lib/Format/FormatToken.h @@ -586,6 +586,7 @@ kw_import = &IdentTable.get("import"); kw_is = &IdentTable.get("is"); kw_let = &IdentTable.get("let"); + kw_type = &IdentTable.get("type"); kw_var = &IdentTable.get("var"); kw_yield = &IdentTable.get("yield"); @@ -637,6 +638,7 @@ IdentifierInfo *kw_import; IdentifierInfo *kw_is; IdentifierInfo *kw_let; + IdentifierInfo *kw_type; IdentifierInfo *kw_var; IdentifierInfo *kw_yield; Index: cfe/trunk/unittests/Format/FormatTestJS.cpp =================================================================== --- cfe/trunk/unittests/Format/FormatTestJS.cpp +++ cfe/trunk/unittests/Format/FormatTestJS.cpp @@ -914,6 +914,12 @@ verifyFormat("function(x: A|B = A | B): C&D {}"); verifyFormat("function x(path: number|string) {}"); verifyFormat("function x(): string|number {}"); + verifyFormat("type Foo = Bar|Baz;"); + verifyFormat("type Foo = Bar<X>|Baz;"); + verifyFormat("type Foo = (Bar<X>|Baz);"); + verifyFormat("let x: Bar|Baz;"); + verifyFormat("let x: Bar<X>|Baz;"); + verifyFormat("let x: (Foo|Bar)[];"); } TEST_F(FormatTestJS, ClassDeclarations) {
Index: cfe/trunk/lib/Format/TokenAnnotator.cpp =================================================================== --- cfe/trunk/lib/Format/TokenAnnotator.cpp +++ cfe/trunk/lib/Format/TokenAnnotator.cpp @@ -134,6 +134,10 @@ if (Left->is(TT_OverloadedOperatorLParen)) { Contexts.back().IsExpression = false; + } else if (Style.Language == FormatStyle::LK_JavaScript && + Line.startsWith(Keywords.kw_type, tok::identifier)) { + // type X = (...); + Contexts.back().IsExpression = false; } else if (Left->Previous && (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_decltype, tok::kw_if, tok::kw_while, tok::l_paren, @@ -147,6 +151,10 @@ Keywords.kw_function)))) { // function(...) or function f(...) Contexts.back().IsExpression = false; + } else if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous && + Left->Previous->is(TT_JsTypeColon)) { + // let x: (SomeType); + Contexts.back().IsExpression = false; } else if (Left->Previous && Left->Previous->is(tok::r_square) && Left->Previous->MatchingParen && Left->Previous->MatchingParen->is(TT_LambdaLSquare)) { @@ -913,6 +921,9 @@ void modifyContext(const FormatToken &Current) { if (Current.getPrecedence() == prec::Assignment && !Line.First->isOneOf(tok::kw_template, tok::kw_using, tok::kw_return) && + // Type aliases use `type X = ...;` in TypeScript. + !(Style.Language == FormatStyle::LK_JavaScript && + Line.startsWith(Keywords.kw_type, tok::identifier)) && (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) { Contexts.back().IsExpression = true; if (!Line.startsWith(TT_UnaryOperator)) { Index: cfe/trunk/lib/Format/FormatToken.h =================================================================== --- cfe/trunk/lib/Format/FormatToken.h +++ cfe/trunk/lib/Format/FormatToken.h @@ -586,6 +586,7 @@ kw_import = &IdentTable.get("import"); kw_is = &IdentTable.get("is"); kw_let = &IdentTable.get("let"); + kw_type = &IdentTable.get("type"); kw_var = &IdentTable.get("var"); kw_yield = &IdentTable.get("yield"); @@ -637,6 +638,7 @@ IdentifierInfo *kw_import; IdentifierInfo *kw_is; IdentifierInfo *kw_let; + IdentifierInfo *kw_type; IdentifierInfo *kw_var; IdentifierInfo *kw_yield; Index: cfe/trunk/unittests/Format/FormatTestJS.cpp =================================================================== --- cfe/trunk/unittests/Format/FormatTestJS.cpp +++ cfe/trunk/unittests/Format/FormatTestJS.cpp @@ -914,6 +914,12 @@ verifyFormat("function(x: A|B = A | B): C&D {}"); verifyFormat("function x(path: number|string) {}"); verifyFormat("function x(): string|number {}"); + verifyFormat("type Foo = Bar|Baz;"); + verifyFormat("type Foo = Bar<X>|Baz;"); + verifyFormat("type Foo = (Bar<X>|Baz);"); + verifyFormat("let x: Bar|Baz;"); + verifyFormat("let x: Bar<X>|Baz;"); + verifyFormat("let x: (Foo|Bar)[];"); } TEST_F(FormatTestJS, ClassDeclarations) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits