[clang] [clang-format] js handle anonymous classes (PR #106242)

Krasimir Georgiev via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 28 02:00:44 PDT 2024


https://github.com/krasimirgg updated https://github.com/llvm/llvm-project/pull/106242

>From 32e94bde5a8ed401a9fb1255d8394c552da82dd7 Mon Sep 17 00:00:00 2001
From: Krasimir Georgiev <krasimir at google.com>
Date: Tue, 27 Aug 2024 16:08:07 +0000
Subject: [PATCH 1/4] [clang-format] js handle anonymous classes

Addresses a regression in JavaScript when formatting anonymous classes.
---
 clang/lib/Format/UnwrappedLineParser.cpp | 6 +++++-
 clang/unittests/Format/FormatTestJS.cpp  | 9 +++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 5f1a88d4bcd729..7591eaeae461a7 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3992,6 +3992,9 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
   auto IsNonMacroIdentifier = [](const FormatToken *Tok) {
     return Tok->is(tok::identifier) && Tok->TokenText != Tok->TokenText.upper();
   };
+  // JavaScript/TypeScript supports anonymous classes like:
+  // a = class extends foo { }
+  bool JSPastExtendsOrImplements = false;
   // The actual identifier can be a nested name specifier, and in macros
   // it is often token-pasted.
   // An [[attribute]] can be before the identifier.
@@ -4002,6 +4005,7 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
           FormatTok->isOneOf(tok::period, tok::comma))) {
     if (Style.isJavaScript() &&
         FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) {
+      JSPastExtendsOrImplements = true;
       // JavaScript/TypeScript supports inline object types in
       // extends/implements positions:
       //     class Foo implements {bar: number} { }
@@ -4027,7 +4031,7 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
     case tok::coloncolon:
       break;
     default:
-      if (!ClassName && Previous->is(tok::identifier) &&
+      if (!JSPastExtendsOrImplements && !ClassName && Previous->is(tok::identifier) &&
           Previous->isNot(TT_AttributeMacro)) {
         ClassName = Previous;
       }
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index b910ce620de7a9..734c1590c41ddb 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -579,6 +579,15 @@ TEST_F(FormatTestJS, GoogScopes) {
                "});");
 }
 
+TEST_F(FormatTestJS, GoogAnonymousClass) {
+  verifyFormat("a = class extends goog.structs.a {\n"
+               "  a() {\n"
+               "    return 0;\n"
+               "  }\n"
+               "};");
+}
+
+
 TEST_F(FormatTestJS, IIFEs) {
   // Internal calling parens; no semi.
   verifyFormat("(function() {\n"

>From f42f5d46ba41dc679e42fc50850aa8a6b46c9459 Mon Sep 17 00:00:00 2001
From: Krasimir Georgiev <krasimir at google.com>
Date: Tue, 27 Aug 2024 16:58:48 +0000
Subject: [PATCH 2/4] fix formatting

---
 clang/lib/Format/UnwrappedLineParser.cpp | 4 ++--
 clang/unittests/Format/FormatTestJS.cpp  | 1 -
 2 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 7591eaeae461a7..506386526bd15f 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -4031,8 +4031,8 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
     case tok::coloncolon:
       break;
     default:
-      if (!JSPastExtendsOrImplements && !ClassName && Previous->is(tok::identifier) &&
-          Previous->isNot(TT_AttributeMacro)) {
+      if (!JSPastExtendsOrImplements && !ClassName &&
+          Previous->is(tok::identifier) && Previous->isNot(TT_AttributeMacro)) {
         ClassName = Previous;
       }
     }
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 734c1590c41ddb..4b29ba720f6823 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -587,7 +587,6 @@ TEST_F(FormatTestJS, GoogAnonymousClass) {
                "};");
 }
 
-
 TEST_F(FormatTestJS, IIFEs) {
   // Internal calling parens; no semi.
   verifyFormat("(function() {\n"

>From 8283b7f9bb422c9692ffa83c034dcab8aee41a9a Mon Sep 17 00:00:00 2001
From: Krasimir Georgiev <krasimir at google.com>
Date: Wed, 28 Aug 2024 08:04:46 +0000
Subject: [PATCH 3/4] add a token annotator test

---
 clang/unittests/Format/TokenAnnotatorTest.cpp | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index db44d418a84484..04a9fa760ce878 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -3238,6 +3238,12 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
   EXPECT_BRACE_KIND(Tokens[8], BK_BracedInit);
   EXPECT_BRACE_KIND(Tokens[11], BK_BracedInit);
   EXPECT_BRACE_KIND(Tokens[13], BK_Block);
+
+  Tokens = annotate("a = class extends goog.a {}",
+                    getGoogleStyle(FormatStyle::LanguageKind::LK_JavaScript));
+  ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+  EXPECT_TOKEN(Tokens[7], tok::l_brace, TT_ClassLBrace);
+  EXPECT_BRACE_KIND(Tokens[7], BK_Block);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsElaboratedTypeSpecifier) {

>From af262f7474492ef3d7fe3d3e46dace5e41e6d269 Mon Sep 17 00:00:00 2001
From: Krasimir Georgiev <krasimir at google.com>
Date: Wed, 28 Aug 2024 11:00:35 +0200
Subject: [PATCH 4/4] Update clang/unittests/Format/TokenAnnotatorTest.cpp

Co-authored-by: Owen Pan <owenpiano at gmail.com>
---
 clang/unittests/Format/TokenAnnotatorTest.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 04a9fa760ce878..12c5b03a6f8d01 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -3239,9 +3239,9 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
   EXPECT_BRACE_KIND(Tokens[11], BK_BracedInit);
   EXPECT_BRACE_KIND(Tokens[13], BK_Block);
 
-  Tokens = annotate("a = class extends goog.a {}",
-                    getGoogleStyle(FormatStyle::LanguageKind::LK_JavaScript));
-  ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+  Tokens = annotate("a = class extends goog.a {};",
+                    getGoogleStyle(FormatStyle::LK_JavaScript));
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
   EXPECT_TOKEN(Tokens[7], tok::l_brace, TT_ClassLBrace);
   EXPECT_BRACE_KIND(Tokens[7], BK_Block);
 }



More information about the cfe-commits mailing list