r256546 - clang-format: [JS/TypeScript] Support "enum" as property name.

Daniel Jasper via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 29 00:54:24 PST 2015


Author: djasper
Date: Tue Dec 29 02:54:23 2015
New Revision: 256546

URL: http://llvm.org/viewvc/llvm-project?rev=256546&view=rev
Log:
clang-format: [JS/TypeScript] Support "enum" as property name.

Before:
  enum: string
  [];

After:
  enum: string[];

Modified:
    cfe/trunk/lib/Format/UnwrappedLineParser.cpp
    cfe/trunk/lib/Format/UnwrappedLineParser.h
    cfe/trunk/unittests/Format/FormatTestJS.cpp

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=256546&r1=256545&r2=256546&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Tue Dec 29 02:54:23 2015
@@ -818,7 +818,8 @@ void UnwrappedLineParser::parseStructura
     case tok::kw_enum:
       // parseEnum falls through and does not yet add an unwrapped line as an
       // enum definition can start a structural element.
-      parseEnum();
+      if (!parseEnum())
+        break;
       // This only applies for C++.
       if (Style.Language != FormatStyle::LK_Cpp) {
         addUnwrappedLine();
@@ -1524,11 +1525,17 @@ void UnwrappedLineParser::parseAccessSpe
   addUnwrappedLine();
 }
 
-void UnwrappedLineParser::parseEnum() {
+bool UnwrappedLineParser::parseEnum() {
   // Won't be 'enum' for NS_ENUMs.
   if (FormatTok->Tok.is(tok::kw_enum))
     nextToken();
 
+  // In TypeScript, "enum" can also be used as property name, e.g. in interface
+  // declarations. An "enum" keyword followed by a colon would be a syntax
+  // error and thus assume it is just an identifier.
+  if (Style.Language == FormatStyle::LK_JavaScript && FormatTok->is(tok::colon))
+    return false;
+
   // Eat up enum class ...
   if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
     nextToken();
@@ -1546,22 +1553,23 @@ void UnwrappedLineParser::parseEnum() {
       // return type. In Java, this can be "implements", etc.
       if (Style.Language == FormatStyle::LK_Cpp &&
           FormatTok->is(tok::identifier))
-        return;
+        return false;
     }
   }
 
   // Just a declaration or something is wrong.
   if (FormatTok->isNot(tok::l_brace))
-    return;
+    return true;
   FormatTok->BlockKind = BK_Block;
 
   if (Style.Language == FormatStyle::LK_Java) {
     // Java enums are different.
     parseJavaEnumBody();
-    return;
-  } else if (Style.Language == FormatStyle::LK_Proto) {
+    return true;
+  }
+  if (Style.Language == FormatStyle::LK_Proto) {
     parseBlock(/*MustBeDeclaration=*/true);
-    return;
+    return true;
   }
 
   // Parse enum body.
@@ -1571,6 +1579,7 @@ void UnwrappedLineParser::parseEnum() {
       nextToken();
     addUnwrappedLine();
   }
+  return true;
 
   // There is no addUnwrappedLine() here so that we fall through to parsing a
   // structural element afterwards. Thus, in "enum A {} n, m;",

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.h?rev=256546&r1=256545&r2=256546&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.h (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.h Tue Dec 29 02:54:23 2015
@@ -96,7 +96,7 @@ private:
   void parseNamespace();
   void parseNew();
   void parseAccessSpecifier();
-  void parseEnum();
+  bool parseEnum();
   void parseJavaEnumBody();
   void parseRecord();
   void parseObjCProtocolList();

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=256546&r1=256545&r2=256546&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Tue Dec 29 02:54:23 2015
@@ -762,6 +762,7 @@ TEST_F(FormatTestJS, ClassDeclarations)
 TEST_F(FormatTestJS, InterfaceDeclarations) {
   verifyFormat("interface I {\n"
                "  x: string;\n"
+               "  enum: string[];\n"
                "}\n"
                "var y;");
   // Ensure that state is reset after parsing the interface.




More information about the cfe-commits mailing list