Skip to content

Commit

Permalink
fixes #1005: invalid identifiers allowed on LHS of destructuring
Browse files Browse the repository at this point in the history
assignment
  • Loading branch information
michaelficarra committed Aug 11, 2011
1 parent e5b77b1 commit df5aca9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
19 changes: 14 additions & 5 deletions lib/coffee-script/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# the syntax tree into a string of JavaScript code, call `compile()` on the root.

{Scope} = require './scope'
{RESERVED} = require './lexer'

# Import the helpers we plan to use.
{compact, flatten, extend, merge, del, starts, ends, last} = require './helpers'
Expand Down Expand Up @@ -978,6 +979,8 @@ exports.Assign = class Assign extends Base
acc = IDENTIFIER.test idx.unwrap().value or 0
value = new Value value
value.properties.push new (if acc then Access else Index) idx
if obj.unwrap().value in ['arguments','eval'].concat RESERVED
throw new SyntaxError "assignment to a reserved word: #{obj.compile o} = #{value.compile o}"
return new Assign(obj, value, null, param: @param).compile o, LEVEL_TOP
vvar = value.compile o, LEVEL_LIST
assigns = []
Expand All @@ -999,6 +1002,7 @@ exports.Assign = class Assign extends Base
else
idx = if obj.this then obj.properties[0].name else obj
if not splat and obj instanceof Splat
name = obj.name.unwrap().value
val = "#{olen} <= #{vvar}.length ? #{ utility 'slice' }.call(#{vvar}, #{i}"
if rest = olen - i - 1
ivar = o.scope.freeVariable 'i'
Expand All @@ -1008,16 +1012,19 @@ exports.Assign = class Assign extends Base
val = new Literal val
splat = "#{ivar}++"
else
name = obj.unwrap().value
if obj instanceof Splat
obj = obj.name.compile o
throw SyntaxError \
"multiple splats are disallowed in an assignment: #{obj} ..."
throw new SyntaxError \
"multiple splats are disallowed in an assignment: #{obj}..."
if typeof idx is 'number'
idx = new Literal splat or idx
acc = no
else
acc = isObject and IDENTIFIER.test idx.unwrap().value or 0
val = new Value new Literal(vvar), [new (if acc then Access else Index) idx]
if name? and name in ['arguments','eval'].concat RESERVED
throw new SyntaxError "assignment to a reserved word: #{obj.compile o} = #{val.compile o}"
assigns.push new Assign(obj, val, null, param: @param).compile o, LEVEL_TOP
assigns.push vvar unless top
code = assigns.join ', '
Expand Down
14 changes: 14 additions & 0 deletions test/assignment.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,20 @@ test "destructuring assignment with context (@) properties", ->
test "#1024", ->
eq 2 * [] = 3 + 5, 16

test "#1005: invalid identifiers allowed on LHS of destructuring assignment", ->
disallowed = ['eval', 'arguments'].concat CoffeeScript.RESERVED
throws -> CoffeeScript.compile "[#{disallowed.join ', '}] = x"
throws -> CoffeeScript.compile "[#{disallowed.join '..., '}...] = x"
for v in disallowed when v isnt 'class' # `class` by itself is an expression
throws -> CoffeeScript.compile "[#{v}] = x"
throws -> CoffeeScript.compile "[#{v}...] = x"
doesNotThrow ->
for v in disallowed
CoffeeScript.compile "[a.#{v}] = x"
CoffeeScript.compile "[a.#{v}...] = x"
CoffeeScript.compile "[@#{v}] = x"
CoffeeScript.compile "[@#{v}...] = x"


# Existential Assignment

Expand Down

0 comments on commit df5aca9

Please sign in to comment.