Skip to content
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

Merged
merged 5 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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} { }
Expand All @@ -4027,8 +4031,8 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
case tok::coloncolon:
break;
default:
if (!ClassName && Previous->is(tok::identifier) &&
Previous->isNot(TT_AttributeMacro)) {
if (!JSPastExtendsOrImplements && !ClassName &&
Previous->is(tok::identifier) && Previous->isNot(TT_AttributeMacro)) {
ClassName = Previous;
}
}
Expand Down
8 changes: 8 additions & 0 deletions clang/unittests/Format/FormatTestJS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,14 @@ TEST_F(FormatTestJS, GoogScopes) {
"});");
}

TEST_F(FormatTestJS, GoogAnonymousClass) {
owenca marked this conversation as resolved.
Show resolved Hide resolved
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"
Expand Down
6 changes: 6 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
krasimirgg marked this conversation as resolved.
Show resolved Hide resolved
EXPECT_TOKEN(Tokens[7], tok::l_brace, TT_ClassLBrace);
EXPECT_BRACE_KIND(Tokens[7], BK_Block);
}

TEST_F(TokenAnnotatorTest, UnderstandsElaboratedTypeSpecifier) {
Expand Down
Loading