MyDeveloperDay updated this revision to Diff 395291.
MyDeveloperDay added a comment.

Double check the interaction with the AlignConsecutiveAssignments


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115990/new/

https://reviews.llvm.org/D115990

Files:
  clang/include/clang/Format/Format.h
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTestJS.cpp

Index: clang/unittests/Format/FormatTestJS.cpp
===================================================================
--- clang/unittests/Format/FormatTestJS.cpp
+++ clang/unittests/Format/FormatTestJS.cpp
@@ -2696,5 +2696,115 @@
   verifyFormat("x = 1_000_000 + 12;", "x = 1_000_000   + 12;");
 }
 
+TEST_F(FormatTestJS, AlignConsecutiveDeclarations) {
+  FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
+  Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
+  verifyFormat("let    letVariable = 5;\n"
+               "double constVariable = 10;",
+               Style);
+
+  verifyFormat("let   letVariable = 5;\n"
+               "const constVariable = 10;",
+               Style);
+
+  verifyFormat("let          letVariable = 5;\n"
+               "static const constVariable = 10;",
+               Style);
+
+  verifyFormat("let        letVariable = 5;\n"
+               "static var constVariable = 10;",
+               Style);
+
+  verifyFormat("let letVariable = 5;\n"
+               "var constVariable = 10;",
+               Style);
+
+  verifyFormat("double letVariable = 5;\n"
+               "var    constVariable = 10;",
+               Style);
+
+  verifyFormat("const letVariable = 5;\n"
+               "var   constVariable = 10;",
+               Style);
+
+  verifyFormat("int letVariable = 5;\n"
+               "int constVariable = 10;",
+               Style);
+}
+
+TEST_F(FormatTestJS, AlignConsecutiveAssignments) {
+  FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
+
+  Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
+  verifyFormat("let letVariable      = 5;\n"
+               "double constVariable = 10;",
+               Style);
+
+  verifyFormat("let letVariable     = 5;\n"
+               "const constVariable = 10;",
+               Style);
+
+  verifyFormat("let letVariable            = 5;\n"
+               "static const constVariable = 10;",
+               Style);
+
+  verifyFormat("let letVariable          = 5;\n"
+               "static var constVariable = 10;",
+               Style);
+
+  verifyFormat("let letVariable   = 5;\n"
+               "var constVariable = 10;",
+               Style);
+
+  verifyFormat("double letVariable = 5;\n"
+               "var constVariable  = 10;",
+               Style);
+
+  verifyFormat("const letVariable = 5;\n"
+               "var constVariable = 10;",
+               Style);
+
+  verifyFormat("int letVariable   = 5;\n"
+               "int constVariable = 10;",
+               Style);
+}
+
+TEST_F(FormatTestJS, AlignConsecutiveAssignmentsAndDeclarations) {
+  FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
+  Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive;
+  Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive;
+  verifyFormat("let    letVariable   = 5;\n"
+               "double constVariable = 10;",
+               Style);
+
+  verifyFormat("let   letVariable   = 5;\n"
+               "const constVariable = 10;",
+               Style);
+
+  verifyFormat("let          letVariable   = 5;\n"
+               "static const constVariable = 10;",
+               Style);
+
+  verifyFormat("let        letVariable   = 5;\n"
+               "static var constVariable = 10;",
+               Style);
+
+  verifyFormat("let letVariable   = 5;\n"
+               "var constVariable = 10;",
+               Style);
+
+  verifyFormat("double letVariable   = 5;\n"
+               "var    constVariable = 10;",
+               Style);
+
+  verifyFormat("const letVariable   = 5;\n"
+               "var   constVariable = 10;",
+               Style);
+
+  verifyFormat("int letVariable   = 5;\n"
+               "int constVariable = 10;",
+               Style);
+}
+
 } // namespace format
 } // end namespace clang
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -1826,14 +1826,18 @@
     if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
                               Keywords.kw_as))
       return false;
-    if (Style.Language == FormatStyle::LK_JavaScript &&
+    if (Style.isJavaScript() &&
         Tok.Previous->is(Keywords.kw_in))
       return false;
 
     // Skip "const" as it does not have an influence on whether this is a name.
     FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
-    while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
-      PreviousNotConst = PreviousNotConst->getPreviousNonComment();
+
+    // For javascript const can be like "let" or "var"
+    if (!Style.isJavaScript()){
+      while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
+        PreviousNotConst = PreviousNotConst->getPreviousNonComment();
+    }
 
     if (!PreviousNotConst)
       return false;
@@ -1852,10 +1856,28 @@
         PreviousNotConst->is(TT_TypeDeclarationParen))
       return true;
 
-    return (!IsPPKeyword &&
-            PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto)) ||
-           PreviousNotConst->is(TT_PointerOrReference) ||
-           PreviousNotConst->isSimpleTypeSpecifier();
+    // If is a preprocess keyword like #define.
+    if (IsPPKeyword)
+      return false;
+
+    // int a or auto a.
+    if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto))
+      return true;
+
+    // *a or &a or &&a.
+    if (PreviousNotConst->is(TT_PointerOrReference))
+       return true;
+
+    // MyClass a;
+    if (PreviousNotConst->isSimpleTypeSpecifier())
+        return true;
+
+    // const a = in JavaScript.
+    if (Style.isJavaScript() && PreviousNotConst->is(tok::kw_const))
+      return true;
+    
+    // Not a StartOfName.
+    return false;
   }
 
   /// Determine whether ')' is ending a cast.
Index: clang/include/clang/Format/Format.h
===================================================================
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -2669,6 +2669,7 @@
   bool isCpp() const { return Language == LK_Cpp || Language == LK_ObjC; }
   bool isCSharp() const { return Language == LK_CSharp; }
   bool isJson() const { return Language == LK_Json; }
+  bool isJavaScript() const { return Language == LK_JavaScript; }
 
   /// Language, this format style is targeted at.
   /// \version 3.5
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to