r229703 - clang-format: [JS] support AtScript style annotations for JS.

Daniel Jasper djasper at google.com
Wed Feb 18 09:17:15 PST 2015


Author: djasper
Date: Wed Feb 18 11:17:15 2015
New Revision: 229703

URL: http://llvm.org/viewvc/llvm-project?rev=229703&view=rev
Log:
clang-format: [JS] support AtScript style annotations for JS.

Based on Java annotation support and style.

Patch by Martin Probst.

Modified:
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTestJS.cpp

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=229703&r1=229702&r2=229703&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Wed Feb 18 11:17:15 2015
@@ -881,7 +881,9 @@ private:
       // Line.MightBeFunctionDecl can only be true after the parentheses of a
       // function declaration have been found.
       Current.Type = TT_TrailingAnnotation;
-    } else if (Style.Language == FormatStyle::LK_Java && Current.Previous) {
+    } else if ((Style.Language == FormatStyle::LK_Java ||
+                Style.Language == FormatStyle::LK_JavaScript) &&
+               Current.Previous) {
       if (Current.Previous->is(tok::at) &&
           Current.isNot(Keywords.kw_interface)) {
         const FormatToken &AtToken = *Current.Previous;
@@ -1920,6 +1922,10 @@ bool TokenAnnotator::mustBreakBefore(con
     if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) &&
         Left.NestingLevel == 0)
       return true;
+    if (Left.is(TT_LeadingJavaAnnotation) &&
+        Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
+        Line.Last->is(tok::l_brace))
+      return true;
   } else if (Style.Language == FormatStyle::LK_Java) {
     if (Left.is(TT_LeadingJavaAnnotation) &&
         Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=229703&r1=229702&r2=229703&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Wed Feb 18 11:17:15 2015
@@ -510,5 +510,18 @@ TEST_F(FormatTestJS, ClassDeclarations)
   verifyFormat("class C extends P implements I {}");
 }
 
+TEST_F(FormatTestJS, MetadataAnnotations) {
+  verifyFormat("@A\nclass C {\n}");
+  verifyFormat("@A({arg: 'value'})\nclass C {\n}");
+  verifyFormat("@A\n at B\nclass C {\n}");
+  verifyFormat("class C {\n  @A x: string;\n}");
+  verifyFormat("class C {\n"
+               "  @A\n"
+               "  private x(): string {\n"
+               "    return 'y';\n"
+               "  }\n"
+               "}");
+}
+
 } // end namespace tooling
 } // end namespace clang





More information about the cfe-commits mailing list