when I launch a sonar control I find myself with a smells code for the following function:
const changeMedia = (value: any, step: any) => { step.definition?.steps .filter((obj: any) => obj.media.includes(value)) .map((item: any) => { if (value === 'EMAIL') { return (item.media = 'LETTER'); } else if (value === 'LETTER') { return (item.media = 'EMAIL'); } return item; }); };
I get the following sonar alert:
Extract the assignment of “item.media” from this expression.
What would be the solution to avoid this sonar message?
Advertisement
Answer
const changeMedia = (value, step) => { step.definition?.steps .filter((obj) => obj.media.includes(value)) .map((item) => { if (value === 'EMAIL') { item.media = 'LETTER' return item; // returning item instead of (item.media = 'LETTER') } else if (value === 'LETTER') { item.media = 'EMAIL' return item; } return item; }); };