coffeescript icon indicating copy to clipboard operation
coffeescript copied to clipboard

`catch` block introduces extra var

Open STRd6 opened this issue 2 years ago • 2 comments

try
catch e

Actual:

// Generated by CoffeeScript 2.6.1
var e;

try {

} catch (error) {
  e = error;
}

Expected:

// Generated by CoffeeScript 2.6.1
try {
} catch (e) {
}

I understand that in cases where the catch variable is referenced outside of the catch block it must be var scoped as all variables in CoffeeScript are but in cases where the variable is not referenced at all outside of the block I don't feel that it should be hoisted.

STRd6 avatar Apr 22 '22 23:04 STRd6

You might want to dig a little to find where this behavior was added, and why. There might be something particular about catch blocks that led to this, as it’s not like the actual behavior was easier to implement than the expected.

GeoffreyBooth avatar Apr 22 '22 23:04 GeoffreyBooth

I expect that it is to handle cases where the variable is used later outside of the catch block. The issue is that it is also added in cases where the variable is unused outside of the block.

STRd6 avatar Apr 23 '22 00:04 STRd6