Skip to content

Commit 6cb8471

Browse files
committed
Allow identifier with modules in tagged template literals (e.g. Pg.sql``)
1 parent 11c385a commit 6cb8471

File tree

6 files changed

+58
-24
lines changed

6 files changed

+58
-24
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#### :nail_care: Polish
1616
- No parens around tagged template literals. https://github.com/rescript-lang/rescript-compiler/pull/6639
17+
- 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
1718

1819
#### :bug: Bug Fix
1920

jscomp/syntax/src/res_core.ml

+10-9
Original file line numberDiff line numberDiff line change
@@ -2071,8 +2071,7 @@ and parsePrimaryExpr ~operand ?(noCall = false) p =
20712071
| Backtick
20722072
when noCall = false && p.prevEndPos.pos_lnum == p.startPos.pos_lnum -> (
20732073
match expr.pexp_desc with
2074-
| Pexp_ident {txt = Longident.Lident ident} ->
2075-
parseTemplateExpr ~prefix:ident p
2074+
| Pexp_ident long_ident -> parseTemplateExpr ~prefix:long_ident p
20762075
| _ ->
20772076
Parser.err ~startPos:expr.pexp_loc.loc_start
20782077
~endPos:expr.pexp_loc.loc_end p
@@ -2253,13 +2252,15 @@ and parseBinaryExpr ?(context = OrdinaryExpr) ?a p prec =
22532252
(* | _ -> false *)
22542253
(* ) *)
22552254

2256-
and parseTemplateExpr ?(prefix = "js") p =
2255+
and parseTemplateExpr ?prefix p =
22572256
let partPrefix =
22582257
(* we could stop treating js and j prefix as something special
22592258
for json, we would first need to remove @as(json`true`) feature *)
22602259
match prefix with
2261-
| "js" | "j" | "json" -> Some prefix
2262-
| _ -> None
2260+
| Some {txt = Longident.Lident (("js" | "j" | "json") as prefix); _} ->
2261+
Some prefix
2262+
| Some _ -> None
2263+
| None -> Some "js"
22632264
in
22642265
let startPos = p.Parser.startPos in
22652266

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

2299-
let genTaggedTemplateCall () =
2300-
let lident = Longident.Lident prefix in
2300+
let genTaggedTemplateCall lident =
23012301
let ident =
23022302
Ast_helper.Exp.ident ~attrs:[] ~loc:Location.none
23032303
(Location.mknoloc lident)
@@ -2348,8 +2348,9 @@ and parseTemplateExpr ?(prefix = "js") p =
23482348
in
23492349

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

23542355
(* Overparse: let f = a : int => a + 1, is it (a : int) => or (a): int =>
23552356
* Also overparse constraints:

jscomp/syntax/tests/printer/other/expected/string.res.txt

+2
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ let heart = "\u2665"
1717
let smile = "emoji: \u{1F600}"
1818

1919
let taggedTemplate = sql`select * from ${table} where id = ${id}`
20+
21+
let taggedTemplate = Pg.sql`select * from ${table} where id = ${id}`

jscomp/syntax/tests/printer/other/string.res

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ let heart = "\u2665"
1616

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

19-
let taggedTemplate = sql`select * from ${table} where id = ${id}`
19+
let taggedTemplate = sql`select * from ${table} where id = ${id}`
20+
21+
let taggedTemplate = Pg.sql`select * from ${table} where id = ${id}`

jscomp/test/tagged_template_test.js

+31-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/tagged_template_test.res

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
@module("./tagged_template_lib.js") @taggedTemplate
2-
external sql: (array<string>, array<string>) => string = "sql"
1+
module Pg = {
2+
@module("./tagged_template_lib.js") @taggedTemplate
3+
external sql: (array<string>, array<string>) => string = "sql"
4+
}
35

46
let table = "users"
57
let id = "5"
68

9+
let queryWithModule = Pg.sql`SELECT * FROM ${table} WHERE id = ${id}`
10+
11+
open Pg
712
let query = sql`SELECT * FROM ${table} WHERE id = ${id}`
813

914
@module("./tagged_template_lib.js") @taggedTemplate
@@ -29,6 +34,10 @@ Mt.from_pair_suites(
2934
(
3035
"with externals, it should return a string with the correct interpolations",
3136
() => Eq(query, "SELECT * FROM 'users' WHERE id = '5'"),
37+
),
38+
(
39+
"with module scoped externals, it should also return a string with the correct interpolations",
40+
() => Eq(queryWithModule, "SELECT * FROM 'users' WHERE id = '5'"),
3241
),
3342
(
3443
"with externals, it should return the result of the function",

0 commit comments

Comments
 (0)