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

Đang mở
#2,494 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
55/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
php
Lĩnh vực
tooling

Hướng nghiên cứu

Bắt đầu với bản tái hiện PHP tối giản từ issue bằng cách sử dụng trình định dạng dòng lệnh, sau đó so sánh đầu ra của nó với công cụ báo cáo hoặc playground. Truy vết đường dẫn định dạng khối PHP và chú thích ở cuối chịu trách nhiệm chèn thẻ mở bổ sung. Được xem là hoàn tất khi trường hợp comment-after-if được định dạng mà không có \u003c?php thừa và vẫn hợp lệ về cú pháp.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
PHP
Star
1.9k
Fork
139
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của prettier/plugin-php

Tất cả issue của prettier/plugin-php

Issue tương tự

Thêm issue về PHP

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.