From f0e17fc20fc2bf5603eb930ae1704f06a21ecce4 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Tue, 10 Apr 2012 17:07:38 -0400 Subject: [PATCH] fixing compound assignments to global variables. oof. --- lib/coffee-script/nodes.js | 2 +- src/nodes.coffee | 3 ++- test/assignment.coffee | 7 +++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/coffee-script/nodes.js b/lib/coffee-script/nodes.js index 88ea7982..40204246 100644 --- a/lib/coffee-script/nodes.js +++ b/lib/coffee-script/nodes.js @@ -1631,7 +1631,7 @@ Assign.prototype.compileConditional = function(o) { var left, right, _ref2; _ref2 = this.variable.cacheReference(o), left = _ref2[0], right = _ref2[1]; - if (left.base instanceof Literal && left.base.value !== "this" && !o.scope.check(left.base.value)) { + if (!left.properties.length && left.base instanceof Literal && left.base.value !== "this" && !o.scope.check(left.base.value)) { throw new Error("the variable \"" + left.base.value + "\" can't be assigned with " + this.context + " because it has not been defined."); } if (__indexOf.call(this.context, "?") >= 0) { diff --git a/src/nodes.coffee b/src/nodes.coffee index ec683d44..c30db3d4 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -1119,7 +1119,8 @@ exports.Assign = class Assign extends Base compileConditional: (o) -> [left, right] = @variable.cacheReference o # Disallow conditional assignment of undefined variables. - if left.base instanceof Literal and left.base.value != "this" and not o.scope.check left.base.value + if not left.properties.length and left.base instanceof Literal and + left.base.value != "this" and not o.scope.check left.base.value throw new Error "the variable \"#{left.base.value}\" can't be assigned with #{@context} because it has not been defined." if "?" in @context then o.isExistentialEquals = true new Op(@context[...-1], left, new Assign(right, @value, '=') ).compile o diff --git a/test/assignment.coffee b/test/assignment.coffee index dd4bbb0c..8426dc21 100644 --- a/test/assignment.coffee +++ b/test/assignment.coffee @@ -296,6 +296,13 @@ test "#1627: prohibit conditional assignment of undefined variables", -> throws (-> CoffeeScript.compile "-> -> -> x ?= 10"), null, "prohibit (-> -> -> x ?= 10)" doesNotThrow (-> CoffeeScript.compile "x = null; -> -> -> x ?= 10"), "allow (x = null; -> -> -> x ?= 10)" + +test "more existential assignment", -> + global.temp ?= 0 + eq global.temp, 0 + global.temp or= 100 + eq global.temp, 100 + delete global.temp test "#1348, #1216: existential assignment compilation", -> nonce = {}