]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] recognize exported type definitions.
authorMartin Probst <martin@probst.io>
Wed, 7 Jun 2017 12:53:22 +0000 (12:53 +0000)
committerMartin Probst <martin@probst.io>
Wed, 7 Jun 2017 12:53:22 +0000 (12:53 +0000)
Summary: Support "export type T = {...};", in addition to just "type T = {...};".

Reviewers: klimek

Differential Revision: https://reviews.llvm.org/D33980

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@304904 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 2af931cdf1ba7a419d1877a7361f1b0d3f91ba64..7ce699cf14a1523a5442bd3f1b8d1e960fbc5994 100644 (file)
@@ -135,8 +135,11 @@ private:
     if (Left->is(TT_OverloadedOperatorLParen)) {
       Contexts.back().IsExpression = false;
     } else if (Style.Language == FormatStyle::LK_JavaScript &&
-               Line.startsWith(Keywords.kw_type, tok::identifier)) {
+               (Line.startsWith(Keywords.kw_type, tok::identifier) ||
+                Line.startsWith(tok::kw_export, Keywords.kw_type,
+                                tok::identifier))) {
       // type X = (...);
+      // export type X = (...);
       Contexts.back().IsExpression = false;
     } else if (Left->Previous &&
         (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_decltype,
@@ -979,9 +982,12 @@ private:
   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.
+        // Type aliases use `type X = ...;` in TypeScript and can be exported
+        // using `export type ...`.
         !(Style.Language == FormatStyle::LK_JavaScript &&
-          Line.startsWith(Keywords.kw_type, tok::identifier)) &&
+          (Line.startsWith(Keywords.kw_type, tok::identifier) ||
+           Line.startsWith(tok::kw_export, Keywords.kw_type,
+                           tok::identifier))) &&
         (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
       Contexts.back().IsExpression = true;
       if (!Line.startsWith(TT_UnaryOperator)) {
index 14af1c92f4af0ab0710f2a6d5a00a44fe67cd9a9..e84f470687ec60a10c80b9a908d60d4784884ec6 100644 (file)
@@ -1226,6 +1226,12 @@ TEST_F(FormatTestJS, UnionIntersectionTypes) {
   verifyFormat("let x: Bar|Baz;");
   verifyFormat("let x: Bar<X>|Baz;");
   verifyFormat("let x: (Foo|Bar)[];");
+  verifyFormat("type X = {\n"
+               "  a: Foo|Bar;\n"
+               "};");
+  verifyFormat("export type X = {\n"
+               "  a: Foo|Bar;\n"
+               "};");
 }
 
 TEST_F(FormatTestJS, ClassDeclarations) {