豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

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

@Pawel-Bakalarz

Description

@Pawel-Bakalarz

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions