From ed8a54995d39552290cfe668ae676a3e2e7e4286 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Wed, 13 Jan 2010 22:25:58 -0500 Subject: [PATCH] with splats allowed in destructuring assignment --- lib/coffee_script/nodes.rb | 13 ++++++++++--- .../test_destructuring_assignment.coffee | 18 +++++++++++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/coffee_script/nodes.rb b/lib/coffee_script/nodes.rb index ddb52b84..234560da 100644 --- a/lib/coffee_script/nodes.rb +++ b/lib/coffee_script/nodes.rb @@ -482,9 +482,12 @@ module CoffeeScript @variable.base.objects.each_with_index do |obj, i| obj, i = obj.value, obj.variable.base if @variable.object? access_class = @variable.array? ? IndexNode : AccessorNode - assigns << AssignNode.new( - obj, ValueNode.new(Value.new(val_var), [access_class.new(Value.new(i.to_s))]) - ).compile(o) + if obj.is_a?(SplatNode) + val = LiteralNode.wrap(obj.compile_value(o, val_var, @variable.base.objects.index(obj))) + else + val = ValueNode.new(Value.new(val_var), [access_class.new(Value.new(i.to_s))]) + end + assigns << AssignNode.new(obj, val).compile(o) end write(assigns.join("\n")) end @@ -609,6 +612,10 @@ module CoffeeScript @name.compile(o) end + def compile_value(o, name, index) + "Array.prototype.slice.call(#{name}, #{index})" + end + end # An object literal. diff --git a/test/fixtures/execution/test_destructuring_assignment.coffee b/test/fixtures/execution/test_destructuring_assignment.coffee index 6998a700..aba854a8 100644 --- a/test/fixtures/execution/test_destructuring_assignment.coffee +++ b/test/fixtures/execution/test_destructuring_assignment.coffee @@ -43,4 +43,20 @@ person: { {name: a, family: {brother: {addresses: [one, {city: b}]}}}: person print(a is "Bob") -print(b is "Moquasset NY, 10021") \ No newline at end of file +print(b is "Moquasset NY, 10021") + + +test: { + person: { + address: [ + "------" + "Street 101" + "Apt 101" + "City 101" + ] + } +} + +{person: {address: [ignore, addr...]}}: test + +print(addr.join(', ') is "Street 101, Apt 101, City 101") \ No newline at end of file