I have a project where ESLint throws this error from while loop. It does not make sence to me. Error says:
497:14 error 'from' is not modified in this loop no-unmodified-loop-condition
497:22 error 'to' is not modified in this loop no-unmodified-loop-condition
This is the code (look at while cycle):
mediaSettings: (state) => {
const timestamps = [];
const events = [];
state.media.forEach( medium => {
if( !medium.settings.dates ) return;
medium.settings.dates.forEach( dateRange => {
if( !dateRange.date_from || !dateRange.date_to ) return;
const from = new Date( dateRange.date_from );
const to = new Date( dateRange.date_to );
while ( from <= to ) {
if( timestamps.includes(from.getTime()) ) {
from.setDate( from.getDate() 1 );
continue;
}
events.push({
date: new Date( from.getTime() ), // Need Date clone via new Date().
mediumId: medium.id,
});
from.setDate( from.getDate() 1 );
};
});
});
return events;
}
What is that? Can somebody tell me plase how to fix it? It does not make sence. This is not an error.
CodePudding user response:
I found clever solution on this page which disables ESLint for some block of code and also a specific ESLint rules. It looks like:
mediaSettings: (state) => {
const timestamps = [];
const events = [];
state.media.forEach( medium => {
if( !medium.settings.dates ) return;
medium.settings.dates.forEach( dateRange => {
if( !dateRange.date_from || !dateRange.date_to ) return;
const from = new Date( dateRange.date_from );
const to = new Date( dateRange.date_to );
/* eslint-disable no-unmodified-loop-condition */
while ( from <= to ) {
if( timestamps.includes(from.getTime()) ) {
from.setDate( from.getDate() 1 );
continue;
}
events.push({
date: new Date( from.getTime() ), // Need Date clone via new Date().
mediumId: medium.id,
});
from.setDate( from.getDate() 1 );
};
/* eslint-enable no-unmodified-loop-condition */
});
});
return events;
}
CodePudding user response:
You wouldn't get this error if you are using reassignable variables like this:
...
let from = new Date(dateRange.date_from)
let to = new Date(dateRange.date_to)
while (from <= to) {
if (timestamps.includes(from.getTime())) {
from = from.setDate(from.getDate() 1)
continue
}
events.push({
date: new Date(from.getTime()), // Need Date clone via new Date().
mediumId: medium.id,
})
from = from.setDate(from.getDate() 1)
}
...