vscode-intelephense icon indicating copy to clipboard operation
vscode-intelephense copied to clipboard

`',' expected` in javascript embedded in PHP

Open leonardolara opened this issue 11 months ago • 3 comments

Describe the bug Intelephense's javascript analyzer is showing a ',' expected in opening brackets after PHP code in line 8 of the code below.

To Reproduce

<?php
header('Content-Type:text/html; charset=UTF-8');
$fruits = [1 => 'apples', 2 => 'oranges', 3 => 'lemons'];
?>
<script>
var x = [
    <?php foreach ($fruits as $key => $name) { ?>
    {
        "key": "<?php echo $key ?>",
        "name": "<?php echo $name ?>"
    },
    <?php } ?>
];
</script>

Expected behavior There should be no syntax error detected in this code at line 8.

Screenshots N/A

Platform and version Windows 10, Intelephense v1.10.4.

leonardolara avatar Mar 28 '24 12:03 leonardolara

I can't reproduce this with the given code. Could it be coming from a different extension?

bmewburn avatar Mar 30 '24 04:03 bmewburn

I tried to isolate the extension by disabling/enabling each one. The syntax highlight was gone when I disabled intelephense.

Em sáb., 30 de mar. de 2024, 01:01, Ben Mewburn @.***> escreveu:

I can't reproduce this with the given code. Could it be coming from a different extension?

— Reply to this email directly, view it on GitHub https://github.com/bmewburn/vscode-intelephense/issues/2834#issuecomment-2027904400, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC7PC3EJJIH46BVPVGMZOBLY2Y2H3AVCNFSM6AAAAABFMXLDNWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMRXHEYDINBQGA . You are receiving this because you authored the thread.Message ID: @.***>

leonardolara avatar Mar 30 '24 11:03 leonardolara

I'm also experiencing this intermittently. Sometimes the ',' expected error comes up on file save. Sometimes it goes away and will return if modifying surrounding lines.

Example of what reproduces the error (PHP conditional within JS object literal inside of an anonymous JS callback function argument passed to a named JS function):

<?php
function named_function_1($arg1) {
  // do some
  // php stuff
  ?> <!-- stepping out to output html/javascript -->
  <script>
    callingNamedJsFunction('some js arg', function() {
      const element = document.querySelector('.some-selector');
      element.obj = {
        prop1: element.querySelector('#p-1'),
        prop2: element.querySelector('#p-2'),
        prop3: element.querySelector('#p-3'),
        <?php if ($arg1): ?>
        prop4: element.querySelector('#p-4'),
        prop5: element.querySelector('#p-5'),
        <?php endif; ?>
      };
      // do more
      // js stuff
    });
  </script>
  <?php
  // do more
  // php stuff
};
?>
image

Putting the same PHP conditional outside of the JS object literal does not reproduce the error:

<?php
function named_function_2($arg1) {
  // do some
  // php stuff
  ?> <!-- stepping out to output html/javascript -->
  <script>
    callingNamedJsFunction('some js arg', function() {
      const element = document.querySelector('.some-selector');
      element.obj = {
        prop1: element.querySelector('#p-1'),
        prop2: element.querySelector('#p-2'),
        prop3: element.querySelector('#p-3'),
      };

      <?php if ($arg1): ?>
      element.obj.prop4 = element.querySelector('#p-4');
      element.obj.prop5 = element.querySelector('#p-5');
      <?php endif; ?>
      // do more
      // js stuff
    });
  </script>
  <?php
  // do more
  // php stuff
};
?>

The error goes away when disabling Intelephense and reloading extensions. It returns when enabling Intelephense.

999j avatar Sep 26 '24 22:09 999j