From 7a16db9ad33af024d0e08d146b770c9c78fdcec8 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sat, 10 Jul 2010 09:01:22 -0400 Subject: [PATCH] fixing inline-loop object-in-array tests for instance variables. Issue #481 --- lib/nodes.js | 2 +- src/nodes.coffee | 6 +++--- test/test_operations.coffee | 8 ++++++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/nodes.js b/lib/nodes.js index 12b51fc0..d77d27b4 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -1218,7 +1218,7 @@ i = _c[0]; l = _c[1]; prefix = this.obj1 !== this.obj2 ? this.obj1 + '; ' : ''; - return "!!(function(){ " + (prefix) + "for (var " + i + "=0, " + l + "=" + (this.arr1) + ".length; " + i + "<" + l + "; " + i + "++) if (" + (this.arr2) + "[" + i + "] === " + this.obj2 + ") return true; })()"; + return "!!(function(){ " + (prefix) + "for (var " + i + "=0, " + l + "=" + (this.arr1) + ".length; " + i + "<" + l + "; " + i + "++) if (" + (this.arr2) + "[" + i + "] === " + this.obj2 + ") return true; }).call(this)"; }; return InNode; })(); diff --git a/src/nodes.coffee b/src/nodes.coffee index 894526d0..19a843b8 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -136,9 +136,9 @@ exports.BaseNode: class BaseNode class: 'BaseNode' children: [] - unwrap: -> this + unwrap: -> this isStatement: -> no - isPureStatement: -> no + isPureStatement: -> no topSensitive: -> no #### Expressions @@ -1079,7 +1079,7 @@ exports.InNode: class InNode extends BaseNode [@arr1, @arr2]: @array.compileReference o, {precompile: yes} [i, l]: [o.scope.freeVariable(), o.scope.freeVariable()] prefix: if @obj1 isnt @obj2 then @obj1 + '; ' else '' - "!!(function(){ ${prefix}for (var $i=0, $l=${@arr1}.length; $i<$l; $i++) if (${@arr2}[$i] === $@obj2) return true; })()" + "!!(function(){ ${prefix}for (var $i=0, $l=${@arr1}.length; $i<$l; $i++) if (${@arr2}[$i] === $@obj2) return true; }).call(this)" #### TryNode diff --git a/test/test_operations.coffee b/test/test_operations.coffee index 839743e6..6ed88a04 100644 --- a/test/test_operations.coffee +++ b/test/test_operations.coffee @@ -51,6 +51,14 @@ list: [1, 2, 7] result: if list[2] in [7, 10] then 100 else -1 ok result is 100 +# And with array presence on an instance variable. +obj: { + list: [1, 2, 3, 4, 5] + in_list: (value) -> value in @list +} +ok obj.in_list 4 +ok not obj.in_list 0 + # Non-spaced values still work. x: 10 y: -5