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

Commit b3aa1db

Browse files
author
Iwan
committed
Add extra test cases for printing of comments inside if-let.
1 parent d10ac6e commit b3aa1db

File tree

8 files changed

+2387
-2274
lines changed

8 files changed

+2387
-2274
lines changed

src/napkin_comments_table.ml

Lines changed: 1751 additions & 1711 deletions
Large diffs are not rendered by default.

src/napkin_parsetree_viewer.ml

Lines changed: 562 additions & 562 deletions
Large diffs are not rendered by default.

src/napkin_parsetree_viewer.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,5 @@ val rewriteUnderscoreApply: Parsetree.expression -> Parsetree.expression
137137

138138
(* (__x) => f(a, __x, c) -----> f(a, _, c) *)
139139
val isUnderscoreApplySugar: Parsetree.expression -> bool
140+
141+
val hasIfLetAttribute: Parsetree.attributes -> bool

src/napkin_printer.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2393,12 +2393,19 @@ and printIfChain pexp_attributes ifs elseExpr cmtTbl =
23932393
printExpressionBlock ~braces:true thenExpr cmtTbl;
23942394
]
23952395
| IfLet (pattern, conditionExpr) ->
2396+
let conditionDoc =
2397+
let doc = printExpressionWithComments conditionExpr cmtTbl in
2398+
match Parens.expr conditionExpr with
2399+
| Parens.Parenthesized -> addParens doc
2400+
| Braced braces -> printBraces doc conditionExpr braces
2401+
| Nothing -> doc
2402+
in
23962403
Doc.concat [
23972404
ifTxt;
23982405
Doc.text "let ";
23992406
printPattern pattern cmtTbl;
24002407
Doc.text " = ";
2401-
printExpressionWithComments conditionExpr cmtTbl;
2408+
conditionDoc;
24022409
Doc.space;
24032410
printExpressionBlock ~braces:true thenExpr cmtTbl;
24042411
]

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,34 @@ comment
913913
"
914914
`;
915915

916+
exports[`ifLet.res 1`] = `
917+
"if let /* c0 */ /* c1 */ Some(
918+
/* c2 */ x /* c3 */,
919+
) /* c4 */ = /* c5 */ makeOpt() /* c6 */ {
920+
/* c7 */ foo() /* c8 */
921+
} else {
922+
/* c9 */ bar() /* c10 */
923+
}
924+
925+
let getZ2 = nested =>
926+
if let /* c-4 */ Some(point) /* c-3 */ = /* c-2 */ nested.origin /* c-1 */ {
927+
if let /* c0 */ Some(/* c1 */ z /* c2 */) = /* c3 */ point.z /* c4 */ {
928+
let pi /* inside */ = 3.13 *. point.z // trailing1
929+
let e /* inside1 */ = 42.3 // trailing2
930+
pi *. e // trailing3
931+
} else {
932+
let pi /* inside2 */ = 3.13 // trailing4
933+
let e /* inside3 */ = 42.3 // trailing5
934+
pi *. e // trailing6
935+
}
936+
} else {
937+
let pi /* inside4 */ = 3.13 // trailing7
938+
let e /* inside5 */ = 42.3 // trailing8
939+
pi *. e // trailing9
940+
}
941+
"
942+
`;
943+
916944
exports[`interfaceWithOneSingleComment.nsi 1`] = `
917945
"// retain this comment
918946
"

tests/printer/comments/ifLet.res

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
if /* c0 */ let /* c1 */ Some(/* c2 */ x /* c3 */) /* c4 */ = /* c5 */ makeOpt() /* c6 */ {
2+
/* c7 */ foo() /* c8 */
3+
} else {
4+
/* c9 */ bar() /* c10 */
5+
}
6+
7+
let getZ2 = nested =>
8+
if let /* c-4 */ Some(point) /* c-3 */ = /* c-2 */ nested.origin /* c-1 */ {
9+
if let /* c0 */ Some( /* c1 */ z /* c2 */ ) =/* c3 */ point.z /* c4 */ {
10+
let pi /* inside */ = 3.13 *. point.z // trailing1
11+
let e /* inside1 */= 42.3 // trailing2
12+
pi *. e // trailing3
13+
} else {
14+
let pi /* inside2 */= 3.13 // trailing4
15+
let e /* inside3 */= 42.3 // trailing5
16+
pi *. e // trailing6
17+
}
18+
} else {
19+
let pi /* inside4 */ = 3.13 // trailing7
20+
let e /* inside5 */ = 42.3 // trailing8
21+
pi *. e // trailing9
22+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,6 +2387,13 @@ let a = b
23872387
} else {
23882388
2
23892389
}
2390+
2391+
// print parens correct
2392+
if let Some(x) = (3: option<int>) {
2393+
(x: int)
2394+
} else {
2395+
(20: int)
2396+
}
23902397
"
23912398
`;
23922399

tests/printer/expr/if.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,10 @@ let a = b
215215
} else {
216216
2
217217
}
218+
219+
// print parens correct
220+
if let Some(x) = (3: option<int>) {
221+
(x: int)
222+
} else {
223+
(20: int)
224+
}

0 commit comments

Comments
 (0)