FP on S2259 - Properties of variables with "null" or "undefined" values should not be accessed

SonarScanner 4.7.0.2747
Analyzing on SonarQube server 8.9.6

Using optional chaining with a function call (myVar?.myFunc()) can cause an error, even though this usage is actually fine.

import { useState } from "react";
import "./styles.css";

export default function App() {
  const [player, setPlayer] = useState();

  if (player?.getCurrentTime() > 0) { // Fail: TypeError can be thrown as "player" might be null or undefined here.
    console.log("player defined");
  } else {
    console.log("player undefined");
  }

  const onStateChange = (event) => {
    if (!player) {
      setPlayer(event.target);
    }
  };

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <div id="YouTube" onReady={onStateChange} onStateChange={onStateChange} />
    </div>
  );
}

CodeSandbox: great-raman-twy63i - CodeSandbox

Hey there.

Thanks for the report. In the case of this rule – optional chaining is supported (and this false-positive won’t be raised) in the very latest version of SonarQube (v9.6) but not in v8.9 LTS.