-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang-format] js handle anonymous classes #106242
Conversation
Addresses a regression in JavaScript when formatting anonymous classes.
@llvm/pr-subscribers-clang-format Author: Krasimir Georgiev (krasimirgg) ChangesAddresses a regression in JavaScript when formatting anonymous classes. Full diff: https://github.com/llvm/llvm-project/pull/106242.diff 2 Files Affected:
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"
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LG except some nits.
Co-authored-by: Owen Pan <[email protected]>
Addresses a regression in JavaScript when formatting anonymous classes. --------- Co-authored-by: Owen Pan <[email protected]> (cherry picked from commit 77d63cf)
/pull-request #106390 |
Addresses a regression in JavaScript when formatting anonymous classes. --------- Co-authored-by: Owen Pan <[email protected]> (cherry picked from commit 77d63cf)
This extends the fix in llvm#106242 for other derived class types.
This extends the fix in llvm#106242 for other derived class types.
Addresses a regression in JavaScript when formatting anonymous classes.