mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
Fixes #2490 -- adding guard-style if blocks
This commit is contained in:
parent
0120db0efc
commit
13fae12f69
4 changed files with 83 additions and 31 deletions
|
@ -507,7 +507,10 @@
|
|||
})
|
||||
],
|
||||
IfBlock: [
|
||||
o('IF Expression Block', function() {
|
||||
o('IF INDENT GuardBlock OUTDENT', function() {
|
||||
$3.type = $1;
|
||||
return $3;
|
||||
}), o('IF Expression Block', function() {
|
||||
return new If($2, $3, {
|
||||
type: $1
|
||||
});
|
||||
|
@ -517,6 +520,15 @@
|
|||
}));
|
||||
})
|
||||
],
|
||||
GuardBlock: [
|
||||
o('Expression Block', function() {
|
||||
return new If($1, $2);
|
||||
}), o('GuardBlock TERMINATOR Expression Block', function() {
|
||||
return $1.addElse(new If($3, $4));
|
||||
}), o('GuardBlock ELSE Block', function() {
|
||||
return $1.addElse($3);
|
||||
})
|
||||
],
|
||||
If: [
|
||||
o('IfBlock'), o('IfBlock ELSE Block', function() {
|
||||
return $1.addElse($3);
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -510,10 +510,19 @@ grammar =
|
|||
# if-related rules are broken up along these lines in order to avoid
|
||||
# ambiguity.
|
||||
IfBlock: [
|
||||
o 'IF INDENT GuardBlock OUTDENT', -> $3.type = $1; $3
|
||||
o 'IF Expression Block', -> new If $2, $3, type: $1
|
||||
o 'IfBlock ELSE IF Expression Block', -> $1.addElse new If $4, $5, type: $3
|
||||
]
|
||||
|
||||
# The guard-style form of *if*, where each expression serves as the truthy
|
||||
# conditional.
|
||||
GuardBlock: [
|
||||
o 'Expression Block', -> new If $1, $2
|
||||
o 'GuardBlock TERMINATOR Expression Block', -> $1.addElse new If $3, $4
|
||||
o 'GuardBlock ELSE Block', -> $1.addElse $3
|
||||
]
|
||||
|
||||
# The full complement of *if* expressions, including postfix one-liner
|
||||
# *if* and *unless*.
|
||||
If: [
|
||||
|
|
|
@ -427,3 +427,23 @@ test "Throw should be usable as an expression.", ->
|
|||
throw new Error 'failed'
|
||||
catch e
|
||||
ok e is 'up'
|
||||
|
||||
|
||||
test "#2490. Block-style, guard-style if/else chain.", ->
|
||||
result = if
|
||||
1 - 1
|
||||
false
|
||||
process.someUndefinedThing
|
||||
false
|
||||
1 + 1
|
||||
'correct'
|
||||
|
||||
eq result, 'correct'
|
||||
|
||||
func = -> if
|
||||
null
|
||||
false
|
||||
else
|
||||
'correct'
|
||||
|
||||
eq func(), 'correct'
|
||||
|
|
Loading…
Reference in a new issue