Vscode-intelephense: Bad formatting inside mixed JS/PHP script block

Created on 28 Jun 2019  路  6Comments  路  Source: bmewburn/vscode-intelephense

The following valid PHP code showcases a couple of issues I've seen with the formatter:

<html>
  <body>
    <script>
      var test1 = '<?= $_SERVER['PHP_SELF'] ?>';
      var test2 = "<?= true ? htmlentities('"""') : ''?>";
      var test3 = <?= time() ?>;
    </script>
  </body>
</html>

When formatting this code, it results in the following screwed up code:

<html>

<body>
    <script>
        var test1 = '<?= $_SERVER['
        PHP_SELF '] ?>';
        var test2 = "<?= true ? htmlentities('"
        ""
        ') : '
        ' ?>";
        var test3 = < ? = time() ? > ;
    </script>
</body>

</html>
bug

Most helpful comment

Actually it's not completely fixed, here is another test case which goes wrong:

<html><body><script>
var test = {
    <?= $condition ? 'item1' : 'item2' ?>: <?= $data ?>
};
</script></body></html>

Output after formatting:

<html>

<body>
    <script>
        var test = {
            <
            ? php ? > : <?= $condition ? 'item1' : 'item2' ?>
        };
    </script>
</body>

</html>

All 6 comments

Thanks for fixing this!

Actually it's not completely fixed, here is another test case which goes wrong:

<html><body><script>
var test = {
    <?= $condition ? 'item1' : 'item2' ?>: <?= $data ?>
};
</script></body></html>

Output after formatting:

<html>

<body>
    <script>
        var test = {
            <
            ? php ? > : <?= $condition ? 'item1' : 'item2' ?>
        };
    </script>
</body>

</html>

Thanks, I didn't spot any more formatting issues!

image
version 1.1.4

@saurfang587 please post code, not screenshots. ~I cant reproduce this~. When I format a similar snippet I get the following.
Edit: looks like the php tag at the top is what prevents it from formatting

Before format:

<script>
<?php $this->foo() ?> //this breaks below formatting
function
_test(myVar)
{
$('.selector').picker({
"a":
true,
"b":
myVar        
});
}

</script>

After format:

<script>
function
_test(myVar) {
    $('.selector').picker({
        "a": true,
        "b": myVar
    });
}
</script>

@saurfang587 this is upstream - https://github.com/beautify-web/js-beautify/issues/1687

Place a comment before the first php tag as a workaround.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aleksandervines picture aleksandervines  路  3Comments

steven7mwesigwa picture steven7mwesigwa  路  4Comments

zlianon picture zlianon  路  3Comments

mushmelty picture mushmelty  路  4Comments

dgunay picture dgunay  路  3Comments