prettier / prettier/plugin-php

Unstable runs: comment in php tag mixed with html

Open
#2,426 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
1.9k
Forks
139
PR merge metrics
No merged PRs in 30d

Description

When prettier-php has a comment directly after <php, it reformats the comment on each successive run, resulting in strange behaviour. The most bizarre is copying the line on each successive run of prettier.

note: By stable, I mean the second time you run prettier, it doesn't change the files. If there's a better word for this, I'm happy to change my description.

This bug is captured in a comment on another issue. However, due to this bug breaking a core properties of prettier, stable transformations, I think it's worth its own issue.

similar bugs:

Prettier 3.5.3

PHP Plugin 0.22.4

# Options (if any): None
module.exports = {
	plugins: ["@prettier/plugin-php"],
};

Example 1

Input:

<?php
/**
 * The template for displaying the footer
 */
?>

<div class="footer">
	<?php
	// Footer
	get_template_part("template-parts/footer", "2022"); ?>
</div>

<?php wp_footer(); ?>

Output run 1:

<?php
/**
 * The template for displaying the footer
 */
?>

<div class="footer">
	<?php // Footer
 get_template_part("template-parts/footer", "2022"); ?>
</div>

<?php wp_footer(); ?>

Output run 2:

<?php
/**
 * The template for displaying the footer
 */
?>

<div class="footer">
	<?php // Footer
// Footer
get_template_part("template-parts/footer", "2022"); ?>
</div>

<?php wp_footer(); ?>

Output run 3: each successive run adds a new line with // Footer

Example 2

Input:

		</main><!-- #main -->
	</div><!-- #primary -->

<?php
// get_sidebar();
get_footer();

Output run 1:

		</main><!-- #main -->
	</div><!-- #primary -->

<?php // get_sidebar();
get_footer();

Output run 2:

		</main><!-- #main -->
	</div><!-- #primary -->

<?php // get_sidebar();

get_footer();

Example 3

Input:

			<header class="page-header">
				<h1 class="page-title">
				<?php
				/* translators: %s: search query. */
				printf(
    	esc_html__("Search Results for: %s", "gpo18"),
    	"<span>" . get_search_query() . "</span>"
    ); ?>
				</h1>
			</header><!-- .page-header -->

Output run 1:

			<header class="page-header">
				<h1 class="page-title">
				<?php /* translators: %s: search query. */
    printf(
    	esc_html__("Search Results for: %s", "gpo18"),
    	"<span>" . get_search_query() . "</span>"
    ); ?>
				</h1>
			</header><!-- .page-header -->

Output run 2:

			<header class="page-header">
				<h1 class="page-title">
				 /* translators: %s: search query. */<?php
  	/* translators: %s: search query. */
  	printf(
    	esc_html__("Search Results for: %s", "gpo18"),
    	"<span>" . get_search_query() . "</span>"
    ); ?>
				</h1>
			</header><!-- .page-header -->

Output run 3:

			<header class="page-header">
				<h1 class="page-title">
				 /* translators: %s: search query. */ /* translators: %s: search query. */<?php
  	/* translators: %s: search query. */
  	printf(
     	esc_html__("Search Results for: %s", "gpo18"),
     	"<span>" . get_search_query() . "</span>"
     ); ?>
				</h1>
			</header><!-- .page-header -->

cc: @movva-gpu

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the three examples with Prettier 3.5.3 and PHP Plugin 0.22.4, starting with comments immediately after the PHP tag. Trace the formatter path handling PHP and mixed HTML, then add regression coverage for the examples; done means repeated formatting produces identical output and no comment lines are duplicated or moved.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.