From 0ada1dfc3f8562bd981e30f7aba702e3a4cb4052 Mon Sep 17 00:00:00 2001 From: satyr Date: Tue, 5 Oct 2010 12:03:13 +0900 Subject: [PATCH] follow-up to d1bca636; fixed unrolling of the single case (`{@a} = o` -> `@a = o.a`) --- lib/nodes.js | 10 +++------- src/nodes.coffee | 14 ++++++-------- test/test_pattern_matching.coffee | 2 ++ 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/lib/nodes.js b/lib/nodes.js index f723b414..446378dc 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -801,7 +801,7 @@ join = ''; } indent = prop instanceof CommentNode ? '' : this.idt(1); - if (prop instanceof ValueNode && prop.tags['this']) { + if (prop instanceof ValueNode && prop.tags["this"]) { prop = new AssignNode(prop.properties[0].name, prop, 'object'); } else if (!(prop instanceof AssignNode) && !(prop instanceof CommentNode)) { prop = new AssignNode(prop, prop, 'object'); @@ -1000,7 +1000,7 @@ if (obj instanceof AssignNode) { _ref2 = obj, idx = _ref2.variable.base, obj = _ref2.value; } else { - idx = isObject ? obj : literal(0); + idx = isObject ? (obj.tags["this"] ? obj.properties[0].name : obj) : literal(0); } if (!(value instanceof ValueNode)) { value = new ValueNode(value); @@ -1023,11 +1023,7 @@ if (obj instanceof AssignNode) { _ref2 = [obj.value, obj.variable.base], obj = _ref2[0], idx = _ref2[1]; } else { - if (obj.tags['this']) { - idx = obj.properties[0].name; - } else { - idx = obj; - } + idx = obj.tags["this"] ? obj.properties[0].name : obj; } } if (!(obj instanceof ValueNode || obj instanceof SplatNode)) { diff --git a/src/nodes.coffee b/src/nodes.coffee index bf9b0e0d..25c5b5a0 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -714,7 +714,7 @@ exports.ObjectNode = class ObjectNode extends BaseNode join = "\n" if (prop is lastNoncom) or (prop instanceof CommentNode) join = '' if i is @properties.length - 1 indent = if prop instanceof CommentNode then '' else @idt 1 - if prop instanceof ValueNode and prop.tags['this'] + if prop instanceof ValueNode and prop.tags.this prop = new AssignNode prop.properties[0].name, prop, 'object' else if prop not instanceof AssignNode and prop not instanceof CommentNode prop = new AssignNode prop, prop, 'object' @@ -886,7 +886,9 @@ exports.AssignNode = class AssignNode extends BaseNode if obj instanceof AssignNode {variable: {base: idx}, value: obj} = obj else - idx = if isObject then obj else literal 0 + idx = if isObject + if obj.tags.this then obj.properties[0].name else obj + else literal 0 value = new ValueNode value unless value instanceof ValueNode accessClass = if IDENTIFIER.test idx.value then AccessorNode else IndexNode value.properties.push new accessClass idx @@ -904,12 +906,8 @@ exports.AssignNode = class AssignNode extends BaseNode # A regular object pattern-match. [obj, idx] = [obj.value, obj.variable.base] else - if obj.tags['this'] - # A shorthand `{@a, @b, @c} = val` pattern-match. - idx = obj.properties[0].name - else - # A shorthand `{a, b, c} = val` pattern-match. - idx = obj + # A shorthand `{a, b, @c} = val` pattern-match. + idx = if obj.tags.this then obj.properties[0].name else obj unless obj instanceof ValueNode or obj instanceof SplatNode throw new Error 'pattern matching must use only identifiers on the left-hand side.' accessClass = if isObject and IDENTIFIER.test(idx.value) then AccessorNode else IndexNode diff --git a/test/test_pattern_matching.coffee b/test/test_pattern_matching.coffee index dbf20750..ce312738 100644 --- a/test/test_pattern_matching.coffee +++ b/test/test_pattern_matching.coffee @@ -149,6 +149,8 @@ obj = func: (list, object) -> [@one, @two] = list {@a, @b} = object + {@a} = object # must not unroll this + null obj.func [1, 2], a: 'a', b: 'b'