mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-04-11 03:00:13 -04:00
fixing the trailing-else-in-switch-getting-sucked-in-bug, Issue 195.
This commit is contained in:
@@ -462,7 +462,7 @@
|
|||||||
Switch: [o("SWITCH Expression INDENT Whens OUTDENT", function() {
|
Switch: [o("SWITCH Expression INDENT Whens OUTDENT", function() {
|
||||||
return $4.rewrite_condition($2);
|
return $4.rewrite_condition($2);
|
||||||
}), o("SWITCH Expression INDENT Whens ELSE Block OUTDENT", function() {
|
}), 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.
|
// The inner list of whens.
|
||||||
|
|||||||
12
lib/nodes.js
12
lib/nodes.js
@@ -1246,9 +1246,15 @@
|
|||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
// Rewrite a chain of IfNodes to add a default case as the final else.
|
// Rewrite a chain of IfNodes to add a default case as the final else.
|
||||||
add_else: function add_else(exprs) {
|
add_else: function add_else(exprs, statement) {
|
||||||
this.is_chain() ? this.else_body.add_else(exprs) : (this.else_body = exprs && exprs.unwrap());
|
if (this.is_chain()) {
|
||||||
this.children.push(exprs);
|
this.else_body.add_else(exprs);
|
||||||
|
} else {
|
||||||
|
if (!(statement)) {
|
||||||
|
exprs = exprs.unwrap();
|
||||||
|
}
|
||||||
|
this.children.push((this.else_body = exprs));
|
||||||
|
}
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
// If the else_body is an IfNode itself, then we've got an if-else chain.
|
// If the else_body is an IfNode itself, then we've got an if-else chain.
|
||||||
|
|||||||
@@ -356,7 +356,7 @@ case 167:this.$ = (function () {
|
|||||||
break;
|
break;
|
||||||
case 168:this.$ = $$[$0-5+4-1].rewrite_condition($$[$0-5+2-1]);
|
case 168:this.$ = $$[$0-5+4-1].rewrite_condition($$[$0-5+2-1]);
|
||||||
break;
|
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;
|
break;
|
||||||
case 170:this.$ = $$[$0-1+1-1];
|
case 170:this.$ = $$[$0-1+1-1];
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -394,7 +394,7 @@ grammar: {
|
|||||||
# Switch/When blocks.
|
# Switch/When blocks.
|
||||||
Switch: [
|
Switch: [
|
||||||
o "SWITCH Expression INDENT Whens OUTDENT", -> $4.rewrite_condition($2)
|
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.
|
# The inner list of whens.
|
||||||
|
|||||||
@@ -985,9 +985,12 @@ IfNode: exports.IfNode: inherit Node, {
|
|||||||
this
|
this
|
||||||
|
|
||||||
# Rewrite a chain of IfNodes to add a default case as the final else.
|
# Rewrite a chain of IfNodes to add a default case as the final else.
|
||||||
add_else: (exprs) ->
|
add_else: (exprs, statement) ->
|
||||||
if @is_chain() then @else_body.add_else(exprs) else @else_body: exprs and exprs.unwrap()
|
if @is_chain()
|
||||||
@children.push(exprs)
|
@else_body.add_else exprs
|
||||||
|
else
|
||||||
|
exprs: exprs.unwrap() unless statement
|
||||||
|
@children.push @else_body: exprs
|
||||||
this
|
this
|
||||||
|
|
||||||
# If the else_body is an IfNode itself, then we've got an if-else chain.
|
# If the else_body is an IfNode itself, then we've got an if-else chain.
|
||||||
|
|||||||
@@ -41,3 +41,13 @@ result: switch num += 5
|
|||||||
|
|
||||||
ok result
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user