bmewburn / vscode-intelephense

PHP intellisense for Visual Studio Code
https://intelephense.com
Other
1.62k stars 94 forks source link

Invalid Switch/Case Formatting with Alternative syntax for control structures #2975

Open DMouse10462 opened 2 months ago

DMouse10462 commented 2 months ago

Describe the bug Switch/case, when used as a control structure within (HTML) output, must not contain whitespace between the switch statement and the first case. See warning at https://www.php.net/manual/en/control-structures.alternative-syntax.php

The formatter provided indents the syntax as with other control structures, violating this rule.

To Reproduce Format the following code:

<?php
$var = 1;
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
<?php switch ($var): ?>
<?php case (1): ?>
    THING 1
    <?php break ?>
<?php case (2): ?>
    THING 2
    <?php break ?>
<?php endswitch ?>
</body>

</html>

Observe the output, which adds indentation to the first case statement in violation of the syntax requirements:

<?php
$var = 1;
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <?php switch ($var): ?>
    <?php
        case (1): ?>
            THING 1
            <?php break ?>
        <?php
        case (2): ?>
            THING 2
            <?php break ?>
    <?php endswitch ?>
</body>

</html>

An error appears at the opening <?php token with the following message:

syntax error, unexpected T_INLINE_HTML " ", expecting "endswitch" or "case" or "default"

Expected behavior No indentation should be added before <?php case (1): ?>. Ideally, formatting should also not break the case statement onto a new line from <?php.

Platform and version Windows 11 Version 22H2 (OS Build 19405.4651) VS Code Version: 1.89.1 (system setup) PHP Intelephense Version 1.10.4

bmewburn commented 2 months ago

As a workaround you can wrap this block in <!-- @formatter:off --> and <!-- @formatter:on --> to disable. If the code is so fragile that a single whitespace casues a parse error then there is probably a better way to write it.