Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit f613227

Browse files
authored
Merge pull request #248 from rescript-lang/case-if
Move towards "if" keyword for guards on pattern match cases.
2 parents cf56dff + c47eccc commit f613227

File tree

7 files changed

+32
-13
lines changed

7 files changed

+32
-13
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Unreleased
22

3+
* Implement new syntax for guards on pattern match cases in [3248](https://github.com/rescript-lang/syntax/pull/248)
34
* Implement intelligent breaking for poly-var type expressions in [#246](https://github.com/rescript-lang/syntax/pull/246)
45
* Improve indentation of fast pipe chain in let binding in [#244](https://github.com/rescript-lang/syntax/pull/244)
56
* Improve printing of non-callback arguments in call expressions with callback in [#241](https://github.com/rescript-lang/syntax/pull/241/files)

src/res_core.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3230,7 +3230,7 @@ and parseWhileExpression p =
32303230

32313231
and parsePatternGuard p =
32323232
match p.Parser.token with
3233-
| When ->
3233+
| When | If ->
32343234
Parser.next p;
32353235
Some (parseExpr ~context:WhenExpr p)
32363236
| _ ->
@@ -6132,7 +6132,7 @@ and parsePayload p =
61326132
Parser.next p;
61336133
let pattern = parsePattern p in
61346134
let expr = match p.token with
6135-
| When ->
6135+
| When | If ->
61366136
Parser.next p;
61376137
Some (parseExpr p)
61386138
| _ ->

src/res_printer.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4330,7 +4330,7 @@ and printCase (case: Parsetree.case) cmtTbl =
43304330
| Some expr -> Doc.group (
43314331
Doc.concat [
43324332
Doc.line;
4333-
Doc.text "when ";
4333+
Doc.text "if ";
43344334
printExpressionWithComments expr cmtTbl;
43354335
]
43364336
)
@@ -4791,7 +4791,7 @@ and printPayload (payload : Parsetree.payload) cmtTbl =
47914791
| Some expr ->
47924792
Doc.concat [
47934793
Doc.line;
4794-
Doc.text "when ";
4794+
Doc.text "if ";
47954795
printExpressionWithComments expr cmtTbl;
47964796
]
47974797
| None -> Doc.nil

tests/parsing/grammar/expressions/__snapshots__/parse.spec.js.snap

+8-1
Original file line numberDiff line numberDiff line change
@@ -1224,7 +1224,14 @@ exports[`switch.js 1`] = `
12241224
";;match x with | A -> () | B -> ()
12251225
;;match a + b with | _ -> ()
12261226
;;match a + b with | _ -> ()
1227-
;;match (a, b) with | (Some a, Some b) -> (a + b) + c | _ -> 3"
1227+
;;match (a, b) with | (Some a, Some b) -> (a + b) + c | _ -> 3
1228+
;;match person1 with
1229+
| Teacher _ -> ()
1230+
| Student { reportCard = { gpa } } when gpa < 0.5 ->
1231+
Js.log \\"What's happening\\"
1232+
| Student { reportCard = { gpa } } when gpa > 0.9 ->
1233+
Js.log \\"Take more free time, you study too much.\\"
1234+
| Student _ -> Js.log \\"Heyo\\""
12281235
`;
12291236
12301237
exports[`try.js 1`] = `

tests/parsing/grammar/expressions/switch.js

+11
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,14 @@ switch (a, b) {
1616
| (Some(a), Some(b)) => a + b + c
1717
| _ => 3
1818
}
19+
20+
switch person1 {
21+
| Teacher(_) => () // do nothing
22+
| Student({reportCard: {gpa}}) if gpa < 0.5 =>
23+
Js.log("What's happening")
24+
| Student({reportCard: {gpa}}) if gpa > 0.9 =>
25+
Js.log("Take more free time, you study too much.")
26+
| Student(_) =>
27+
// fall-through, catch-all case
28+
Js.log("Heyo")
29+
}

tests/printer/comments/__snapshots__/render.spec.js.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,9 @@ let multiply = (/* c0 */ m1 /* c1 */, /* c2 */ m2 /* c3 */) => {
601601
}
602602
603603
switch x {
604-
| Blue when /* c0 */ multicore.enabled /* c1 */ === /* c2 */ true /* c3 */ => ()
604+
| Blue if /* c0 */ multicore.enabled /* c1 */ === /* c2 */ true /* c3 */ => ()
605605
| Red
606-
when {
606+
if {
607607
// c0
608608
open /* c1 */ Multicore // c2
609609
// c3
@@ -651,7 +651,7 @@ exports[`case.res 1`] = `
651651
} {
652652
| (None, None) => N.empty
653653
| (Some(n) /* (Node (l1, v1, d1, r1, h1), _) */, _)
654-
when {
654+
if {
655655
open N
656656
heightGet(n) >=
657657
switch N.toOpt(s2) {
@@ -1940,7 +1940,7 @@ let walkList: 'node. (
19401940
) => unit = (~prevLoc=?, ~getLoc, ~walkNode, l, t, comments) => {
19411941
open Location
19421942
switch l {
1943-
| _ when comments == list{} => ()
1943+
| _ if comments == list{} => ()
19441944
| list{} =>
19451945
switch prevLoc {
19461946
| Some(loc) => attach(t.trailing, loc, comments)

tests/printer/other/__snapshots__/render.spec.js.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ let x = 1
109109
@attr(? var)
110110
let x = 1
111111
112-
@attr(? var when x === 1)
112+
@attr(? var if x === 1)
113113
let x = 1
114114
115115
%ext(: let x: int)
@@ -129,21 +129,21 @@ exports[`case.js 1`] = `
129129
"let printExprFunParameters = (~uncurried, parameters) =>
130130
switch parameters {
131131
| list{([], Asttypes.Nolabel, None, {Parsetree.ppat_desc: Ppat_any})}
132-
when !uncurried =>
132+
if !uncurried =>
133133
Doc.text(\\"_\\")
134134
| list{(
135135
[],
136136
Asttypes.Nolabel,
137137
None,
138138
{Parsetree.ppat_desc: Ppat_var(stringLoc)},
139-
)} when !uncurried =>
139+
)} if !uncurried =>
140140
Doc.text(stringLoc.txt)
141141
| list{(
142142
[],
143143
Nolabel,
144144
None,
145145
{ppat_desc: Ppat_construct({txt: Longident.Lident(\\"()\\")}, None)},
146-
)} when !uncurried =>
146+
)} if !uncurried =>
147147
Doc.text(\\"()\\")
148148
| parameters =>
149149
let lparen = if uncurried {

0 commit comments

Comments
 (0)