]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] recognized named functions in AnnotatingParser.
authorMartin Probst <martin@probst.io>
Thu, 9 Jun 2016 22:49:04 +0000 (22:49 +0000)
committerMartin Probst <martin@probst.io>
Thu, 9 Jun 2016 22:49:04 +0000 (22:49 +0000)
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

lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTestJS.cpp

index 2a5fe94e0094d556978b4cc58be4283d39479e4d..b469cfad2bbc6cc1cc08d5f0dcc6d40c04561e0b 100644 (file)
@@ -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 &&
index f646b82c2135b56f0a5276127b30f792903cbaf8..3d9677922cffdfc6e838147a42c09bbf7823f21e 100644 (file)
@@ -903,6 +903,8 @@ TEST_F(FormatTestJS, UnionIntersectionTypes) {
   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 {}");
+  verifyFormat("function x(path: number|string) {}");
+  verifyFormat("function x(): string|number {}");
 }
 
 TEST_F(FormatTestJS, ClassDeclarations) {