Skip to content

Commit d03beb9

Browse files
[clang-format] Do not break on JS fields like on goto labels (#76233)
This regressions was introduced in 70d7ea0. The commit moved some code and correctly picked up an explicit check for not running on Verilog. However, the moved code also never ran for JavaScript and after the commit we run it there and this causes the wrong formatting of: ```js export type Params = Config&{ columns: Column[]; }; ``` into ```js export type Params = Config&{ columns: Column[]; }; ```
1 parent 48b9106 commit d03beb9

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,8 +1650,10 @@ void UnwrappedLineParser::parseStructuralElement(
16501650
return;
16511651
}
16521652
// In Verilog labels can be any expression, so we don't do them here.
1653-
if (!Style.isVerilog() && Tokens->peekNextToken()->is(tok::colon) &&
1654-
!Line->MustBeDeclaration) {
1653+
// JS doesn't have macros, and within classes colons indicate fields, not
1654+
// labels.
1655+
if (!Style.isJavaScript() && !Style.isVerilog() &&
1656+
Tokens->peekNextToken()->is(tok::colon) && !Line->MustBeDeclaration) {
16551657
nextToken();
16561658
Line->Tokens.begin()->Tok->MustBreakBefore = true;
16571659
FormatTok->setFinalizedType(TT_GotoLabelColon);

clang/unittests/Format/FormatTestJS.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2836,5 +2836,11 @@ TEST_F(FormatTestJS, AlignConsecutiveAssignmentsAndDeclarations) {
28362836
Style);
28372837
}
28382838

2839+
TEST_F(FormatTestJS, DontBreakFieldsAsGoToLabels) {
2840+
verifyFormat("export type Params = Config&{\n"
2841+
" columns: Column[];\n"
2842+
"};");
2843+
}
2844+
28392845
} // namespace format
28402846
} // end namespace clang

0 commit comments

Comments
 (0)