]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] support AtScript style annotations for JS.
authorDaniel Jasper <djasper@google.com>
Wed, 18 Feb 2015 17:17:15 +0000 (17:17 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 18 Feb 2015 17:17:15 +0000 (17:17 +0000)
Based on Java annotation support and style.

Patch by Martin Probst.

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

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

index 8db48ff1ed579a52b76aee8ef105a366ca50b14d..4057342968591313ef98a1990c6b4c5b53d811ba 100644 (file)
@@ -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(const AnnotatedLine &Line,
     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) &&
index f062cbfaf23959c16edccbd06b75589cb719aefa..8c74b0425ad1cb10d988d3d01fee3ea5c529741a 100644 (file)
@@ -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@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