From 4419f7ca0f2b2c3697de8ec0f958d9137723339d Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sat, 17 Sep 2011 22:21:47 -0400 Subject: [PATCH] Issue #1703, - --x --- lib/coffee-script/nodes.js | 7 ++++--- src/nodes.coffee | 6 ++++-- test/operators.coffee | 4 ++++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/coffee-script/nodes.js b/lib/coffee-script/nodes.js index d779e2ea..e0ad77f7 100644 --- a/lib/coffee-script/nodes.js +++ b/lib/coffee-script/nodes.js @@ -1579,12 +1579,13 @@ }).addElse(this.second).compile(o); }; Op.prototype.compileUnary = function(o) { - var op, parts; + var op, parts, plusMinus; parts = [op = this.operator]; - if ((op === 'new' || op === 'typeof' || op === 'delete') || (op === '+' || op === '-') && this.first instanceof Op && this.first.operator === op) { + plusMinus = op === '+' || op === '-'; + if ((op === 'new' || op === 'typeof' || op === 'delete') || plusMinus && this.first instanceof Op && this.first.operator === op) { parts.push(' '); } - if (op === 'new' && this.first.isStatement(o)) { + if ((plusMinus && this.first instanceof Op) || (op === 'new' && this.first.isStatement(o))) { this.first = new Parens(this.first); } parts.push(this.first.compile(o, LEVEL_OP)); diff --git a/src/nodes.coffee b/src/nodes.coffee index cb8d8637..9eae273e 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -1366,9 +1366,11 @@ exports.Op = class Op extends Base # Compile a unary **Op**. compileUnary: (o) -> parts = [op = @operator] + plusMinus = op in ['+', '-'] parts.push ' ' if op in ['new', 'typeof', 'delete'] or - op in ['+', '-'] and @first instanceof Op and @first.operator is op - @first = new Parens @first if op is 'new' and @first.isStatement o + plusMinus and @first instanceof Op and @first.operator is op + if (plusMinus && @first instanceof Op) or (op is 'new' and @first.isStatement o) + @first = new Parens @first parts.push @first.compile o, LEVEL_OP parts.reverse() if @flip parts.join '' diff --git a/test/operators.coffee b/test/operators.coffee index 21a8ce39..08d2c2c0 100644 --- a/test/operators.coffee +++ b/test/operators.coffee @@ -247,3 +247,7 @@ test "#1234: Applying a splat to :: applies the splat to the wrong object", -> test "#1102: String literal prevents line continuation", -> eq "': '", '' + "': '" + +test "#1703, ---x is invalid JS", -> + x = 2 + eq (- --x), -1 \ No newline at end of file