Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1c8411f

Browse files
committed
merging in early error for compound assignment to undeclared variables.
2 parents de99704 + 3484ca5 commit 1c8411f

3 files changed

Lines changed: 29 additions & 21 deletions

File tree

lib/coffee-script/nodes.js

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nodes.coffee

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,9 +1117,12 @@ exports.Assign = class Assign extends Base
11171117
# operands are only evaluated once, even though we have to reference them
11181118
# more than once.
11191119
compileConditional: (o) ->
1120-
[left, rite] = @variable.cacheReference o
1120+
[left, right] = @variable.cacheReference o
1121+
# Disallow conditional assignment of undefined variables.
1122+
if left.base instanceof Literal and left.base.value != "this" and not o.scope.check left.base.value
1123+
throw new Error "the variable \"#{left.base.value}\" can't be assigned with #{@context} because it has not been defined."
11211124
if "?" in @context then o.isExistentialEquals = true
1122-
new Op(@context[...-1], left, new Assign(rite, @value, '=') ).compile o
1125+
new Op(@context[...-1], left, new Assign(right, @value, '=') ).compile o
11231126

11241127
# Compile the assignment from an array splice literal, using JavaScript's
11251128
# `Array#splice` method.

test/assignment.coffee

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3024
test "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

291300
test "#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

Comments
 (0)