[PATCH] D19240: clang-format: [JS] support `interface` as a free standing identifier.

Martin Probst via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 19 10:05:51 PDT 2016


mprobst updated this revision to Diff 54218.
mprobst added a comment.

- reuse mustBeJSIdent for interface detection


http://reviews.llvm.org/D19240

Files:
  lib/Format/UnwrappedLineParser.cpp
  unittests/Format/FormatTestJS.cpp

Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -136,6 +136,9 @@
                "};");
   verifyFormat("var struct = 2;");
   verifyFormat("var union = 2;");
+  verifyFormat("var interface = 2;");
+  verifyFormat("interface = 2;");
+  verifyFormat("x = interface instanceof y;");
 }
 
 TEST_F(FormatTestJS, CppKeywords) {
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -663,10 +663,8 @@
          Tok.isNot(tok::kw_noexcept);
 }
 
-static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
-                                 const FormatToken *FormatTok) {
-  if (FormatTok->Tok.isLiteral())
-    return true;
+static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
+                          const FormatToken *FormatTok) {
   // FIXME: This returns true for C/C++ keywords like 'struct'.
   return FormatTok->is(tok::identifier) &&
          (FormatTok->Tok.getIdentifierInfo() == nullptr ||
@@ -679,6 +677,11 @@
                               Keywords.kw_interface, Keywords.kw_throws));
 }
 
+static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
+                                 const FormatToken *FormatTok) {
+  return FormatTok->Tok.isLiteral() || mustBeJSIdent(Keywords, FormatTok);
+}
+
 // isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement
 // when encountered after a value (see mustBeJSIdentOrValue).
 static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
@@ -1007,6 +1010,19 @@
       if ((Style.Language == FormatStyle::LK_JavaScript ||
            Style.Language == FormatStyle::LK_Java) &&
           FormatTok->is(Keywords.kw_interface)) {
+        if (Style.Language == FormatStyle::LK_JavaScript) {
+          // In JavaScript/TypeScript, "interface" can be used as a standalone
+          // identifier, e.g. in `var interface = 1;`. If "interface" is
+          // followed by another identifier, it is very like to be an actual
+          // interface declaration.
+          unsigned StoredPosition = Tokens->getPosition();
+          FormatToken *Next = Tokens->getNextToken();
+          FormatTok = Tokens->setPosition(StoredPosition);
+          if (Next && !mustBeJSIdent(Keywords, Next)) {
+            nextToken();
+            break;
+          }
+        }
         parseRecord();
         addUnwrappedLine();
         return;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19240.54218.patch
Type: text/x-patch
Size: 2614 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160419/5b00781d/attachment-0001.bin>


More information about the cfe-commits mailing list