]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] handle object types in extends positions.
authorMartin Probst <martin@probst.io>
Tue, 1 Aug 2017 15:46:10 +0000 (15:46 +0000)
committerMartin Probst <martin@probst.io>
Tue, 1 Aug 2017 15:46:10 +0000 (15:46 +0000)
Summary:
clang-format would previously drop the whitespace after `extends` in code such as:

    class Foo extends {} {}

Where the first set of curly braces is an inline object literal type.

Reviewers: djasper

Subscribers: klimek, cfe-commits

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

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

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

index b2ffbbb167828ea4a5817a14e9fe4de53922d088..318bb259509aafd3e893554ed189aebddd2128aa 100644 (file)
@@ -2341,7 +2341,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
         Left.isOneOf(Keywords.kw_function, Keywords.kw_yield))
       return false;
     if (Right.isOneOf(tok::l_brace, tok::l_square) &&
-        Left.isOneOf(Keywords.kw_function, Keywords.kw_yield))
+        Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
+                     Keywords.kw_extends, Keywords.kw_implements))
       return true;
     // JS methods can use some keywords as names (e.g. `delete()`).
     if (Right.is(tok::l_paren) && Line.MustBeDeclaration &&
index bbed83263ce0de7aea4bb1b3cc47833763320ea0..753eada8f1b09a2118a63737dddbd5aecf121604 100644 (file)
@@ -1970,6 +1970,17 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
          ((Style.Language == FormatStyle::LK_Java ||
            Style.Language == FormatStyle::LK_JavaScript) &&
           FormatTok->isOneOf(tok::period, tok::comma))) {
+    if (Style.Language == FormatStyle::LK_JavaScript &&
+        FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) {
+      // JavaScript/TypeScript supports inline object types in
+      // extends/implements positions:
+      //     class Foo implements {bar: number} { }
+      nextToken();
+      if (FormatTok->is(tok::l_brace)) {
+        tryToParseBracedList();
+        continue;
+      }
+    }
     bool IsNonMacroIdentifier =
         FormatTok->is(tok::identifier) &&
         FormatTok->TokenText != FormatTok->TokenText.upper();
index ba922b0f59fff36e999bd3d4c349789d1b39ffa1..d91df54f3da97c023fb81eef708aaf7a8e32ad8f 100644 (file)
@@ -1400,6 +1400,17 @@ TEST_F(FormatTestJS, InterfaceDeclarations) {
                "}");
 }
 
+TEST_F(FormatTestJS, ObjectTypesInExtendsImplements) {
+  verifyFormat("class C extends {} {}");
+  verifyFormat("class C implements {bar: number} {}");
+  // Somewhat odd, but probably closest to reasonable formatting?
+  verifyFormat("class C implements {\n"
+               "  bar: number,\n"
+               "  baz: string,\n"
+               "} {}");
+  verifyFormat("class C<P extends {}> {}");
+}
+
 TEST_F(FormatTestJS, EnumDeclarations) {
   verifyFormat("enum Foo {\n"
                "  A = 1,\n"