Skip to content

Commit 6fa3289

Browse files
authored
support tagged templates (#6250)
* support tagged template in rescript and allow to bind to JS tagged templates using @taggedTemplate annotation * Set version to 11.1.0 * add tagged templates to changelog This work was originally based on kevinbarabash#2
1 parent 9f81707 commit 6fa3289

29 files changed

+390
-63
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
> - :house: [Internal]
1111
> - :nail_care: [Polish]
1212
13-
# 11.0.2 (Unreleased)
13+
# 11.1.0 (Unreleased)
14+
15+
#### :rocket: New Feature
16+
17+
- experimental support of tagged template literals, eg ```sql`select * from ${table}```. https://github.com/rescript-lang/rescript-compiler/pull/6250
1418

1519
# 11.0.1
1620

jscomp/common/bs_version.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
* You should have received a copy of the GNU Lesser General Public License
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24-
let version = "11.0.2"
24+
let version = "11.1.0"
2525
let header = "// Generated by ReScript, PLEASE EDIT WITH CARE"
2626
let package_name = ref "rescript"

jscomp/core/j.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ and expression_desc =
115115
This can be constructed either in a static way [E.array_index_by_int] or a dynamic way
116116
[E.array_index]
117117
*)
118+
| Tagged_template of expression * expression list * expression list
118119
| Static_index of expression * string * int32 option
119120
(* The third argument bool indicates whether we should
120121
print it as

jscomp/core/js_analyzer.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) =
103103
| String_append (a, b) | Seq (a, b) -> no_side_effect a && no_side_effect b
104104
| Length (e, _) | Caml_block_tag (e, _) | Typeof e -> no_side_effect e
105105
| Bin (op, a, b) -> op <> Eq && no_side_effect a && no_side_effect b
106+
| Tagged_template (call_expr, strings, values) -> no_side_effect call_expr &&
107+
Ext_list.for_all strings no_side_effect && Ext_list.for_all values no_side_effect
106108
| Js_not _ | Cond _ | FlatCall _ | Call _ | New _ | Raw_js_code _
107109
(* actually true? *) ->
108110
false
@@ -204,7 +206,7 @@ let rec eq_expression ({ expression_desc = x0 } : J.expression)
204206
| _ -> false)
205207
| Length _ | Is_null_or_undefined _ | String_append _ | Typeof _ | Js_not _
206208
| Cond _ | FlatCall _ | New _ | Fun _ | Raw_js_code _ | Array _
207-
| Caml_block_tag _ | Object _
209+
| Caml_block_tag _ | Object _ | Tagged_template _
208210
| Number (Uint _) ->
209211
false
210212
| Await _ -> false

jscomp/core/js_dump.ml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ let exp_need_paren (e : J.expression) =
165165
| Js_not _ | Bool _ | New _ ->
166166
false
167167
| Await _ -> false
168+
| Tagged_template _ -> false
168169

169170
let comma_idents (cxt : cxt) f ls = iter_lst cxt f ls Ext_pp_scope.ident comma
170171

@@ -596,6 +597,24 @@ and expression_desc cxt ~(level : int) f x : cxt =
596597
P.string f L.null;
597598
comma_sp f;
598599
expression ~level:1 cxt f el))
600+
| Tagged_template (callExpr, stringArgs, valueArgs) ->
601+
let cxt = expression cxt ~level f callExpr in
602+
P.string f "`";
603+
let rec aux cxt xs ys = match xs, ys with
604+
| [], [] -> ()
605+
| [{J.expression_desc = Str { txt; _ }}], [] ->
606+
P.string f txt
607+
| {J.expression_desc = Str { txt; _ }} :: x_rest, y :: y_rest ->
608+
P.string f txt;
609+
P.string f "${";
610+
let cxt = expression cxt ~level f y in
611+
P.string f "}";
612+
aux cxt x_rest y_rest
613+
| _ -> assert false
614+
in
615+
aux cxt stringArgs valueArgs;
616+
P.string f "`";
617+
cxt
599618
| String_index (a, b) ->
600619
P.group f 1 (fun _ ->
601620
let cxt = expression ~level:15 cxt f a in

jscomp/core/js_exp_make.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ let call ?comment ~info e0 args : t =
7272
let flat_call ?comment e0 es : t =
7373
{ expression_desc = FlatCall (e0, es); comment }
7474

75+
let tagged_template ?comment callExpr stringArgs valueArgs : t =
76+
{ expression_desc = Tagged_template (callExpr, stringArgs, valueArgs); comment }
77+
7578
let runtime_var_dot ?comment (x : string) (e1 : string) : J.expression =
7679
{
7780
expression_desc =

jscomp/core/js_exp_make.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ val call : ?comment:string -> info:Js_call_info.t -> t -> t list -> t
279279

280280
val flat_call : ?comment:string -> t -> t -> t
281281

282+
val tagged_template : ?comment:string -> t -> t list -> t list -> t
283+
282284
val new_ : ?comment:string -> J.expression -> J.expression list -> t
283285

284286
val array : ?comment:string -> J.mutable_flag -> J.expression list -> t

jscomp/core/js_fold.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ class fold =
124124
let _self = _self#expression _x0 in
125125
let _self = list (fun _self -> _self#expression) _self _x1 in
126126
_self
127+
| Tagged_template (_x0, _x1, _x2) ->
128+
let _self = _self#expression _x0 in
129+
let _self = list (fun _self -> _self#expression) _self _x1 in
130+
let _self = list (fun _self -> _self#expression) _self _x2 in
131+
_self
127132
| String_index (_x0, _x1) ->
128133
let _self = _self#expression _x0 in
129134
let _self = _self#expression _x1 in

jscomp/core/js_record_fold.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ let expression_desc : 'a. ('a, expression_desc) fn =
130130
let st = _self.expression _self st _x0 in
131131
let st = list _self.expression _self st _x1 in
132132
st
133+
| Tagged_template (_xo, _x1, _x2) ->
134+
let st = _self.expression _self st _xo in
135+
let st = list _self.expression _self st _x1 in
136+
let st = list _self.expression _self st _x2 in
137+
st
133138
| String_index (_x0, _x1) ->
134139
let st = _self.expression _self st _x0 in
135140
let st = _self.expression _self st _x1 in

jscomp/core/js_record_iter.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ let expression_desc : expression_desc fn =
107107
| Call (_x0, _x1, _x2) ->
108108
_self.expression _self _x0;
109109
list _self.expression _self _x1
110+
| Tagged_template (_x0, _x1, _x2) ->
111+
_self.expression _self _x0;
112+
list _self.expression _self _x1;
113+
list _self.expression _self _x2
110114
| String_index (_x0, _x1) ->
111115
_self.expression _self _x0;
112116
_self.expression _self _x1

0 commit comments

Comments
 (0)