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:
โ Applies to:
if (...) {
// ...
// Last line before the conditional keyword
} else {
}
โ Does not apply to:
if (...) {
// ...
// The conditional keyword is not on next line
}
else {
}
โ Applies to:
if (...) {
// ...
// Same level
} else {
}
โ Does not apply to:
if (...) {
// ...
// Not the same level
} else {
}
โ 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 (...) {
// ...
}
```php
if (...) {
// ...
// Nobody would ever do that, but it needs to be specified
} if (...) {
}
```
โ 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 {
}
โ Applies to:
if (...) {
$foo = 'bar';
// Comment
} else {
}
```php
if (...) {
// Comment
// Comment
} else {
}
```php
if (...) {
// This code example is probably debateable.
// Comment
} else {
}
โ Does not apply to:
if (...) {
// Comment
} else {
}
php
if (...) {
// Does not apply because this is
// a single comment as per precondition
} else {
}
Conclusion: What do you think?
I agree, also we should support comments between, note in https://github.com/prettier/plugin-php/issues/829
Yeah, the plugin already formats comments outside the condition body correctly:
if (...) {
}
// Comment
else {
}
That's why I explicitely excluded those in the listed characteristics. :)
Oh, also I just realized: issue #1000! ๐
Most helpful comment
Oh, also I just realized: issue #1000! ๐