diff --git a/lib/coffee-script/nodes.js b/lib/coffee-script/nodes.js index b992b52c83..e8a9131d62 100644 --- a/lib/coffee-script/nodes.js +++ b/lib/coffee-script/nodes.js @@ -1013,9 +1013,9 @@ var compiled, from, fromStr, to, toStr, _ref3; _ref3 = this.range, to = _ref3.to, from = _ref3.from; 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)) { - 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 || '') + ")"; }; @@ -2045,6 +2045,7 @@ Op.prototype.compileUnary = function(o) { var op, parts, plusMinus; + if (o.level >= LEVEL_ACCESS) return (new Parens(this)).compile(o); parts = [op = this.operator]; plusMinus = op === '+' || op === '-'; if ((op === 'new' || op === 'typeof' || op === 'delete') || plusMinus && this.first instanceof Op && this.first.operator === op) { diff --git a/src/nodes.coffee b/src/nodes.coffee index 19ac3c52b6..2dd840d570 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -759,13 +759,14 @@ exports.Slice = class Slice extends Base compileNode: (o) -> {to, from} = @range 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) toStr = ', ' + if @range.exclusive compiled else if SIMPLENUM.test compiled - (+compiled + 1).toString() + "#{+compiled + 1}" else + compiled = to.compile o, LEVEL_ACCESS "#{compiled} + 1 || 9e9" ".slice(#{ fromStr }#{ toStr or '' })" @@ -1506,6 +1507,8 @@ exports.Op = class Op extends Base # Compile a unary **Op**. compileUnary: (o) -> + if o.level >= LEVEL_ACCESS + return (new Parens this).compile o parts = [op = @operator] plusMinus = op in ['+', '-'] parts.push ' ' if op in ['new', 'typeof', 'delete'] or diff --git a/test/assignment.coffee b/test/assignment.coffee index 4b13f9fafc..3e8b9783c9 100644 --- a/test/assignment.coffee +++ b/test/assignment.coffee @@ -267,6 +267,10 @@ test "#1005: invalid identifiers allowed on LHS of destructuring assignment", -> CoffeeScript.compile "[@#{v}] = x" CoffeeScript.compile "[@#{v}...] = x" +test "#2055: destructuring assignment with `new`", -> + {length} = new Array + eq 0, length + # Existential Assignment