Skip to content

Polish tagged template literals #6645

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

Merged
merged 2 commits into from
Feb 20, 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

#### :nail_care: Polish
- No parens around tagged template literals. https://github.com/rescript-lang/rescript-compiler/pull/6639
- Allow identifier with modules in tagged template literals (e.g. Pg.sql`select * from ${table} where id = ${id}`). https://github.com/rescript-lang/rescript-compiler/pull/6645

#### :bug: Bug Fix

- Fix compiler crash when reexporting tagged template literal externals. https://github.com/rescript-lang/rescript-compiler/pull/6645

# 11.1.0-rc.2

Expand Down
15 changes: 7 additions & 8 deletions jscomp/core/lam_compile_external_call.ml
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,15 @@ let translate_scoped_access scopes obj =
let translate_ffi (cxt : Lam_compile_context.t) arg_types
(ffi : External_ffi_types.external_spec) (args : J.expression list) =
match ffi with
| Js_call { external_module_name; name; splice; scopes; tagged_template = true } ->
| Js_call { external_module_name; name; splice: _; scopes; tagged_template = true } ->
let fn = translate_scoped_module_val external_module_name name scopes in
(match args with
| [ stringArgs; valueArgs ] -> (
match (stringArgs, valueArgs) with
| ({expression_desc = Array (strings, _); _}, {expression_desc = Array (values, _); _}) ->
E.tagged_template fn strings values
| _ -> assert false
)
| _ -> assert false)
| [ {expression_desc = Array (strings, _); _}; {expression_desc = Array (values, _); _} ] ->
E.tagged_template fn strings values
| _ -> let args, eff, dynamic = assemble_args_has_splice arg_types args in
add_eff eff
(if dynamic then splice_apply fn args
else E.call ~info:{ arity = Full; call_info = Call_na } fn args))
| Js_call { external_module_name = module_name; name = fn; splice; scopes; tagged_template = false } ->
let fn = translate_scoped_module_val module_name fn scopes in
if splice then
Expand Down
19 changes: 10 additions & 9 deletions jscomp/syntax/src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2071,8 +2071,7 @@ and parsePrimaryExpr ~operand ?(noCall = false) p =
| Backtick
when noCall = false && p.prevEndPos.pos_lnum == p.startPos.pos_lnum -> (
match expr.pexp_desc with
| Pexp_ident {txt = Longident.Lident ident} ->
parseTemplateExpr ~prefix:ident p
| Pexp_ident long_ident -> parseTemplateExpr ~prefix:long_ident p
| _ ->
Parser.err ~startPos:expr.pexp_loc.loc_start
~endPos:expr.pexp_loc.loc_end p
Expand Down Expand Up @@ -2253,13 +2252,15 @@ and parseBinaryExpr ?(context = OrdinaryExpr) ?a p prec =
(* | _ -> false *)
(* ) *)

and parseTemplateExpr ?(prefix = "js") p =
and parseTemplateExpr ?prefix p =
let partPrefix =
(* we could stop treating js and j prefix as something special
for json, we would first need to remove @as(json`true`) feature *)
match prefix with
| "js" | "j" | "json" -> Some prefix
| _ -> None
| Some {txt = Longident.Lident (("js" | "j" | "json") as prefix); _} ->
Some prefix
| Some _ -> None
| None -> Some "js"
in
let startPos = p.Parser.startPos in

Expand Down Expand Up @@ -2296,8 +2297,7 @@ and parseTemplateExpr ?(prefix = "js") p =
let values = Ext_list.filter_map parts snd in
let endPos = p.Parser.endPos in

let genTaggedTemplateCall () =
let lident = Longident.Lident prefix in
let genTaggedTemplateCall lident =
let ident =
Ast_helper.Exp.ident ~attrs:[] ~loc:Location.none
(Location.mknoloc lident)
Expand Down Expand Up @@ -2348,8 +2348,9 @@ and parseTemplateExpr ?(prefix = "js") p =
in

match prefix with
| "js" | "j" | "json" -> genInterpolatedString ()
| _ -> genTaggedTemplateCall ()
| Some {txt = Longident.Lident ("js" | "j" | "json"); _} | None ->
genInterpolatedString ()
| Some {txt = lident} -> genTaggedTemplateCall lident

(* Overparse: let f = a : int => a + 1, is it (a : int) => or (a): int =>
* Also overparse constraints:
Expand Down
2 changes: 2 additions & 0 deletions jscomp/syntax/tests/printer/other/expected/string.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ let heart = "\u2665"
let smile = "emoji: \u{1F600}"

let taggedTemplate = sql`select * from ${table} where id = ${id}`

let taggedTemplate = Pg.sql`select * from ${table} where id = ${id}`
4 changes: 3 additions & 1 deletion jscomp/syntax/tests/printer/other/string.res
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ let heart = "\u2665"

let smile = "emoji: \u{1F600}"

let taggedTemplate = sql`select * from ${table} where id = ${id}`
let taggedTemplate = sql`select * from ${table} where id = ${id}`

let taggedTemplate = Pg.sql`select * from ${table} where id = ${id}`
3 changes: 1 addition & 2 deletions jscomp/test/build.ninja
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,7 @@ o test/submodule.cmi test/submodule.cmj : cc test/submodule.res | $bsc $stdlib r
o test/submodule_call.cmi test/submodule_call.cmj : cc test/submodule_call.res | test/submodule.cmj $bsc $stdlib runtime
o test/switch_case_test.cmi test/switch_case_test.cmj : cc test/switch_case_test.res | test/mt.cmj $bsc $stdlib runtime
o test/switch_string.cmi test/switch_string.cmj : cc test/switch_string.res | $bsc $stdlib runtime
o test/tagged_template_test.cmj : cc_cmi test/tagged_template_test.res | test/mt.cmj test/tagged_template_test.cmi $bsc $stdlib runtime
o test/tagged_template_test.cmi : cc test/tagged_template_test.resi | $bsc $stdlib runtime
o test/tagged_template_test.cmi test/tagged_template_test.cmj : cc test/tagged_template_test.res | test/mt.cmj $bsc $stdlib runtime
o test/tailcall_inline_test.cmi test/tailcall_inline_test.cmj : cc test/tailcall_inline_test.res | test/mt.cmj $bsc $stdlib runtime
o test/template.cmi test/template.cmj : cc test/template.res | $bsc $stdlib runtime
o test/test.cmi test/test.cmj : cc test/test.res | $bsc $stdlib runtime
Expand Down
65 changes: 53 additions & 12 deletions jscomp/test/tagged_template_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions jscomp/test/tagged_template_test.res
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
@module("./tagged_template_lib.js") @taggedTemplate
external sql: (array<string>, array<string>) => string = "sql"
module Pg = {
@module("./tagged_template_lib.js") @taggedTemplate
external sql: (array<string>, array<string>) => string = "sql"
}

let table = "users"
let id = "5"

let queryWithModule = Pg.sql`SELECT * FROM ${table} WHERE id = ${id}`

open Pg
let query = sql`SELECT * FROM ${table} WHERE id = ${id}`

@module("./tagged_template_lib.js") @taggedTemplate
Expand All @@ -29,6 +34,10 @@ Mt.from_pair_suites(
(
"with externals, it should return a string with the correct interpolations",
() => Eq(query, "SELECT * FROM 'users' WHERE id = '5'"),
),
(
"with module scoped externals, it should also return a string with the correct interpolations",
() => Eq(queryWithModule, "SELECT * FROM 'users' WHERE id = '5'"),
),
(
"with externals, it should return the result of the function",
Expand Down
Empty file.