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

Fixes #2567 -- optimize generated code with an existential is directly negated.

This commit is contained in:
Jeremy Ashkenas 2012-10-23 18:17:53 -04:00
parent f029695db8
commit 35787ef79b
3 changed files with 15 additions and 2 deletions

View file

@ -2258,10 +2258,14 @@
Op.prototype.compileUnary = function(o) {
var op, parts, plusMinus;
parts = [op = this.operator];
if (op === '!' && this.first instanceof Existence) {
this.first.negated = !this.first.negated;
return this.first.compile(o);
}
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) {
parts.push(' ');

View file

@ -1530,9 +1530,12 @@ exports.Op = class Op extends Base
# Compile a unary **Op**.
compileUnary: (o) ->
parts = [op = @operator]
if op is '!' and @first instanceof Existence
@first.negated = not @first.negated
return @first.compile 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
plusMinus and @first instanceof Op and @first.operator is op

View file

@ -282,3 +282,9 @@ test "#2197: Existential existential double trouble", ->
func()? ? 100
eq counter, 1
test "#2567: Optimization of negated existential produces correct result", ->
a = 1
ok !(!a?)
ok !b?