prettier / prettier/plugin-php

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

オープン
#1,000 コメント 3 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
PHP
スター
1.9k
フォーク
139
PR マージ指標
30日以内にマージされた PR はありません

説明

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?

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

提供された PHP の入力例と出力例を動作確認用のフィクスチャとして使用し、その後 formatter のコメント処理パスを調査してください。記載された条件をすべて適用し、条件キーワードの前にあるコメントが条件のインデント位置にとどまり、無関係なコメントは既存のフォーマットを維持することを確認してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
php
領域
tooling
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。