PHPCSStandards / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
805 stars 47 forks source link

False positive OperatorSpacing for JS function chaining #495

Closed spl1nes closed 2 months ago

spl1nes commented 2 months ago

Describe the bug

This is not an important issue for me since it's just a little extra thing, but I do sometimes use this CodeSniffer for JS as well.

In JS (?.) we can do function chaining with null checks, just like in php (?->). When I use Squiz.WhiteSpace.OperatorSpacing in php the sniffer correctly recognizes that ?-> is "one" operator and doesn't expect white spaces around ?. For consistency it would be good if it would also recognize ?. as "one" operator in JS and NOT demand a white space around ?.

Code sample

const adventurer = {
    name: 'Alice',
    cat: {
        name: 'Dinah',
    },
  };

  const dogName = adventurer.dog?.name;
  console.log(dogName);
  // Expected output: undefined

  console.log(adventurer.someNonExistentMethod?.());
  // Expected output: undefined

Custom ruleset

<?xml version="1.0"?>
<ruleset name="My Custom Standard">
<rule ref="Squiz.WhiteSpace.OperatorSpacing">
    <properties>
        <property name="ignoreNewlines" value="true"/>
    </properties>
</rule>
</ruleset>

To reproduce

--------------------------------------------------------------------------------------------------------
FOUND 4 ERRORS AFFECTING 2 LINES
--------------------------------------------------------------------------------------------------------
  8 | ERROR | [x] Expected 1 space before "?"; 0 found (Squiz.WhiteSpace.OperatorSpacing.NoSpaceBefore)
  8 | ERROR | [x] Expected 1 space after "?"; 0 found (Squiz.WhiteSpace.OperatorSpacing.NoSpaceAfter)
 12 | ERROR | [x] Expected 1 space before "?"; 0 found (Squiz.WhiteSpace.OperatorSpacing.NoSpaceBefore)
 12 | ERROR | [x] Expected 1 space after "?"; 0 found (Squiz.WhiteSpace.OperatorSpacing.NoSpaceAfter)
--------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------------------------------

Expected behavior

No errors.

Versions (please complete the following information)

Operating System linux
PHP version 8.2
PHP_CodeSniffer version 3.9.2
Standard Squiz
Install type Composer local

Please confirm

jrfnl commented 2 months ago

@spl1nes Thanks for reporting this. Javascript support will be removed in PHPCS 4.0, so this is unlikely to get fixed. There are other, JS specific, code styling tools available nowadays. I suggest you use those.

If a small fix for this issue could/would be submitted, I might accept it, but extensive changes to support this in the JS Tokenizer will not be accepted anymore.

spl1nes commented 2 months ago

Oh, nvm then. Thanks for the info, I didn't know about the dropped support for JS.