llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format
            
<details>
<summary>Changes</summary>
Dictionary literal keys and strings in TypeScript type declarations can not be 
broken.

The problem was pointed out by @alexfh and @e-kud here:

https://reviews.llvm.org/D154093#4644512
--
Full diff: https://github.com/llvm/llvm-project/pull/66168.diff

3 Files Affected:

- (modified) clang/lib/Format/ContinuationIndenter.cpp (+6) 
- (modified) clang/unittests/Format/FormatTestJS.cpp (+22) 
- (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+19) 


<pre>
diff --git a/clang/lib/Format/ContinuationIndenter.cpp 
b/clang/lib/Format/ContinuationIndenter.cpp
index ac62dab1b07cdca..75ab08de42ea0e8 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -2241,6 +2241,12 @@ ContinuationIndenter::createBreakableToken(const 
FormatToken &amp;Current,
     if (Style.isJson() || !Style.BreakStringLiterals || !AllowBreak)
       return nullptr;
 
+    // Strings in TypeScript types and dictionary keys can not be broken.
+    if (Style.isJavaScript() &amp;&amp; (Current.is(TT_SelectorName) ||
+                                 State.Line-&gt;startsWith(Keywords.kw_type))) 
{
+      return nullptr;
+    }
+
     // Don&#x27;t break string literals inside preprocessor directives (except 
for
     // #define directives, as their contents are stored in separate lines and
     // are not affected by this check).
diff --git a/clang/unittests/Format/FormatTestJS.cpp 
b/clang/unittests/Format/FormatTestJS.cpp
index bc4c7ff7ba6b30f..309326569143df6 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -1596,6 +1596,28 @@ TEST_F(FormatTestJS, StringLiteralConcatenation) {
                &quot;var literal = `xxxxxx ${xxxxxxxxxxxxxxxxxxxxxx + &quot;
                &quot;xxxxxxxxxxxxxxxxxxxxxx} xxxxxx`;&quot;,
                getGoogleJSStyleWithColumns(14));
+
+  // Strings in a TypeScript type declaration can&#x27;t be broken.
+  verifyFormat(&quot;type x =\n&quot;
+               &quot;    
&#x27;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&#x27;;&quot;,
+               getGoogleJSStyleWithColumns(20));
+  verifyFormat(&quot;/* type */ type x =\n&quot;
+               &quot;    
&#x27;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&#x27;;&quot;,
+               getGoogleJSStyleWithColumns(20));
+  // Dictionary keys can&#x27;t be broken. Values can be broken.
+  verifyFormat(&quot;var w = {\n&quot;
+               &quot;  
&#x27;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&#x27;:\n&quot;
+               &quot;      &#x27;xxxxxxxxxx&#x27; +\n&quot;
+               &quot;      &#x27;xxxxxxxxxx&#x27; +\n&quot;
+               &quot;      &#x27;xxxxxxxxxx&#x27; +\n&quot;
+               &quot;      &#x27;xxxxxxxxxx&#x27; +\n&quot;
+               &quot;      &#x27;xxx&#x27;,\n&quot;
+               &quot;};&quot;,
+               &quot;var w = {\n&quot;
+               &quot;  
&#x27;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&#x27;:\n&quot;
+               &quot;      
&#x27;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&#x27;,\n&quot;
+               &quot;};&quot;,
+               getGoogleJSStyleWithColumns(20));
 }
 
 TEST_F(FormatTestJS, RegexLiteralClassification) {
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp 
b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 434852983712940..9ab1eae85f29c4d 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2040,6 +2040,25 @@ TEST_F(TokenAnnotatorTest, 
UnderstandDesignatedInitializers) {
   EXPECT_TOKEN(Tokens[13], tok::period, TT_DesignatedInitializerPeriod);
 }
 
+TEST_F(TokenAnnotatorTest, UnderstandsJavaScript) {
+  auto Annotate = [this](llvm::StringRef Code) {
+    return annotate(Code, getLLVMStyle(FormatStyle::LK_JavaScript));
+  };
+
+  // Dictionary.
+  auto Tokens = Annotate(&quot;var x = {&#x27;x&#x27; : 1, &#x27;y&#x27; : 
2};&quot;);
+  ASSERT_EQ(Tokens.size(), 14u) &lt;&lt; Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::l_brace, TT_DictLiteral);
+  EXPECT_TOKEN(Tokens[4], tok::string_literal, TT_SelectorName);
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_DictLiteral);
+  EXPECT_TOKEN(Tokens[8], tok::string_literal, TT_SelectorName);
+  EXPECT_TOKEN(Tokens[9], tok::colon, TT_DictLiteral);
+  // Change when we need to annotate these.
+  EXPECT_BRACE_KIND(Tokens[3], BK_Unknown);
+  EXPECT_BRACE_KIND(Tokens[11], BK_Unknown);
+  EXPECT_TOKEN(Tokens[11], tok::r_brace, TT_Unknown);
+}
+
 } // namespace
 } // namespace format
 } // namespace clang
</pre>
</details>


https://github.com/llvm/llvm-project/pull/66168
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to