fixes #2055: destructuring assignment with `new`

This commit is contained in:
Michael Ficarra 2012-01-18 23:12:50 -05:00
parent 97cd2dbc41
commit 7c56da26f6
3 changed files with 12 additions and 4 deletions

View File

@ -1013,9 +1013,9 @@
var compiled, from, fromStr, to, toStr, _ref3; var compiled, from, fromStr, to, toStr, _ref3;
_ref3 = this.range, to = _ref3.to, from = _ref3.from; _ref3 = this.range, to = _ref3.to, from = _ref3.from;
fromStr = from && from.compile(o, LEVEL_PAREN) || '0'; fromStr = from && from.compile(o, LEVEL_PAREN) || '0';
compiled = to && to.compile(o, LEVEL_ACCESS); compiled = to && to.compile(o, LEVEL_PAREN);
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) : (compiled = to.compile(o, LEVEL_ACCESS), "" + compiled + " + 1 || 9e9"));
} }
return ".slice(" + fromStr + (toStr || '') + ")"; return ".slice(" + fromStr + (toStr || '') + ")";
}; };
@ -2045,6 +2045,7 @@
Op.prototype.compileUnary = function(o) { Op.prototype.compileUnary = function(o) {
var op, parts, plusMinus; var op, parts, plusMinus;
if (o.level >= LEVEL_ACCESS) return (new Parens(this)).compile(o);
parts = [op = this.operator]; parts = [op = this.operator];
plusMinus = op === '+' || op === '-'; plusMinus = op === '+' || op === '-';
if ((op === 'new' || op === 'typeof' || op === 'delete') || plusMinus && this.first instanceof Op && this.first.operator === op) { if ((op === 'new' || op === 'typeof' || op === 'delete') || plusMinus && this.first instanceof Op && this.first.operator === op) {

View File

@ -759,13 +759,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_ACCESS compiled = to and to.compile o, LEVEL_PAREN
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}"
else else
compiled = to.compile o, LEVEL_ACCESS
"#{compiled} + 1 || 9e9" "#{compiled} + 1 || 9e9"
".slice(#{ fromStr }#{ toStr or '' })" ".slice(#{ fromStr }#{ toStr or '' })"
@ -1506,6 +1507,8 @@ exports.Op = class Op extends Base
# Compile a unary **Op**. # Compile a unary **Op**.
compileUnary: (o) -> compileUnary: (o) ->
if o.level >= LEVEL_ACCESS
return (new Parens this).compile o
parts = [op = @operator] parts = [op = @operator]
plusMinus = op in ['+', '-'] plusMinus = op in ['+', '-']
parts.push ' ' if op in ['new', 'typeof', 'delete'] or parts.push ' ' if op in ['new', 'typeof', 'delete'] or

View File

@ -267,6 +267,10 @@ test "#1005: invalid identifiers allowed on LHS of destructuring assignment", ->
CoffeeScript.compile "[@#{v}] = x" CoffeeScript.compile "[@#{v}] = x"
CoffeeScript.compile "[@#{v}...] = x" CoffeeScript.compile "[@#{v}...] = x"
test "#2055: destructuring assignment with `new`", ->
{length} = new Array
eq 0, length
# Existential Assignment # Existential Assignment