@@ -19,12 +19,6 @@ test "unassignable values", ->
1919 for nonref in [' ' , ' ""' , ' 0' , ' f()' ].concat CoffeeScript .RESERVED
2020 eq nonce, (try CoffeeScript .compile " #{ nonref} = v" catch e then nonce)
2121
22- test " compound assignments should not declare" , ->
23- # TODO: make description more clear
24- # TODO: remove reference to Math
25- eq Math , (-> Math or= 0 )()
26-
27-
2822# Compound Assignment
2923
3024test " boolean operators" , ->
@@ -285,8 +279,23 @@ test "existential assignment", ->
285279 c = null
286280 c ?= nonce
287281 eq nonce, c
288- d ?= nonce
289- eq nonce, d
282+
283+ test " #1627: prohibit conditional assignment of undefined variables" , ->
284+ throws (-> CoffeeScript .compile " x ?= 10" ), null , " prohibit (x ?= 10)"
285+ throws (-> CoffeeScript .compile " x ||= 10" ), null , " prohibit (x ||= 10)"
286+ throws (-> CoffeeScript .compile " x or= 10" ), null , " prohibit (x or= 10)"
287+ throws (-> CoffeeScript .compile " do -> x ?= 10" ), null , " prohibit (do -> x ?= 10)"
288+ throws (-> CoffeeScript .compile " do -> x ||= 10" ), null , " prohibit (do -> x ||= 10)"
289+ throws (-> CoffeeScript .compile " do -> x or= 10" ), null , " prohibit (do -> x or= 10)"
290+ doesNotThrow (-> CoffeeScript .compile " x = null; x ?= 10" ), " allow (x = null; x ?= 10)"
291+ doesNotThrow (-> CoffeeScript .compile " x = null; x ||= 10" ), " allow (x = null; x ||= 10)"
292+ doesNotThrow (-> CoffeeScript .compile " x = null; x or= 10" ), " allow (x = null; x or= 10)"
293+ doesNotThrow (-> CoffeeScript .compile " x = null; do -> x ?= 10" ), " allow (x = null; do -> x ?= 10)"
294+ doesNotThrow (-> CoffeeScript .compile " x = null; do -> x ||= 10" ), " allow (x = null; do -> x ||= 10)"
295+ doesNotThrow (-> CoffeeScript .compile " x = null; do -> x or= 10" ), " allow (x = null; do -> x or= 10)"
296+
297+ throws (-> CoffeeScript .compile " -> -> -> x ?= 10" ), null , " prohibit (-> -> -> x ?= 10)"
298+ doesNotThrow (-> CoffeeScript .compile " x = null; -> -> -> x ?= 10" ), " allow (x = null; -> -> -> x ?= 10)"
290299
291300test " #1348, #1216: existential assignment compilation" , ->
292301 nonce = {}
@@ -295,14 +304,7 @@ test "#1348, #1216: existential assignment compilation", ->
295304 eq nonce, b
296305 # the first ?= compiles into a statement; the second ?= compiles to a ternary expression
297306 eq a ?= b ?= 1 , nonce
298-
299- e ?= f ?= g ?= 1
300- eq e + g, 2
301-
302- # need to ensure the two vars are not defined, hence the strange names;
303- # broke earlier when using c ?= d ?= 1 because `d` is declared elsewhere
304- eq und1_1348 ?= und2_1348 ?= 1 , 1
305-
307+
306308 if a then a ?= 2 else a = 3
307309 eq a, nonce
308310
0 commit comments