prettier / prettier/plugin-php

Prettier inserts a syntax error when there is a comment after an if statement

Offen
#2,494 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
PHP
Sterne
1.9k
Forks
139
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

When prettier is used via command line or via VSCode shortcut, prettier inserts an extra <?php string, creating a syntax error.

Input:

<?php if (true): // example comment
  ?>
  <h1>Heading</h1>
<?php endif; ?>

Output:

<?php if (true):<?php
  // example comment
  ?>
  <h1>Heading</h1>
<?php endif; ?>

This behavior, however, is not present within the reporting tool, where the output is as expected: @prettier/plugin-php v0.25.0
Playground link

The .prettierrc file:

{
  "useTabs": false,
  "tabWidth": 2,
  "trailingComma": "es5",
  "semi": true,
  "singleQuote": true,
  "arrowParens": "avoid",
  "printWidth": 120,
  "plugins": ["@prettier/plugin-php"]
}
  • Prettier version: 3.8.3
  • PHP Plugin version: 0.25.0
Further information

This issue only happens when a comment is right after an if statement, and the comment is the last thing inside the php block. Here are examples of cases where the code is formatted correctly:

<?php
// example comment
if (true): ?>
  <h1>Heading</h1>
<?php endif; ?>
<?php
$test = 'Foo';
if (true):
  // example comment
  $test = 'Bar'; ?>
  <h1>Heading</h1>
<?php
endif; ?>

Those examples are after they were formatted using the command line prettier.

The second example is somewhat questionable, as I suspect the endif statement should not be moved to a new line - before the formatting happened it was in a single line, like in the first example.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der minimalen PHP-Reproduktion aus dem Issue unter Verwendung des Befehlszeilen-Formatierers und vergleiche dann deren Ausgabe mit dem Reporting-Tool oder Playground. Verfolge den Formatierungspfad für PHP-Blöcke und nachgestellte Kommentare, der für das Einfügen des zusätzlichen öffnenden Tags verantwortlich ist. Erledigt ist die Aufgabe, wenn der Fall comment-after-if ohne ein zusätzliches \u003c?php formatiert wird und syntaktisch gültig bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
php
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
55/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.