From 2f7c076a503ca00c19a0db599512010a89dde951 Mon Sep 17 00:00:00 2001 From: satyr Date: Thu, 21 Oct 2010 22:13:39 +0900 Subject: [PATCH] fixed a bug where `in` malfunctioned in commaed lists --- lib/lexer.js | 2 +- lib/nodes.js | 13 ++++--------- lib/rewriter.js | 8 ++++---- src/nodes.coffee | 8 +++----- test/test_operations.coffee | 5 ++++- 5 files changed, 16 insertions(+), 20 deletions(-) diff --git a/lib/lexer.js b/lib/lexer.js index 274548e2..f7886a85 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -192,7 +192,7 @@ if (match = HEREGEX.exec(this.chunk)) { return this.heregexToken(match); } - if (_ref2 = this.tag(), __indexOf.call(NOT_REGEX, _ref2) >= 0) { + if ((_ref2 = this.tag(), __indexOf.call(NOT_REGEX, _ref2) >= 0)) { return false; } if (!(match = REGEX.exec(this.chunk))) { diff --git a/lib/nodes.js b/lib/nodes.js index 2919285e..d78d0d44 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -993,7 +993,7 @@ delete o.top; return ifn.compile(o); } - if (_ref2 = this.context, __indexOf.call(this.CONDITIONAL, _ref2) >= 0) { + if ((_ref2 = this.context, __indexOf.call(this.CONDITIONAL, _ref2) >= 0)) { return this.compileConditional(o); } } @@ -1389,7 +1389,7 @@ }; Op.prototype.isChainable = function() { var _ref2; - return _ref2 = this.operator, __indexOf.call(this.CHAINABLE, _ref2) >= 0; + return (_ref2 = this.operator, __indexOf.call(this.CHAINABLE, _ref2) >= 0); }; Op.prototype.invert = function() { var op; @@ -1486,13 +1486,8 @@ }), { precompile: true }), sub = _ref2[0], ref = _ref2[1]; - code = utility('indexOf') + (".call(" + (this.array.compile(o)) + ", " + ref + ") "); - code += this.negated ? '< 0' : '>= 0'; - if (sub === ref) { - return code; - } - code = sub + ', ' + code; - return this.parenthetical ? code : "(" + code + ")"; + code = utility('indexOf') + (".call(" + (this.array.compile(o)) + ", " + ref + ") ") + (this.negated ? '< 0' : '>= 0'); + return sub === ref ? code : "(" + sub + ", " + code + ")"; }; In.prototype.toString = function(idt) { return In.__super__.toString.call(this, idt, this.constructor.name + (this.negated ? '!' : '')); diff --git a/lib/rewriter.js b/lib/rewriter.js index 512f2e45..07560273 100644 --- a/lib/rewriter.js +++ b/lib/rewriter.js @@ -45,9 +45,9 @@ if (!token || levels < 0) { return action.call(this, token, i - 1); } - if (_ref = token[0], __indexOf.call(EXPRESSION_START, _ref) >= 0) { + if ((_ref = token[0], __indexOf.call(EXPRESSION_START, _ref) >= 0)) { levels += 1; - } else if (_ref2 = token[0], __indexOf.call(EXPRESSION_END, _ref2) >= 0) { + } else if ((_ref2 = token[0], __indexOf.call(EXPRESSION_END, _ref2) >= 0)) { levels -= 1; } i += 1; @@ -154,7 +154,7 @@ }; return this.scanTokens(function(token, i, tokens) { var _ref, idx, tag, tok; - if (_ref = tag = token[0], __indexOf.call(EXPRESSION_START, _ref) >= 0) { + if ((_ref = tag = token[0], __indexOf.call(EXPRESSION_START, _ref) >= 0)) { stack.push(tag === 'INDENT' && this.tag(i - 1) === '{' ? '{' : tag); return 1; } @@ -326,7 +326,7 @@ } return this.scanTokens(function(token, i, tokens) { var _ref, inv, match, mtag, oppos, tag, val; - if (_ref = tag = token[0], __indexOf.call(EXPRESSION_START, _ref) >= 0) { + if ((_ref = tag = token[0], __indexOf.call(EXPRESSION_START, _ref) >= 0)) { stack.push(token); return 1; } diff --git a/src/nodes.coffee b/src/nodes.coffee index 8120ba43..749db005 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -1273,11 +1273,9 @@ exports.In = class In extends Base compileLoopTest: (o) -> [sub, ref] = @object.compileReference merge(o, top: yes), precompile: yes - code = utility('indexOf') + ".call(#{ @array.compile o }, #{ref}) " - code += (if @negated then '< 0' else '>= 0') - return code if sub is ref - code = sub + ', ' + code - if @parenthetical then code else "(#{code})" + code = utility('indexOf') + ".call(#{ @array.compile o }, #{ref}) " + + if @negated then '< 0' else '>= 0' + if sub is ref then code else "(#{sub}, #{code})" toString: (idt) -> super idt, @constructor.name + if @negated then '!' else '' diff --git a/test/test_operations.coffee b/test/test_operations.coffee index ce8fa0b3..2b202f11 100644 --- a/test/test_operations.coffee +++ b/test/test_operations.coffee @@ -151,7 +151,10 @@ share = 0 a = -> share++ if share is 0 b = -> share++ if share is 1 c = -> share++ if share is 2 -ok a() not in [b(),c()] and share is 3 +ok a() not in [b(),c()] and share is 3 + +# `in` with cache and `__indexOf` should work in commaed lists +eq [Object() in Array()].length, 1 # Operators should respect new lines as spaced.