Loops should not be infinite (javascript:S2189)

const getDatesBetweenDates = (startDate, endDate) => {
	let dates = [];
	const endDateObject = new Date(endDate);
	const iteratorDate = new Date(startDate);
    // Analyser complains about the next line
	while (iteratorDate < endDateObject) {
		dates = [...dates, new Date(iteratorDate)];
		iteratorDate.setDate(iteratorDate.getDate() + 1);
	dates = [...dates, new Date(endDate)];
	let sortedDates = dates.sort((a, b) => a - b);
	console.log('Dates between:', sortedDates);
	return sortedDates;

The analyser is complaining that iteratorDate and endDateObject are not modified in the while loop, but iteratorDate is modified.

Hey @pcolmer

As noted in the thread on how to report a false-positive, can you tell us what product you’re using and, if relevant, which version?

In any case, I think you are probably reporting this known false-positive which you can track on GitHub: FP on S2189 when loop condition variables are modified through function calls · Issue #2866 · SonarSource/SonarJS · GitHub

My apologies.

  • SonarQube Community Edition
  • Version 9.2.3 (build 50713)

Thanks for the link to the GitHub issue.