Improved the tests and removed the hardcoded variable, according to suggestions.

This commit is contained in:
Bruno Bernardino
2015-08-16 21:27:28 +01:00
parent 24e8f1c98f
commit efdc67241a
4 changed files with 17 additions and 16 deletions

View File

@@ -2684,10 +2684,11 @@
};
Try.prototype.compileNode = function(o) {
var catchPart, ensurePart, generatedErrorVariableName, placeholder, tryPart;
var catchPart, ensurePart, generatedErrorVariableName, placeholder, scope, tryPart;
o.indent += TAB;
scope = o.scope;
tryPart = this.attempt.compileToFragments(o, LEVEL_TOP);
catchPart = this.recovery ? (!this.errorVariable || this.errorVariable.value !== '_error' ? generatedErrorVariableName = '_error' : generatedErrorVariableName = '__error', placeholder = new Literal(generatedErrorVariableName), this.errorVariable ? this.recovery.unshift(new Assign(this.errorVariable, placeholder)) : void 0, [].concat(this.makeCode(" catch ("), placeholder.compileToFragments(o), this.makeCode(") {\n"), this.recovery.compileToFragments(o, LEVEL_TOP), this.makeCode("\n" + this.tab + "}"))) : !(this.ensure || this.recovery) ? [this.makeCode(" catch (" + generatedErrorVariableName + ") {}")] : [];
catchPart = this.recovery ? (generatedErrorVariableName = scope.freeVariable('_error'), placeholder = new Literal(generatedErrorVariableName), this.errorVariable ? this.recovery.unshift(new Assign(this.errorVariable, placeholder)) : void 0, [].concat(this.makeCode(" catch ("), placeholder.compileToFragments(o), this.makeCode(") {\n"), this.recovery.compileToFragments(o, LEVEL_TOP), this.makeCode("\n" + this.tab + "}"))) : !(this.ensure || this.recovery) ? [this.makeCode(" catch (" + generatedErrorVariableName + ") {}")] : [];
ensurePart = this.ensure ? [].concat(this.makeCode(" finally {\n"), this.ensure.compileToFragments(o, LEVEL_TOP), this.makeCode("\n" + this.tab + "}")) : [];
return [].concat(this.makeCode(this.tab + "try {\n"), tryPart, this.makeCode("\n" + this.tab + "}"), catchPart, ensurePart);
};

View File

@@ -1892,14 +1892,11 @@ exports.Try = class Try extends Base
# is optional, the *catch* is not.
compileNode: (o) ->
o.indent += TAB
scope = o.scope
tryPart = @attempt.compileToFragments o, LEVEL_TOP
catchPart = if @recovery
# Prevent user-defined error variable clashing with the generated one
if not @errorVariable or @errorVariable.value isnt '_error'
generatedErrorVariableName = '_error'
else
generatedErrorVariableName = '__error'
generatedErrorVariableName = scope.freeVariable '_error'
placeholder = new Literal generatedErrorVariableName
@recovery.unshift new Assign @errorVariable, placeholder if @errorVariable
[].concat @makeCode(" catch ("), placeholder.compileToFragments(o), @makeCode(") {\n"),

View File

@@ -443,6 +443,18 @@ test "#1500: Assignment to variables similar to generated variables", ->
arrayEq [1, 2], f.call scope = {}, 1, 2
eq 1, scope.a
try throw 'foo'
catch _error
eq _error, 'foo'
eq _error, 'foo'
try throw 'bar'
catch __error
eq __error, 'bar'
eq __error, 'bar'
doesNotThrow -> CoffeeScript.compile '(@slice...) ->'
test "Assignment to variables similar to helper functions", ->

View File

@@ -33,15 +33,6 @@ test "catch statements should introduce their argument to scope", ->
do -> e = 5
eq 5, e
test "#4036 catch statements' error variable should not clash with user-defined variable", ->
compile = (code) -> CoffeeScript.compile(code, bare: yes).trim().replace(/\s+/g, " ")
eq "var _error; try { something(); } catch (__error) { _error = __error; console.log(_error); }", compile("try something()\ncatch _error\n console.log _error")
eq "var __error; try { something(); } catch (_error) { __error = _error; console.log(__error); }", compile("try something()\ncatch __error\n console.log __error")
eq "var error; try { something(); } catch (_error) { error = _error; console.log(error); }", compile("try something()\ncatch error\n console.log error")
test "loop variable should be accessible after for-of loop", ->
d = (x for x of {1:'a',2:'b'})
ok x in ['1','2']