From cd670f709ae992fbf2bf7baba4450b1c75dae7d3 Mon Sep 17 00:00:00 2001 From: glennsl Date: Sat, 8 Jul 2023 00:32:37 +0200 Subject: [PATCH 1/3] test(printer): add failing test for type constraint in template literal --- .../syntax/tests/printer/expr/expected/templateLiteral.res.txt | 2 ++ jscomp/syntax/tests/printer/expr/templateLiteral.res | 2 ++ 2 files changed, 4 insertions(+) diff --git a/jscomp/syntax/tests/printer/expr/expected/templateLiteral.res.txt b/jscomp/syntax/tests/printer/expr/expected/templateLiteral.res.txt index 10e925c174..f0085b2d1d 100644 --- a/jscomp/syntax/tests/printer/expr/expected/templateLiteral.res.txt +++ b/jscomp/syntax/tests/printer/expr/expected/templateLiteral.res.txt @@ -49,6 +49,8 @@ let s = ` let s = `$dollar without $braces $interpolation` +let s = `${(s: string)}` + // don't sugar to one single template literal, keep the concatenation `the sky` ++ `is blue` diff --git a/jscomp/syntax/tests/printer/expr/templateLiteral.res b/jscomp/syntax/tests/printer/expr/templateLiteral.res index ba0546a71d..5829bee872 100644 --- a/jscomp/syntax/tests/printer/expr/templateLiteral.res +++ b/jscomp/syntax/tests/printer/expr/templateLiteral.res @@ -49,6 +49,8 @@ let s = ` let s = `$dollar without $braces $interpolation` +let s = `${(s: string)}` + // don't sugar to one single template literal, keep the concatenation `the sky` ++ `is blue` From 3986c1e5caedfab5b64ec01219197e9729141baf Mon Sep 17 00:00:00 2001 From: glennsl Date: Sat, 8 Jul 2023 00:33:25 +0200 Subject: [PATCH 2/3] fix(printer): add parens/braces if necessary in template literal substitutions --- jscomp/syntax/src/res_printer.ml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/jscomp/syntax/src/res_printer.ml b/jscomp/syntax/src/res_printer.ml index 0bdfaa1b4e..41155a3fd5 100644 --- a/jscomp/syntax/src/res_printer.ml +++ b/jscomp/syntax/src/res_printer.ml @@ -3453,6 +3453,12 @@ and printTemplateLiteral ~state expr cmtTbl = printStringContents txt | _ -> let doc = printExpressionWithComments ~state expr cmtTbl in + let doc = + match Parens.expr expr with + | Parens.Parenthesized -> addParens doc + | Braced braces -> printBraces doc expr braces + | Nothing -> doc + in Doc.group (Doc.concat [Doc.text "${"; Doc.indent doc; Doc.rbrace]) in let content = walkExpr expr in From b121d5ad7b43538f44cc34f57e792b94fb755657 Mon Sep 17 00:00:00 2001 From: glennsl Date: Sat, 8 Jul 2023 00:41:42 +0200 Subject: [PATCH 3/3] style: formatting --- jscomp/syntax/src/res_printer.ml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jscomp/syntax/src/res_printer.ml b/jscomp/syntax/src/res_printer.ml index 41155a3fd5..c9331961b0 100644 --- a/jscomp/syntax/src/res_printer.ml +++ b/jscomp/syntax/src/res_printer.ml @@ -1452,7 +1452,8 @@ and printConstructorDeclaration2 ~state i printComments doc cmtTbl cd.pcd_name.loc in let constrArgs = - printConstructorArguments ~isDotDotDot ~state ~indent:true cd.pcd_args cmtTbl + printConstructorArguments ~isDotDotDot ~state ~indent:true cd.pcd_args + cmtTbl in let gadt = match cd.pcd_res with