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] Disable string breaking in JS for now #66372

Merged
merged 2 commits into from
Sep 15, 2023

Conversation

sstwcw
Copy link
Contributor

@sstwcw sstwcw commented Sep 14, 2023

See the discussion
here.

The functionality is not mature enough.

See the discussion
[here](llvm#66168 (comment)).

The functionality is not mature enough.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang-format labels Sep 14, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 14, 2023

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-format

Changes See the discussion [here](https://github.com//pull/66168#issuecomment-1719038797).

The functionality is not mature enough.

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

4 Files Affected:

  • (modified) clang/docs/ClangFormatStyleOptions.rst (+5-5)
  • (modified) clang/include/clang/Format/Format.h (+5-5)
  • (modified) clang/lib/Format/ContinuationIndenter.cpp (+4-9)
  • (modified) clang/unittests/Format/FormatTestJS.cpp (+2-112)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index d5d9faa5c78cffb..4ab0b3a207270dc 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -2783,17 +2783,17 @@ the configuration (without a prefix: ``Auto``).
      const char* x =
          "veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString";
 
-  In C#, Java, and JavaScript:
+  In C# and Java:
 
   .. code-block:: c++
 
      true:
-     var x = "veryVeryVeryVeryVeryVe" +
-             "ryVeryVeryVeryVeryVery" +
-             "VeryLongString";
+     string x = "veryVeryVeryVeryVeryVe" +
+                "ryVeryVeryVeryVeryVery" +
+                "VeryLongString";
 
      false:
-     var x =
+     string x =
          "veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString";
 
   C# and JavaScript interpolated strings are not broken.
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index c7bd356b7faeded..3bf70838d059086 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -2062,15 +2062,15 @@ struct FormatStyle {
   ///        "veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString";
   /// \endcode
   ///
-  /// In C#, Java, and JavaScript:
+  /// In C# and Java:
   /// \code
   ///    true:
-  ///    var x = "veryVeryVeryVeryVeryVe" +
-  ///            "ryVeryVeryVeryVeryVery" +
-  ///            "VeryLongString";
+  ///    string x = "veryVeryVeryVeryVeryVe" +
+  ///               "ryVeryVeryVeryVeryVery" +
+  ///               "VeryLongString";
   ///
   ///    false:
-  ///    var x =
+  ///    string x =
   ///        "veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString";
   /// \endcode
   ///
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 6673b5c703b835f..15b9ad41a37af59 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -2237,15 +2237,10 @@ ContinuationIndenter::createBreakableToken(const FormatToken &Current,
                                            LineState &State, bool AllowBreak) {
   unsigned StartColumn = State.Column - Current.ColumnWidth;
   if (Current.isStringLiteral()) {
-    // Strings in JSON can not be broken.
-    if (Style.isJson() || !Style.BreakStringLiterals || !AllowBreak)
-      return nullptr;
-
-    // Strings in TypeScript types and dictionary keys can not be broken.
-    if (Style.isJavaScript() &&
-        (Current.is(TT_SelectorName) ||
-         State.Line->startsWith(Keywords.kw_type) ||
-         State.Line->startsWith(tok::kw_export, Keywords.kw_type))) {
+    // Strings in JSON can not be broken. Breaking strings in JavaScript is
+    // disabled for now.
+    if (Style.isJson() || Style.isJavaScript() || !Style.BreakStringLiterals ||
+        !AllowBreak) {
       return nullptr;
     }
 
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 51543d0a54d8561..23b010dbc982684 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -1506,121 +1506,11 @@ TEST_F(FormatTestJS, StringLiteralConcatenation) {
   verifyFormat("var literal = 'hello ' +\n"
                "    'world';");
 
-  // Long strings should be broken.
+  // String breaking is disabled for now.
   verifyFormat("var literal =\n"
-               "    'xxxxxxxx ' +\n"
-               "    'xxxxxxxx';",
+               "    'xxxxxxxx xxxxxxxx';",
                "var literal = 'xxxxxxxx xxxxxxxx';",
                getGoogleJSStyleWithColumns(17));
-  verifyFormat("var literal =\n"
-               "    'xxxxxxxx ' +\n"
-               "    'xxxxxxxx';",
-               "var literal = 'xxxxxxxx xxxxxxxx';",
-               getGoogleJSStyleWithColumns(18));
-  verifyFormat("var literal =\n"
-               "    'xxxxxxxx' +\n"
-               "    ' xxxxxxxx';",
-               "var literal = 'xxxxxxxx xxxxxxxx';",
-               getGoogleJSStyleWithColumns(16));
-  // The quotes should be correct.
-  for (char OriginalQuote : {'\'', '"'}) {
-    auto VerifyQuotes = [=](FormatStyle::JavaScriptQuoteStyle StyleQuote,
-                            char TargetQuote) {
-      auto Style = getGoogleJSStyleWithColumns(17);
-      Style.JavaScriptQuotes = StyleQuote;
-      std::string Target{"var literal =\n"
-                         "    \"xxxxxxxx \" +\n"
-                         "    \"xxxxxxxx\";"};
-      std::string Original{"var literal = \"xxxxxxxx xxxxxxxx\";"};
-      std::replace(Target.begin(), Target.end(), '"', TargetQuote);
-      std::replace(Original.begin(), Original.end(), '"', OriginalQuote);
-      verifyFormat(Target, Original, Style);
-    };
-    VerifyQuotes(FormatStyle::JSQS_Leave, OriginalQuote);
-    VerifyQuotes(FormatStyle::JSQS_Single, '\'');
-    VerifyQuotes(FormatStyle::JSQS_Double, '"');
-  }
-  // Parentheses should be added when necessary.
-  verifyFormat("var literal =\n"
-               "    ('xxxxxxxx ' +\n"
-               "     'xxxxxx')[0];",
-               "var literal = 'xxxxxxxx xxxxxx'[0];",
-               getGoogleJSStyleWithColumns(18));
-  auto Style = getGoogleJSStyleWithColumns(20);
-  Style.SpacesInParens = FormatStyle::SIPO_Custom;
-  Style.SpacesInParensOptions.Other = true;
-  verifyFormat("var literal =\n"
-               "    ( 'xxxxxxxx ' +\n"
-               "      'xxxxxx' )[0];",
-               "var literal = 'xxxxxxxx xxxxxx'[0];", Style);
-  // FIXME: When the part before the string literal is shorter than the
-  // continuation indentation, and the option AlignAfterOpenBracket is set to
-  // AlwaysBreak which is the default for the Google style, the unbroken string
-  // does not get to a new line while the broken string does due to the added
-  // parentheses. The formatter does not do it in one pass.
-  EXPECT_EQ(
-      "x = ('xxxxxxxx ' +\n"
-      "     'xxxxxx')[0];",
-      format("x = 'xxxxxxxx xxxxxx'[0];", getGoogleJSStyleWithColumns(18)));
-  verifyFormat("x =\n"
-               "    ('xxxxxxxx ' +\n"
-               "     'xxxxxx')[0];",
-               getGoogleJSStyleWithColumns(18));
-  // Breaking of template strings and regular expressions is not implemented.
-  verifyFormat("var literal =\n"
-               "    `xxxxxxxx xxxxxxxx`;",
-               getGoogleJSStyleWithColumns(18));
-  verifyFormat("var literal =\n"
-               "    /xxxxxxxx xxxxxxxx/;",
-               getGoogleJSStyleWithColumns(18));
-  // There can be breaks in the code inside a template string.
-  verifyFormat("var literal = `xxxxxx ${\n"
-               "    xxxxxxxxxx} xxxxxx`;",
-               "var literal = `xxxxxx ${xxxxxxxxxx} xxxxxx`;",
-               getGoogleJSStyleWithColumns(14));
-  verifyFormat("var literal = `xxxxxx ${\n"
-               "    xxxxxxxxxx} xxxxxx`;",
-               "var literal = `xxxxxx ${xxxxxxxxxx} xxxxxx`;",
-               getGoogleJSStyleWithColumns(15));
-  // Identifiers inside the code inside a template string should not be broken
-  // even if the column limit is exceeded.  This following behavior is not
-  // optimal.  The part after the closing brace which exceeds the column limit
-  // can be put on a new line.  Change this test when it is implemented.
-  verifyFormat("var literal = `xxxxxx ${\n"
-               "    xxxxxxxxxxxxxxxxxxxxxx} xxxxxx`;",
-               "var literal = `xxxxxx ${xxxxxxxxxxxxxxxxxxxxxx} xxxxxx`;",
-               getGoogleJSStyleWithColumns(14));
-  verifyFormat("var literal = `xxxxxx ${\n"
-               "    xxxxxxxxxxxxxxxxxxxxxx +\n"
-               "    xxxxxxxxxxxxxxxxxxxxxx} xxxxxx`;",
-               "var literal = `xxxxxx ${xxxxxxxxxxxxxxxxxxxxxx + "
-               "xxxxxxxxxxxxxxxxxxxxxx} xxxxxx`;",
-               getGoogleJSStyleWithColumns(14));
-
-  // Strings in a TypeScript type declaration can't be broken.
-  verifyFormat("type x =\n"
-               "    'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';",
-               getGoogleJSStyleWithColumns(20));
-  verifyFormat("/* type */ type x =\n"
-               "    'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';",
-               getGoogleJSStyleWithColumns(20));
-  verifyFormat("export type x =\n"
-               "    'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';",
-               getGoogleJSStyleWithColumns(20));
-  // Dictionary keys can't be broken. Values can be broken.
-  verifyFormat("var w = {\n"
-               "  'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx':\n"
-               "      'xxxxxxxxxx' +\n"
-               "      'xxxxxxxxxx' +\n"
-               "      'xxxxxxxxxx' +\n"
-               "      'xxxxxxxxxx' +\n"
-               "      'xxx',\n"
-               "};",
-               "var w = {\n"
-               "  'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx':\n"
-               "      'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',\n"
-               "};",
-               getGoogleJSStyleWithColumns(20));
 }
 
 TEST_F(FormatTestJS, RegexLiteralClassification) {

Copy link
Contributor

@alexfh alexfh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the right call for now. There are multiple contexts in JS, TS and libraries like Closure, where simple string literals are expected. Getting all of these right may require some back-and-forths, during which clang-format will continue producing invalid code. Let's get back to a good state and figure out details after that.


false:
var x =
string x =
"veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString";

C# and JavaScript interpolated strings are not broken.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
C# and JavaScript interpolated strings are not broken.
C# interpolated strings are not broken.

(Current.is(TT_SelectorName) ||
State.Line->startsWith(Keywords.kw_type) ||
State.Line->startsWith(tok::kw_export, Keywords.kw_type))) {
// Strings in JSON can not be broken. Breaking strings in JavaScript is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Strings in JSON can not be broken. Breaking strings in JavaScript is
// Strings in JSON cannot be broken. Breaking strings in JavaScript is

@eywdck2l eywdck2l merged commit ae90f68 into llvm:main Sep 15, 2023
1 of 2 checks passed
@sstwcw sstwcw deleted the format-js-strings branch September 16, 2023 14:32
ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Sep 19, 2023
See the discussion

[here](llvm#66168 (comment)).

The functionality is not mature enough.
@owenca owenca removed the clang Clang issues not falling into any other category label May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants