From a64afe61622094e1eee160d231dbdba18584d844 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Mon, 22 Feb 2010 19:22:09 -0500 Subject: [PATCH] fixing the trailing-else-in-switch-getting-sucked-in-bug, Issue 195. --- lib/grammar.js | 2 +- lib/nodes.js | 12 +++++++++--- lib/parser.js | 2 +- src/grammar.coffee | 2 +- src/nodes.coffee | 9 ++++++--- test/test_switch.coffee | 10 ++++++++++ 6 files changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/grammar.js b/lib/grammar.js index 4ff0021a..f8537eb9 100644 --- a/lib/grammar.js +++ b/lib/grammar.js @@ -462,7 +462,7 @@ Switch: [o("SWITCH Expression INDENT Whens OUTDENT", function() { return $4.rewrite_condition($2); }), o("SWITCH Expression INDENT Whens ELSE Block OUTDENT", function() { - return $4.rewrite_condition($2).add_else($6); + return $4.rewrite_condition($2).add_else($6, true); }) ], // The inner list of whens. diff --git a/lib/nodes.js b/lib/nodes.js index b28879b5..9374746e 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -1246,9 +1246,15 @@ return this; }, // Rewrite a chain of IfNodes to add a default case as the final else. - add_else: function add_else(exprs) { - this.is_chain() ? this.else_body.add_else(exprs) : (this.else_body = exprs && exprs.unwrap()); - this.children.push(exprs); + add_else: function add_else(exprs, statement) { + if (this.is_chain()) { + this.else_body.add_else(exprs); + } else { + if (!(statement)) { + exprs = exprs.unwrap(); + } + this.children.push((this.else_body = exprs)); + } return this; }, // If the else_body is an IfNode itself, then we've got an if-else chain. diff --git a/lib/parser.js b/lib/parser.js index 6eeb5cf2..9bd10a12 100755 --- a/lib/parser.js +++ b/lib/parser.js @@ -356,7 +356,7 @@ case 167:this.$ = (function () { break; case 168:this.$ = $$[$0-5+4-1].rewrite_condition($$[$0-5+2-1]); break; -case 169:this.$ = $$[$0-7+4-1].rewrite_condition($$[$0-7+2-1]).add_else($$[$0-7+6-1]); +case 169:this.$ = $$[$0-7+4-1].rewrite_condition($$[$0-7+2-1]).add_else($$[$0-7+6-1], true); break; case 170:this.$ = $$[$0-1+1-1]; break; diff --git a/src/grammar.coffee b/src/grammar.coffee index 6fb430d5..ce8b99e1 100644 --- a/src/grammar.coffee +++ b/src/grammar.coffee @@ -394,7 +394,7 @@ grammar: { # Switch/When blocks. Switch: [ o "SWITCH Expression INDENT Whens OUTDENT", -> $4.rewrite_condition($2) - o "SWITCH Expression INDENT Whens ELSE Block OUTDENT", -> $4.rewrite_condition($2).add_else($6) + o "SWITCH Expression INDENT Whens ELSE Block OUTDENT", -> $4.rewrite_condition($2).add_else($6, true) ] # The inner list of whens. diff --git a/src/nodes.coffee b/src/nodes.coffee index bcd7ecd8..d47a4e9e 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -985,9 +985,12 @@ IfNode: exports.IfNode: inherit Node, { this # Rewrite a chain of IfNodes to add a default case as the final else. - add_else: (exprs) -> - if @is_chain() then @else_body.add_else(exprs) else @else_body: exprs and exprs.unwrap() - @children.push(exprs) + add_else: (exprs, statement) -> + if @is_chain() + @else_body.add_else exprs + else + exprs: exprs.unwrap() unless statement + @children.push @else_body: exprs this # If the else_body is an IfNode itself, then we've got an if-else chain. diff --git a/test/test_switch.coffee b/test/test_switch.coffee index 7f2fc000..549a1ddc 100644 --- a/test/test_switch.coffee +++ b/test/test_switch.coffee @@ -41,3 +41,13 @@ result: switch num += 5 ok result + +# Ensure that trailing switch elses don't get rewritten. +result: false +switch "word" + when "one thing" + do_something() + else + result: true unless false + +ok result