diff --git a/lib/nodes.js b/lib/nodes.js index b71d8393..d8037edd 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -756,7 +756,7 @@ // object literals to a value. Peeks at their properties to assign inner names. // See: http://wiki.ecmascript.org/doku.php?id=harmony:destructuring compile_pattern_match: function compile_pattern_match(o) { - var _a, _b, access_class, assigns, i, idx, obj, val, val_var; + var _a, _b, access_class, assigns, code, i, idx, obj, val, val_var; val_var = o.scope.free_variable(); assigns = [this.idt() + val_var + ' = ' + this.value.compile(o) + ';']; o.top = true; @@ -781,7 +781,11 @@ } assigns.push(new AssignNode(obj, val).compile(o)); } - return assigns.join("\n"); + code = assigns.join("\n"); + if (o.returns) { + code += '\n' + this.idt() + 'return ' + this.variable.compile(o) + ';'; + } + return code; }, compile_splice: function compile_splice(o) { var from, l, name, plus, range, to; diff --git a/src/nodes.coffee b/src/nodes.coffee index 91317694..3df8edc8 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -617,7 +617,9 @@ AssignNode: exports.AssignNode: inherit BaseNode, { idx: new LiteralNode(idx) unless typeof idx is 'object' val: new ValueNode(new LiteralNode(val_var), [new access_class(idx)]) assigns.push(new AssignNode(obj, val).compile(o)) - assigns.join("\n") + code: assigns.join("\n") + code += '\n' + @idt() + 'return ' + @variable.compile(o) + ';' if o.returns + code compile_splice: (o) -> name: @variable.compile(merge(o, {only_first: true})) diff --git a/test/test_destructuring_assignment.coffee b/test/test_destructuring_assignment.coffee index 0fafeba5..c32ea15f 100644 --- a/test/test_destructuring_assignment.coffee +++ b/test/test_destructuring_assignment.coffee @@ -7,6 +7,12 @@ ok a is -2 ok b is -1 +func: -> + [a, b]: [b, a] + +ok func().join(' ') is '-1 -2' + + arr: [1, 2, 3] [a, b, c]: arr