-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
switch generates unneeded breaks #5344
Comments
Add new `alwaysJumps` node method to detect when a switch case can safely drop its `break`. Similar to `jumps` but ANDing instead of OR.
Add new `alwaysJumps` node method to detect when a switch case can safely drop its `break`. Similar to `jumps` but ANDing instead of OR.
I'd assume that when using That being said, it's interesting if that isn't too hard to achieve. I just took a quick look at the associated PR. My instinct is it wouldn't be worth introducing the additional logic just for the sake of what could be considered an aesthetic improvement to the transpiled JS |
Indeed, now that I have |
I mean, it would be nice to not output unnecessary |
I agree. Looking at the PR again, it's not much added complexity, but the only real improvement is nicer looking code, which probably isn't worthwhile (unless there's another reason we want to detect unreachable code). I originally wrote this issue before I knew of #5348 does have some improvements to comments which I'll extract into another PR. |
Part of CoffeeScript’s pitch on our improvements to Aside from #5366, there’s only one area that I’d consider investing time in improving our JS output, and that’s to stop outputting |
When you say "block-scoped This seems subtle. For example, the following code behaves differently if for i in [1..2]
x = 5 unless x?
console.log x++ (Outside, it prints And it's not enough to just pull it out of the containing loop. You need to pull it out of all loops. For example: for j in [1..2]
for i in [1..2]
x = 5 unless x?
console.log x++ This should print What's a good example where ... code not using y ...
y = 7 ...it might be nicer to write Distinguishing |
Yes, basically. Although the key here is to not cause any breaking changes. So when in doubt, keep the current “define at the top of the function scope” behavior. A declaration/first assignment within a loop is a great example. In this case, a If you look at this example: if new Date().getHours() < 9
breakfast = if new Date().getDay() is 6 then 'donuts' else 'coffee'
alert "Time to make the #{breakfast}!"
else
alert 'Time to get some work done.'
if (new Date().getHours() < 9) {
let breakfast;
breakfast = new Date().getDay() === 6 ? 'donuts' : 'coffee';
alert(`Time to make the ${breakfast}!`);
} else {
alert('Time to get some work done.');
} And then phase 2 would see that if (new Date().getHours() < 9) {
const breakfast = new Date().getDay() === 6 ? 'donuts' : 'coffee';
alert(`Time to make the ${breakfast}!`);
} else {
alert('Time to get some work done.');
} |
This would certainly be helpful! Having variable declarations as far down block-wise as backwards compatibility allows could, by itself, already greatly aid in type checking. fyi my lsp extension even already partly tries to apply phase 1 where possible and it helps detecting unexpected variable reassignments. |
Discussion moved to #5377. Thanks @GeoffreyBooth -- I think this is a cool thing to pursue. |
This is a feature request that surfaces as a bug when trying to use eslint with standard rules, specifically the
no-unreachable
rule.Input Code
try link
Expected Behavior
Current Behavior
Currently, CoffeeScript generates dead/inaccessible
break
in the first case (because of theif
expression) but it's smart enough not to in the second case.Possible Solution
Ideally, the existing mechanism for not generating
break
lines inswitch
statements can apply to complex implicit return expressions (such asif
expressions), not just simple expressions.Context
One could argue that the extra
break
statements are fine to leave in. Unfortunately, this 2016 eslint issue disagrees with this argument, so there's no easy way to allow thesebreak
statements ineslint
without allowing all dead code (i.e. disabling theno-unreachable
).Environment
Live demo on try website
The text was updated successfully, but these errors were encountered: