From: Martin Probst Date: Thu, 9 Jun 2016 22:49:04 +0000 (+0000) Subject: clang-format: [JS] recognized named functions in AnnotatingParser. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a6d5f7999c292d3bd922f11934d337b78ba4b194;p=clang clang-format: [JS] recognized named functions in AnnotatingParser. Summary: This also fixes union type formatting in function parameter types. Before: function x(path: number| string) {} After: function x(path: number|string) {} Reviewers: djasper Subscribers: klimek, cfe-commits Differential Revision: http://reviews.llvm.org/D21206 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@272330 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 2a5fe94e00..b469cfad2b 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -142,7 +142,10 @@ private: // 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)) { + (Left->Previous->is(Keywords.kw_function) || + (Left->Previous->endsSequence(tok::identifier, + Keywords.kw_function)))) { + // function(...) or function f(...) Contexts.back().IsExpression = false; } else if (Left->Previous && Left->Previous->is(tok::r_square) && Left->Previous->MatchingParen && diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index f646b82c21..3d9677922c 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -903,6 +903,8 @@ TEST_F(FormatTestJS, UnionIntersectionTypes) { verifyFormat("let x: Foo = new Foo();"); verifyFormat("function(x: A|B): C&D {}"); verifyFormat("function(x: A|B = A | B): C&D {}"); + verifyFormat("function x(path: number|string) {}"); + verifyFormat("function x(): string|number {}"); } TEST_F(FormatTestJS, ClassDeclarations) {