1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

fixing compound assignments to global variables. oof.

This commit is contained in:
Jeremy Ashkenas 2012-04-10 17:07:38 -04:00
parent de511e0348
commit f0e17fc20f
3 changed files with 10 additions and 2 deletions

View file

@ -1631,7 +1631,7 @@
Assign.prototype.compileConditional = function(o) { Assign.prototype.compileConditional = function(o) {
var left, right, _ref2; var left, right, _ref2;
_ref2 = this.variable.cacheReference(o), left = _ref2[0], right = _ref2[1]; _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."); 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) { if (__indexOf.call(this.context, "?") >= 0) {

View file

@ -1119,7 +1119,8 @@ exports.Assign = class Assign extends Base
compileConditional: (o) -> compileConditional: (o) ->
[left, right] = @variable.cacheReference o [left, right] = @variable.cacheReference o
# Disallow conditional assignment of undefined variables. # 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." 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 if "?" in @context then o.isExistentialEquals = true
new Op(@context[...-1], left, new Assign(right, @value, '=') ).compile o new Op(@context[...-1], left, new Assign(right, @value, '=') ).compile o

View file

@ -296,6 +296,13 @@ test "#1627: prohibit conditional assignment of undefined variables", ->
throws (-> CoffeeScript.compile "-> -> -> x ?= 10"), null, "prohibit (-> -> -> x ?= 10)" throws (-> CoffeeScript.compile "-> -> -> x ?= 10"), null, "prohibit (-> -> -> x ?= 10)"
doesNotThrow (-> CoffeeScript.compile "x = null; -> -> -> x ?= 10"), "allow (x = null; -> -> -> 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", -> test "#1348, #1216: existential assignment compilation", ->
nonce = {} nonce = {}