1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

fixes #1722: operator precedence in unbounded slice compilation

This commit is contained in:
Michael Ficarra 2011-09-21 18:56:20 -04:00
parent 0171204e50
commit 3b5c889040
4 changed files with 13 additions and 6 deletions

View file

@ -950,9 +950,9 @@
var compiled, from, fromStr, to, toStr, _ref2; var compiled, from, fromStr, to, toStr, _ref2;
_ref2 = this.range, to = _ref2.to, from = _ref2.from; _ref2 = this.range, to = _ref2.to, from = _ref2.from;
fromStr = from && from.compile(o, LEVEL_PAREN) || '0'; fromStr = from && from.compile(o, LEVEL_PAREN) || '0';
compiled = to && to.compile(o, LEVEL_PAREN); compiled = to && to.compile(o, LEVEL_ACCESS);
if (to && !(!this.range.exclusive && +compiled === -1)) { if (to && !(!this.range.exclusive && +compiled === -1)) {
toStr = ', ' + (this.range.exclusive ? compiled : SIMPLENUM.test(compiled) ? (+compiled + 1).toString() : "(" + compiled + " + 1) || 9e9"); toStr = ', ' + (this.range.exclusive ? compiled : SIMPLENUM.test(compiled) ? (+compiled + 1).toString() : "" + compiled + " + 1 || 9e9");
} }
return ".slice(" + fromStr + (toStr || '') + ")"; return ".slice(" + fromStr + (toStr || '') + ")";
}; };

View file

@ -106,7 +106,7 @@
startIndent = 0; startIndent = 0;
condition = function(token, i) { condition = function(token, i) {
var one, tag, three, two, _ref, _ref2; var one, tag, three, two, _ref, _ref2;
_ref = this.tokens.slice(i + 1, (i + 3 + 1) || 9e9), one = _ref[0], two = _ref[1], three = _ref[2]; _ref = this.tokens.slice(i + 1, (i + 3) + 1 || 9e9), one = _ref[0], two = _ref[1], three = _ref[2];
if ('HERECOMMENT' === (one != null ? one[0] : void 0)) return false; if ('HERECOMMENT' === (one != null ? one[0] : void 0)) return false;
tag = token[0]; tag = token[0];
return ((tag === 'TERMINATOR' || tag === 'OUTDENT') && !((two != null ? two[0] : void 0) === ':' || (one != null ? one[0] : void 0) === '@' && (three != null ? three[0] : void 0) === ':')) || (tag === ',' && one && ((_ref2 = one[0]) !== 'IDENTIFIER' && _ref2 !== 'NUMBER' && _ref2 !== 'STRING' && _ref2 !== '@' && _ref2 !== 'TERMINATOR' && _ref2 !== 'OUTDENT')); return ((tag === 'TERMINATOR' || tag === 'OUTDENT') && !((two != null ? two[0] : void 0) === ':' || (one != null ? one[0] : void 0) === '@' && (three != null ? three[0] : void 0) === ':')) || (tag === ',' && one && ((_ref2 = one[0]) !== 'IDENTIFIER' && _ref2 !== 'NUMBER' && _ref2 !== 'STRING' && _ref2 !== '@' && _ref2 !== 'TERMINATOR' && _ref2 !== 'OUTDENT'));
@ -155,7 +155,7 @@
var callObject, current, next, prev, seenControl, seenSingle, tag, _ref, _ref2, _ref3; var callObject, current, next, prev, seenControl, seenSingle, tag, _ref, _ref2, _ref3;
tag = token[0]; tag = token[0];
if (tag === 'CLASS' || tag === 'IF') noCall = true; if (tag === 'CLASS' || tag === 'IF') noCall = true;
_ref = tokens.slice(i - 1, (i + 1 + 1) || 9e9), prev = _ref[0], current = _ref[1], next = _ref[2]; _ref = tokens.slice(i - 1, (i + 1) + 1 || 9e9), prev = _ref[0], current = _ref[1], next = _ref[2];
callObject = !noCall && tag === 'INDENT' && next && next.generated && next[0] === '{' && prev && (_ref2 = prev[0], __indexOf.call(IMPLICIT_FUNC, _ref2) >= 0); callObject = !noCall && tag === 'INDENT' && next && next.generated && next[0] === '{' && prev && (_ref2 = prev[0], __indexOf.call(IMPLICIT_FUNC, _ref2) >= 0);
seenSingle = false; seenSingle = false;
seenControl = false; seenControl = false;

View file

@ -728,14 +728,14 @@ exports.Slice = class Slice extends Base
compileNode: (o) -> compileNode: (o) ->
{to, from} = @range {to, from} = @range
fromStr = from and from.compile(o, LEVEL_PAREN) or '0' fromStr = from and from.compile(o, LEVEL_PAREN) or '0'
compiled = to and to.compile o, LEVEL_PAREN compiled = to and to.compile o, LEVEL_ACCESS
if to and not (not @range.exclusive and +compiled is -1) if to and not (not @range.exclusive and +compiled is -1)
toStr = ', ' + if @range.exclusive toStr = ', ' + if @range.exclusive
compiled compiled
else if SIMPLENUM.test compiled else if SIMPLENUM.test compiled
(+compiled + 1).toString() (+compiled + 1).toString()
else else
"(#{compiled} + 1) || 9e9" "#{compiled} + 1 || 9e9"
".slice(#{ fromStr }#{ toStr or '' })" ".slice(#{ fromStr }#{ toStr or '' })"
#### Obj #### Obj

View file

@ -114,3 +114,10 @@ test "the return value of a splice literal should be the RHS", ->
eq (ary[0..] = 3), 3 eq (ary[0..] = 3), 3
arrayEq [ary[0..0] = 0], [0] arrayEq [ary[0..0] = 0], [0]
test "#1722: operator precedence in unbounded slice compilation", ->
list = [0..9]
n = 2 # some truthy number in `list`
arrayEq [0..n], list[..n]
arrayEq [0..n], list[..n or 0]
arrayEq [0..n], list[..if n then n else 0]