diff --git a/lib/nodes.js b/lib/nodes.js index 48ed026c..d0ee7a20 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -1462,7 +1462,7 @@ this.index = _b[1]; } this.pattern = this.name instanceof ValueNode; - if (this.index instanceof ObjectNode) { + if (this.index instanceof ValueNode) { throw new Error('index cannot be a pattern matching expression'); } this.children = compact([this.body, this.source, this.filter]); @@ -1520,13 +1520,12 @@ svar = scope.free_variable(); index_var = null; source_part = ("" + svar + " = " + (this.source.compile(o)) + ";\n" + this.tab); - if (name && !this.pattern) { - var_part = ("" + body_dent + name + " = " + svar + "[" + ivar + "];\n"); - } if (this.pattern) { o.indent = this.idt(1); o.top = true; var_part = new AssignNode(this.name, literal(("" + svar + "[" + ivar + "]"))).compile(o) + "\n"; + } else if (name) { + var_part = ("" + body_dent + name + " = " + svar + "[" + ivar + "];\n"); } if (!this.object) { lvar = scope.free_variable(); diff --git a/src/nodes.coffee b/src/nodes.coffee index 6ba3fccf..6a2a866c 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -1079,10 +1079,10 @@ exports.ForNode: class ForNode extends BaseNode @step: source.step @object: !!source.object [@name, @index]: [@index, @name] if @object - @pattern: @name instanceof ValueNode - throw new Error('index cannot be a pattern matching expression') if @index instanceof ObjectNode + @pattern: @name instanceof ValueNode + throw new Error('index cannot be a pattern matching expression') if @index instanceof ValueNode @children: compact [@body, @source, @filter] - @returns: false + @returns: false top_sensitive: -> true @@ -1122,11 +1122,12 @@ exports.ForNode: class ForNode extends BaseNode svar: scope.free_variable() index_var: null source_part: "$svar = ${ @source.compile(o) };\n$@tab" - var_part: "$body_dent$name = $svar[$ivar];\n" if name and not @pattern if @pattern o.indent: @idt 1 o.top: true var_part: new AssignNode(@name, literal("$svar[$ivar]")).compile(o) + "\n" + else + var_part: "$body_dent$name = $svar[$ivar];\n" if name if not @object lvar: scope.free_variable() step_part: if @step then "$ivar += ${ @step.compile(o) }" else "$ivar++"