From b810d10e8083a5d92556ace592b64ea258b607c5 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Thu, 15 Jul 2010 21:38:35 -0400 Subject: [PATCH] Fixing Issue #506. existential chains should force parentheses in the presense of a compiled ternary operator. --- lib/nodes.js | 1 + src/nodes.coffee | 5 +++-- test/test_existence.coffee | 5 +++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/nodes.js b/lib/nodes.js index e9f8347a..fbda0569 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -1598,6 +1598,7 @@ }; IfNode.prototype.compileTernary = function(o) { var elsePart, ifPart; + o.operation = true; ifPart = this.condition.compile(o) + ' ? ' + this.bodyNode().compile(o); elsePart = this.elseBody ? this.elseBodyNode().compile(o) : 'null'; return "" + ifPart + " : " + elsePart; diff --git a/src/nodes.coffee b/src/nodes.coffee index 4d10f76b..687f0bf0 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -1402,8 +1402,9 @@ exports.IfNode: class IfNode extends BaseNode # Compile the IfNode as a ternary operator. compileTernary: (o) -> - ifPart: @condition.compile(o) + ' ? ' + @bodyNode().compile(o) - elsePart: if @elseBody then @elseBodyNode().compile(o) else 'null' + o.operation: true + ifPart: @condition.compile(o) + ' ? ' + @bodyNode().compile(o) + elsePart: if @elseBody then @elseBodyNode().compile(o) else 'null' "$ifPart : $elsePart" # Faux-Nodes diff --git a/test/test_existence.coffee b/test/test_existence.coffee index af9013ea..c8643155 100644 --- a/test/test_existence.coffee +++ b/test/test_existence.coffee @@ -93,3 +93,8 @@ ok result is undefined # Assign to the result of an exsitential operation with a minus. x: null ? - 1 ok x is - 1 + + +# Things that compile to ternaries should force parentheses, like operators do. +duration: if options?.animated then 150 else 0 +ok duration is 0