Skip to content

Commit 157fca2

Browse files
committed
first pass at tagged templates
basically kevinbarabash#2 rebased on master
1 parent 3bb11b4 commit 157fca2

36 files changed

+417
-68
lines changed

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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ 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+
(*
107+
TODO: we should check look at each of the expressions in the 'values' list/array
108+
to determine if any of them have side-effects. For now we'll just return false
109+
to make the compiler happy.
110+
*)
111+
| Tagged_template _ -> false
106112
| Js_not _ | Cond _ | FlatCall _ | Call _ | New _ | Raw_js_code _
107113
(* actually true? *) ->
108114
false
@@ -204,7 +210,7 @@ let rec eq_expression ({ expression_desc = x0 } : J.expression)
204210
| _ -> false)
205211
| Length _ | Is_null_or_undefined _ | String_append _ | Typeof _ | Js_not _
206212
| Cond _ | FlatCall _ | New _ | Fun _ | Raw_js_code _ | Array _
207-
| Caml_block_tag _ | Object _
213+
| Caml_block_tag _ | Object _ | Tagged_template _
208214
| Number (Uint _) ->
209215
false
210216
| Await _ -> false

jscomp/core/js_dump.ml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ let exp_need_paren (e : J.expression) =
147147
match e.expression_desc with
148148
(* | Caml_uninitialized_obj _ *)
149149
| Call ({ expression_desc = Fun _ | Raw_js_code _ }, _, _) -> true
150+
(* TODO: implement this *)
151+
| Tagged_template _ -> false
150152
| Raw_js_code { code_info = Exp _ }
151153
| Fun _
152154
| Caml_block
@@ -596,6 +598,29 @@ and expression_desc cxt ~(level : int) f x : cxt =
596598
P.string f L.null;
597599
comma_sp f;
598600
expression ~level:1 cxt f el))
601+
| Tagged_template (callExpr, stringArgs, valueArgs) ->
602+
let cxt = expression cxt ~level f callExpr in
603+
P.string f "`";
604+
let rec aux cxt xs ys = match xs, ys with
605+
| [], [] -> ()
606+
| x_head :: x_rest, ys ->
607+
let cxt = (match x_head with
608+
| {J.expression_desc = Str { txt; _ }} ->
609+
P.string f txt;
610+
cxt
611+
| _ ->
612+
P.string f "${";
613+
let cxt = expression cxt ~level f x_head in
614+
P.string f "}";
615+
cxt
616+
)
617+
in
618+
aux cxt ys x_rest
619+
| _ -> assert false
620+
in
621+
aux cxt stringArgs valueArgs;
622+
P.string f "`";
623+
cxt
599624
| String_index (a, b) ->
600625
P.group f 1 (fun _ ->
601626
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ 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 (_, _, _) ->
128+
_self
127129
| String_index (_x0, _x1) ->
128130
let _self = _self#expression _x0 in
129131
let _self = _self#expression _x1 in

jscomp/core/js_record_fold.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ 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 (_, _, _) ->
134+
(* TODO: implement this *)
135+
st
133136
| String_index (_x0, _x1) ->
134137
let st = _self.expression _self st _x0 in
135138
let st = _self.expression _self st _x1 in

jscomp/core/js_record_iter.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ 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 (exprCall, _, _) ->
111+
(* TODO: implement this *)
112+
_self.expression _self exprCall;
110113
| String_index (_x0, _x1) ->
111114
_self.expression _self _x0;
112115
_self.expression _self _x1

jscomp/core/js_record_map.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ let expression_desc : expression_desc fn =
130130
let _x0 = _self.expression _self _x0 in
131131
let _x1 = list _self.expression _self _x1 in
132132
Call (_x0, _x1, _x2)
133+
| Tagged_template (callExpr, stringsArray, valuesArray) ->
134+
(* TODO: implement this *)
135+
Tagged_template (callExpr, stringsArray, valuesArray)
133136
| String_index (_x0, _x1) ->
134137
let _x0 = _self.expression _self _x0 in
135138
let _x1 = _self.expression _self _x1 in

jscomp/core/lam.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type apply_status = App_na | App_infer_full | App_uncurry
2828
type ap_info = {
2929
ap_loc : Location.t;
3030
ap_inlined : Lambda.inline_attribute;
31+
ap_tagged_template : bool;
3132
ap_status : apply_status;
3233
}
3334

0 commit comments

Comments
 (0)