prettier / prettier/plugin-php

Keep comments associated with a condition at the condition's indentation level

Abierto
#1,000 3 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Lenguaje dominante
PHP
Estrellas
1.9k
Forks
139
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

Note: This very same issue has been open for a pretty long time over at the Prettier repo. It's gotten positive signals from maintainers, but nobody has taken on it yet.

@prettier/plugin-php v0.10.2
Playground link

Input:

<?php

// Do something
if (condition) {
    do_something();

// Explaining why doing another thing
} else {
    do_another_thing();
}

Output:

<?php

// Do something
if (condition) {
    do_something();

    // Explaining why doing another thing
} else {
    do_another_thing();
}

Expected behavior:

<?php

// Do something
if (condition) {
    do_something();

// Explaining why doing another thing
} else {
    do_another_thing();
}

Reasoning:
Comments that can clearly be associated with a condition should stick with that condition, indentation-wise.

Instead, the comment is (incorrectly) recognized as part of the if block and indented with it.

Resolving:
I'm going to list the characteristics to recognize comments that should be aligned with their associated conditions. These characteristics are used in conjunction, so all of them need to apply to align a comment with its condition.

However, there's one precondition which I'll send ahead: For the following characteristics, multiple successive single-line comments with the same indentation level and with no empty lines between them, are one comment.

It's easier to grasp in code:

// In regard of the following characteristics,
// this is actually a single comment.

So, the characteristics for a comment to be intended with its condition are the following:

  • The comment ends on the last line before a conditional keyword after a brace.

    ✅ Applies to:

    if (...) {
        // ...
    
    // Last line before the conditional keyword
    } else {
    }
    

    ❌ Does not apply to:

    if (...) {
        // ...
    
    // The conditional keyword is not on next line
    }
    else {
    }
    
  • The comment has the same indentation level as the conditional keyword.

    ✅ Applies to:

    if (...) {
        // ...
    
    // Same level
    } else {
    }
    

    ❌ Does not apply to:

    if (...) {
        // ...
    
        // Not the same level
    } else {
    }
    
  • The associated conditional keyword continues an existing condition.

    ✅ Applies to:

    if (...) {
        // ...
    
    // The `else` refers to the `if` block above it
    } else {
    }
    

    ❌ Does not apply to:

    // The `if` opens the first branch of a condition
    if (...) {
        // ...
    }
    
    if (...) {
        // ...
    
    // Nobody would ever do that, but it needs to be specified
    } if (...) {
    
    }
    
  • The continued condition has a body wrapped in braces.

    ✅ Applies to:

    if (...) {
        // ...
    
    // The `if` body is wrapped in braces
    } else {
    }
    

    ❌ Does not apply to:

    if (...)
        // ...
    
    // The `if` body is not wrapped in braces
    else {
    }
    
  • The surrounding block contains code other than the comment.

    ✅ Applies to:

    if (...) {
        $foo = 'bar';
    
    // Comment
    } else {
    }
    
    if (...) {
        // Comment
    
    // Comment
    } else {
    }
    
    if (...) {
    // This code example is probably debateable.
    
    // Comment
    } else {
    }
    

    ❌ Does not apply to:

    if (...) {
    // Comment
    } else {
    }
    
    if (...) {
    // Does not apply because this is
    // a single comment as per precondition
    } else {
    }
    

Conclusion: What do you think?

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.

Línea de trabajo

Usa los ejemplos de entrada y salida de PHP proporcionados como fixtures de comportamiento y, después, inspecciona la ruta de gestión de comentarios del formateador. Aplica conjuntamente las condiciones indicadas y verifica que los comentarios anteriores a una palabra clave condicional permanezcan con la indentación de la condición, mientras que los comentarios no relacionados conserven el formato existente.

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

Evaluación

Stack tecnológico
php
Área
tooling
Tipo de issue
Error
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.