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