[PATCH] clang-format: [JS] support cast syntax and type arguments.

Martin Probst martinprobst at google.com
Fri Mar 13 14:51:27 PDT 2015


Hi djasper,

Casts in TS syntax (`foo = <type>bar;`) should not be followed by whitespace.

http://reviews.llvm.org/D8333

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

Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -1757,6 +1757,14 @@
     return false;
   if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
     return false;
+  if (Left.is(TT_TemplateCloser) &&
+      Style.Language == FormatStyle::LK_JavaScript &&
+      !Right.isOneOf(tok::l_brace, tok::comma, tok::l_square,
+                     Keywords.kw_implements, Keywords.kw_extends))
+    // Type assertions ('<type>expr') are not followed by whitespace. Other
+    // locations that should have whitespace following are identified by the
+    // above set of follower tokens.
+    return false;
   return true;
 }

Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -613,5 +613,17 @@
   verifyFormat("var x = `hello` == `hello`;");
 }

+TEST_F(FormatTestJS, CastSyntax) {
+  verifyFormat("var x = <type>foo;");
+}
+
+TEST_F(FormatTestJS, TypeArguments) {
+  verifyFormat("class X<Y> {}");
+  verifyFormat("new X<Y>();");
+  verifyFormat("foo<Y>(a);");
+  verifyFormat("var x: X<Y>[];");
+  verifyFormat("class C extends D<E> implements F<G>, H<I> {}");
+}
+
 } // end namespace tooling
 } // end namespace clang

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D8333.21961.patch
Type: text/x-patch
Size: 1385 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150313/658583c2/attachment.bin>


More information about the cfe-commits mailing list