Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Formatter places opening """ of text block on same line as ? / : operators in ternary expressions

Aperta
#1,378 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
68/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Tranquilla
Stack tecnologico
java
Ambito
tooling

Direzione di ricerca

Inizia formattando il file fornito /tmp/TernaryBug.java con /tmp/google-java-format.jar e confronta il diff mostrato. Traccia la gestione da parte del formatter delle espressioni ternarie contenenti blocchi di testo, quindi verifica che ogni tripla virgoletta di apertura sia l'unico contenuto non costituito da spazi bianchi nella relativa riga e che l'output previsto non sia più in conflitto con il controllo TextBlockGoogleStyleFormatting di Checkstyle.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Description

google-java-format formats ternary expressions containing text blocks such that the opening """ is placed on the same line as the ? and : operators. This appears to violate Google Java Style Guide Section 4.8.9, which states:

The opening """ of a text block is always on a new line. That line may either follow the same indentation rules as other constructs, or it may have no indentation at all (so it starts at the left margin). The closing """ is on a new line with the same indentation as the opening """, and may be followed on the same line by further code. Each line of text in the text block is indented at least as much as the opening and closing """. (If a line is indented further, then the string literal defined by the text block will have space at the start of that line.)

The key requirement is that the """ must be on a new line, but the formatter places an operator (? or :) immediately before it on the same line, meaning the """ is no longer on the new line.

Reproduction
vivek@Viveks-MacBook-Air checkstyle % cat > /tmp/TernaryBug.java << 'EOF'
class TernaryBug {
    boolean flag = true;

    String a = flag
        ? """
          yes
          """
        : """
          no
          """;
}
EOF

vivek@Viveks-MacBook-Air checkstyle % java -jar /tmp/google-java-format.jar /tmp/TernaryBug.java > /tmp/TernaryBug_formatted.java
diff -u /tmp/TernaryBug.java /tmp/TernaryBug_formatted.java
--- /tmp/TernaryBug.java        2026-04-18 18:43:21
+++ /tmp/TernaryBug_formatted.java      2026-04-18 18:43:26
@@ -1,11 +1,12 @@

 class TernaryBug {
-    boolean flag = true;
+  boolean flag = true;
 
-    String a = flag
-        ? """
+  String a =
+      flag
+          ? """
           yes
           """
-        : """
+          : """
           no
           """;
 }

vivek@Viveks-MacBook-Air checkstyle % cat > /tmp/TernaryBug.java << 'EOF'
class TernaryBug {
    boolean flag = true;

    String d = flag ?
        """
        yes
        """
        :
        """
        no
        """;
}
EOF

vivek@Viveks-MacBook-Air checkstyle % java -jar /tmp/google-java-format.jar /tmp/TernaryBug.java > /tmp/TernaryBug_formatted.java
diff -u /tmp/TernaryBug.java /tmp/TernaryBug_formatted.java
--- /tmp/TernaryBug.java        2026-04-18 18:45:14
+++ /tmp/TernaryBug_formatted.java      2026-04-18 18:45:19
@@ -1,12 +1,12 @@

 class TernaryBug {
-    boolean flag = true;
+  boolean flag = true;
 
-    String d = flag ?
-        """
-        yes
-        """
-        :
-        """
-        no
-        """;
+  String d =
+      flag
+          ? """
+          yes
+          """
+          : """
+          no
+          """;
 }

Expected Behavior

The formatter should place the opening"""on its own line, separate from the ? and : operators. For example:

class TernaryBug {
  boolean flag = true;

  String a =
      flag
          ?
              """
              yes
              """
          :
              """
              no
              """;
}

Or any equivalent layout where """ is the only non-whitespace content on its starting line.

Why This Matters

Checkstyle's TextBlockGoogleStyleFormatting check enforces the Google Java Style Guide rule that the opening """ must be on its own line. The current formatter output directly conflicts with this check, code formatted by google-java-format produces Checkstyle violations. This creates a situation where developers cannot simultaneously satisfy both the formatter and the style checker, which are both intended to enforce the same Google Java Style Guide.

Lingua principale
Java
Stelle
6.2k
Fork
936
Merge medio
6m
PR unite (30g)
3

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di google/google-java-format

Tutte le issue di google/google-java-format

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.