[clang] [clang-format] Do not break on JS fields like on goto labels (PR #76233)

via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 22 05:20:37 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: Ilya Biryukov (ilya-biryukov)

<details>
<summary>Changes</summary>

This regressions was introduced in 70d7ea0cebcf363cd0ddcfb76375fb5fada87dd5. 
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[];
};
```

---
Full diff: https://github.com/llvm/llvm-project/pull/76233.diff


2 Files Affected:

- (modified) clang/lib/Format/UnwrappedLineParser.cpp (+4-2) 
- (modified) clang/unittests/Format/FormatTestJS.cpp (+6) 


``````````diff
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index c38b4c884070bb..684609747a5513 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -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);
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index e185eceb353057..3aded8f3726d8b 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -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

``````````

</details>


https://github.com/llvm/llvm-project/pull/76233


More information about the cfe-commits mailing list