Skip to content

Commit

Permalink
[clang-format] Do not break on JS fields like on goto labels (#76233)
Browse files Browse the repository at this point in the history
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[];
};
```
  • Loading branch information
ilya-biryukov committed Dec 22, 2023
1 parent 48b9106 commit d03beb9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1650,8 +1650,10 @@ void UnwrappedLineParser::parseStructuralElement(
return;
}
// In Verilog labels can be any expression, so we don't do them here.
if (!Style.isVerilog() && Tokens->peekNextToken()->is(tok::colon) &&
!Line->MustBeDeclaration) {
// JS doesn't have macros, and within classes colons indicate fields, not
// labels.
if (!Style.isJavaScript() && !Style.isVerilog() &&
Tokens->peekNextToken()->is(tok::colon) && !Line->MustBeDeclaration) {
nextToken();
Line->Tokens.begin()->Tok->MustBreakBefore = true;
FormatTok->setFinalizedType(TT_GotoLabelColon);
Expand Down
6 changes: 6 additions & 0 deletions clang/unittests/Format/FormatTestJS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2836,5 +2836,11 @@ TEST_F(FormatTestJS, AlignConsecutiveAssignmentsAndDeclarations) {
Style);
}

TEST_F(FormatTestJS, DontBreakFieldsAsGoToLabels) {
verifyFormat("export type Params = Config&{\n"
" columns: Column[];\n"
"};");
}

} // namespace format
} // end namespace clang

0 comments on commit d03beb9

Please sign in to comment.