[clang-format] js handle anonymous classes#106242
Merged
krasimirgg merged 5 commits intollvm:mainfrom Aug 28, 2024
Merged
Conversation
Addresses a regression in JavaScript when formatting anonymous classes.
Member
|
@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. |
owenca
reviewed
Aug 28, 2024
Co-authored-by: Owen Pan <owenpiano@gmail.com>
Member
llvmbot
pushed a commit
to llvmbot/llvm-project
that referenced
this pull request
Aug 28, 2024
Addresses a regression in JavaScript when formatting anonymous classes. --------- Co-authored-by: Owen Pan <owenpiano@gmail.com> (cherry picked from commit 77d63cf)
Member
|
/pull-request #106390 |
tru
pushed a commit
to llvmbot/llvm-project
that referenced
this pull request
Sep 1, 2024
Addresses a regression in JavaScript when formatting anonymous classes. --------- Co-authored-by: Owen Pan <owenpiano@gmail.com> (cherry picked from commit 77d63cf)
kadircet
added a commit
to kadircet/llvm-project
that referenced
this pull request
Sep 13, 2024
This extends the fix in llvm#106242 for other derived class types.
kadircet
added a commit
to kadircet/llvm-project
that referenced
this pull request
Sep 16, 2024
This extends the fix in llvm#106242 for other derived class types.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses a regression in JavaScript when formatting anonymous classes.