[clang] [clang-format] js handle anonymous classes (PR #106242)
Krasimir Georgiev via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 27 09:12:06 PDT 2024
https://github.com/krasimirgg created https://github.com/llvm/llvm-project/pull/106242
Addresses a regression in JavaScript when formatting anonymous classes.
>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] [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"
More information about the cfe-commits
mailing list