SonarQube Version 8.9.6
Affected Rule: S4325
Library used for scraping Puppeteer,
The issue occurs in the following methods page.evaluate, page.$eval, page.$$eval (these methods are responsible to perform scraping with the help of browsers console)
I am stuck with the above mentioned rule, If I remove the assertion using the SonarQube Quickfix Typescript throws the following errors:
Property ‘value’ does not exist on type ‘Element’.
Property ‘innerText’ does not exist on type ‘Element’.
and so on …
Please find below the couple of Examples, which I am trying to resolve:
const number = (<HTMLElement>row.querySelector('td:nth-child(1)')).innerHTML;
let someVariable= (<HTMLElement>node.querySelector('#some-ID'))?.innerText;
let GENERATOR = (<HTMLInputElement>htmlDoc.querySelector('input[name="__GENERATOR"]')).value;
I have tried a workaround where I create a new Variable and then typeCast it, Example as follows
let GENERATOR_DOC= (htmlDoc.querySelector('input[name="__GENERATOR"]'));
let GENERATOR = (GENERATOR_DOC as HTMLInputElement).value;
I want to understand why Sonarqube does not gives the unnecessary assertion In case of new variable creation and typecasting it ?
As well is there any better approach to resolve this issue ?
Also would like to understand how did sonarqube Identified that there is an unnecessary assertion for the give examples ?