Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

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

Abierto
#1,378 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
68/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Tranquilo
Stack tecnológico
java
Área
tooling

Línea de trabajo

Comienza formateando el archivo proporcionado /tmp/TernaryBug.java con /tmp/google-java-format.jar y compara el diff mostrado. Sigue cómo el formateador gestiona las expresiones ternarias que contienen bloques de texto y, después, verifica que cada triple comilla de apertura sea el único contenido que no sea espacio en blanco de su línea y que la salida esperada ya no entre en conflicto con la comprobación TextBlockGoogleStyleFormatting de Checkstyle.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Java
Estrellas
6.2k
Forks
936
Merge medio
6 min
PR fusionados (30 d)
3

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de google/google-java-format

Todos los issues de google/google-java-format

Issues similares

Más issues de Java

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.