when I launch a sonar control I find myself with a smells code for the following function:
JavaScript
x
13
13
1
const changeMedia = (value: any, step: any) => {
2
step.definition?.steps
3
.filter((obj: any) => obj.media.includes(value))
4
.map((item: any) => {
5
if (value === 'EMAIL') {
6
return (item.media = 'LETTER');
7
} else if (value === 'LETTER') {
8
return (item.media = 'EMAIL');
9
}
10
return item;
11
});
12
};
13
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
JavaScript
1
14
14
1
const changeMedia = (value, step) => {
2
step.definition?.steps
3
.filter((obj) => obj.media.includes(value))
4
.map((item) => {
5
if (value === 'EMAIL') {
6
item.media = 'LETTER'
7
return item; // returning item instead of (item.media = 'LETTER')
8
} else if (value === 'LETTER') {
9
item.media = 'EMAIL'
10
return item;
11
}
12
return item;
13
});
14
};