]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Support classes.
authorDaniel Jasper <djasper@google.com>
Wed, 18 Feb 2015 17:14:05 +0000 (17:14 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 18 Feb 2015 17:14:05 +0000 (17:14 +0000)
This adds support for JavaScript class definitions (again following
TypeScript & AtScript style). This only required support for
visibility modifiers in JS, everything else was already working.

Patch by Martin Probst, thank you.

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

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

index 5e0261210d4bb34b408053d8ce2a1a4e4458ced2..9b83b2733e1bd0940c255c5129b042ef895e38ab 100644 (file)
@@ -77,7 +77,8 @@ private:
   /// For example, 'public:' labels in classes are offset by 1 or 2
   /// characters to the left from their level.
   int getIndentOffset(const FormatToken &RootToken) {
-    if (Style.Language == FormatStyle::LK_Java)
+    if (Style.Language == FormatStyle::LK_Java ||
+        Style.Language == FormatStyle::LK_JavaScript)
       return 0;
     if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier())
       return Style.AccessModifierOffset;
index 19e591b7031ade16eb81f6e53cdf81e68524a6a2..903884f7c1cc16af4f36345cbd14ec0557fa2c66 100644 (file)
@@ -693,7 +693,8 @@ void UnwrappedLineParser::parseStructuralElement() {
   case tok::kw_public:
   case tok::kw_protected:
   case tok::kw_private:
-    if (Style.Language == FormatStyle::LK_Java)
+    if (Style.Language == FormatStyle::LK_Java ||
+        Style.Language == FormatStyle::LK_JavaScript)
       nextToken();
     else
       parseAccessSpecifier();
@@ -823,13 +824,17 @@ void UnwrappedLineParser::parseStructuralElement() {
         break;
       }
       nextToken();
-      if (Line->Tokens.size() == 1) {
+      if (Line->Tokens.size() == 1 &&
+          // JS doesn't have macros, and within classes colons indicate fields,
+          // not labels.
+          (Style.Language != FormatStyle::LK_JavaScript ||
+           !Line->MustBeDeclaration)) {
         if (FormatTok->Tok.is(tok::colon)) {
           parseLabel();
           return;
         }
         // Recognize function-like macro usages without trailing semicolon as
-        // well as free-standing macrose like Q_OBJECT.
+        // well as free-standing macros like Q_OBJECT.
         bool FunctionLike = FormatTok->is(tok::l_paren);
         if (FunctionLike)
           parseParens();
index d5ce580f24ff8c267741469036db2453fe86d25e..f062cbfaf23959c16edccbd06b75589cb719aefa 100644 (file)
@@ -500,5 +500,15 @@ TEST_F(FormatTestJS, TypeAnnotations) {
   verifyFormat("var x: P<string, (a: number) => string>;");
 }
 
+TEST_F(FormatTestJS, ClassDeclarations) {
+  verifyFormat("class C {\n  x: string = 12;\n}");
+  verifyFormat("class C {\n  x(): string => 12;\n}");
+  verifyFormat("class C {\n  ['x' + 2]: string = 12;\n}");
+  verifyFormat("class C {\n  private x: string = 12;\n}");
+  verifyFormat("class C {\n  private static x: string = 12;\n}");
+  verifyFormat("class C {\n  static x(): string { return 'asd'; }\n}");
+  verifyFormat("class C extends P implements I {}");
+}
+
 } // end namespace tooling
 } // end namespace clang