Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing insert return after pattern matching statement #1141

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion source/parser/function.civet
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,8 @@ function insertReturn(node: ASTNode, outerNode: ASTNode = node): void
// if block
insertReturn(exp.then)
// else block
if (exp.else) insertReturn(exp.else[2])
// PatternMatchingStatement generates else blocks of []
if (exp.else and exp.else.length !== 0) insertReturn(exp.else[2])
// Add explicit return after if block if no else block
else exp.children.push ["", {
type: "ReturnStatement"
Expand Down
2 changes: 1 addition & 1 deletion test/examples.civet
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ describe "examples (from real life)", ->
---
function foo(a) {
if (bar[a] === 'three') return 3
if (a < 0) return (()=>{let m;if(m = bar[-a],m === 'three') { return 3}})()
if (a < 0) return (()=>{let m;if(m = bar[-a],m === 'three') { return 3};return})()
let ret = 9
ret *= a
return ret
Expand Down
28 changes: 28 additions & 0 deletions test/switch.civet
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,34 @@ describe "switch", ->
}
"""

// #1118
testCase """
break after pattern matching statement
---
fn := (x : number) =>
switch x
when 0
num := 10
switch num
<? "number"
return 0
when 1
return 1
---
const fn = (x : number) => {
switch(x) {
case 0: {
const num = 10
if(typeof num === "number") {
return 0};return
}
case 1: {
return 1
}
}
}
"""

throws """
mixed when and pattern matching
---
Expand Down
Loading