]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] support `interface` as a free standing identifier.
authorMartin Probst <martin@probst.io>
Tue, 19 Apr 2016 18:18:59 +0000 (18:18 +0000)
committerMartin Probst <martin@probst.io>
Tue, 19 Apr 2016 18:18:59 +0000 (18:18 +0000)
Summary:
`interface` can be used as a fee standing identifier in JavaScript/TypeScript.
This change uses the heuristic of whether it's followed by another identifier
as an indication.

Reviewers: djasper

Subscribers: klimek, cfe-commits

Differential Revision: http://reviews.llvm.org/D19240

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

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

index 91856a74c350f280b23a211290c51b7b407b7deb..6d2d2cb44c0255f8cdb857d338b67e691bc15c4a 100644 (file)
@@ -1007,6 +1007,21 @@ void UnwrappedLineParser::parseStructuralElement() {
       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 && (Next->isNot(tok::identifier) ||
+                       Next->isOneOf(Keywords.kw_instanceof, Keywords.kw_of,
+                                     Keywords.kw_in))) {
+            nextToken();
+            break;
+          }
+        }
         parseRecord();
         addUnwrappedLine();
         return;
index aa79fa30245994417c67bfddbde039117df89679..d23a55e27084e2a195a92a85977aaa7370e9f690 100644 (file)
@@ -136,6 +136,9 @@ TEST_F(FormatTestJS, ReservedWords) {
                "};");
   verifyFormat("var struct = 2;");
   verifyFormat("var union = 2;");
+  verifyFormat("var interface = 2;");
+  verifyFormat("interface = 2;");
+  verifyFormat("x = interface instanceof y;");
 }
 
 TEST_F(FormatTestJS, CppKeywords) {