diff --git a/lib/coffee-script/nodes.js b/lib/coffee-script/nodes.js index 691f91cd..29d4bf80 100644 --- a/lib/coffee-script/nodes.js +++ b/lib/coffee-script/nodes.js @@ -1675,11 +1675,13 @@ return this; }; Try.prototype.compileNode = function(o) { - var catchPart, errorPart; + var catchPart, ensurePart, errorPart, tryPart; o.indent += TAB; errorPart = this.error ? " (" + (this.error.compile(o)) + ") " : ' '; - catchPart = this.recovery ? (o.scope.add(this.error.value, 'param'), " catch" + errorPart + "{\n" + (this.recovery.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}") : !(this.ensure || this.recovery) ? ' catch (_e) {}' : void 0; - return ("" + this.tab + "try {\n" + (this.attempt.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}" + (catchPart || '')) + (this.ensure ? " finally {\n" + (this.ensure.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}" : ''); + tryPart = this.attempt.compile(o, LEVEL_TOP); + catchPart = this.recovery ? (!o.scope.check(this.error.value) ? o.scope.add(this.error.value, 'param') : void 0, " catch" + errorPart + "{\n" + (this.recovery.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}") : !(this.ensure || this.recovery) ? ' catch (_error) {}' : void 0; + ensurePart = this.ensure ? " finally {\n" + (this.ensure.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}" : ''; + return "" + this.tab + "try {\n" + tryPart + "\n" + this.tab + "}" + (catchPart || '') + ensurePart; }; return Try; })(); diff --git a/src/nodes.coffee b/src/nodes.coffee index 4164609f..9d56e1fc 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -1425,16 +1425,19 @@ exports.Try = class Try extends Base compileNode: (o) -> o.indent += TAB errorPart = if @error then " (#{ @error.compile o }) " else ' ' + tryPart = @attempt.compile o, LEVEL_TOP + catchPart = if @recovery - o.scope.add @error.value, 'param' + o.scope.add @error.value, 'param' unless o.scope.check @error.value " catch#{errorPart}{\n#{ @recovery.compile o, LEVEL_TOP }\n#{@tab}}" else unless @ensure or @recovery - ' catch (_e) {}' - """ - #{@tab}try { - #{ @attempt.compile o, LEVEL_TOP } - #{@tab}}#{ catchPart or '' } - """ + if @ensure then " finally {\n#{ @ensure.compile o, LEVEL_TOP }\n#{@tab}}" else '' + ' catch (_error) {}' + + ensurePart = if @ensure then " finally {\n#{ @ensure.compile o, LEVEL_TOP }\n#{@tab}}" else '' + + """#{@tab}try { + #{tryPart} + #{@tab}}#{ catchPart or '' }#{ensurePart}""" #### Throw