prettier / prettier/plugin-php

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

Aperta
#2,494 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
PHP
Stelle
1.9k
Fork
139
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con la riproduzione PHP minima dell’issue usando il formattatore da riga di comando, quindi confronta il suo output con lo strumento di reporting o il playground. Traccia il percorso di formattazione dei blocchi PHP e dei commenti finali responsabile dell’inserimento del tag di apertura aggiuntivo. Il lavoro è completato quando il caso comment-after-if viene formattato senza un \u003c?php aggiuntivo e rimane sintatticamente valido.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
php
Ambito
tooling
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
55/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.