Jul 23, 2015
Update 7-24-15:
I just found out this was a bug that was fixed nine days ago. Check it out, and update your eslint!
Consider:
function elseAfterReturn(someCondition, otherCondition){
if(someCondition){
console.log('if');
} else if (otherCondition){
console.log('else if');
return;
} else{
// eslint complains no else after return
console.log('else');
}
// do something after if or else, but not else if
return;
}
function noElseAfterReturn(someCondition, otherCondition){
if(someCondition){
console.log('if');
} else if (otherCondition){
console.log('else if');
return;
}
console.log('else');
// do something after if or the supposedly implicit else, but not else if
return;
}
console.log('else after return');
elseAfterReturn(true, false);
console.log();
console.log('no else after return');
noElseAfterReturn(true, false);
The output from the two functions is not the same, but don’t take my word for it.
I modified the script slightly to print to this code block instead of the console
eslint might lead you to believe that the two functions above are identical, but they clearly are not.
Obviously the above is very contrived, but this actually bit me while using expressjs. I had a situation where I accidentally called next()
more than once in some cases which resulted in erratic and difficult to track down errors.
This probably could be solved with better coding practices, but I think it’s worth realizing that no-else-return
can introduce unexpected errors.